Boost logo

Boost Users :

Subject: Re: [Boost-users] [Boost.Units] radians and degrees
From: alfC (alfredo.correa_at_[hidden])
Date: 2010-10-03 02:15:33


On Oct 2, 11:17 am, "Diederick C. Niehorster" <dcni..._at_[hidden]>
wrote:
> Dear List,
>
> I use VS2010.
>
> I have just ventured into Boost.Units as me and my colleagues
> frequently make errors in our code by mixing up degrees and radians
> (function takes one but we supply it the other). In some cases a
> degree representation makes sense, in other the radian representation.
> Basically, I want to use radian and degree quantities and use implicit
> conversions to freely interchange between the two (say a user supplies
> degrees to a function whose declaration asks for radians, or vice
> versa, things should simply be converted on the fly so that the
> function has the input it expects and its output is not wrong). I have
> no need for the SI system or anything else.
>
> I am however having some trouble understanding something that must be
> rather basic. Consider the following code:
>
> ----
> #include <boost/units/quantity.hpp>
> #include <boost/units/systems/si/plane_angle.hpp>
> #include <boost/units/systems/angle/degrees.hpp>
>
> int main(int argc, char* argv[])
> {
>     boost::units::quantity<boost::units::si::plane_angle>
> r(.38*boost::units::si::radian);
>     boost::units::quantity<boost::units::si::plane_angle>
> d(6*boost::units::degree::degree);
>
>     r = 2.*d;
>     d = 6*boost::units::degree::degree;
>     d = 2.*r;
>
>     return 1;}
>
> ----
>
> The declaration of d works just fine, but assigning exactly the same
> to d two lines below does not, giving me a compilation error where the
> crux is "error C2338:
> (is_implicitly_convertible<Unit2,unit_type>::value == true)". I am
> thinking that the problem here is that boost::units::degree::degree is
> not part of the SI system, but of the system defined in
> boost/units/systems/angle/degrees.hpp. However, why does the
> declaration initialization two lines above work fine?

The reason is what Steven W. said.
If you want to make that line work you have to convert the r.h.s. into
a declaration:

 d =
boost::units::quantity<boost::units::si::plane_angle>(6*boost::units::degree::degree);

(BTW, "using namespace boost::units" is your friend)

> In any case, for my usage case, should I simply build a minimal system
> with one dimension, plane_angle, define radian

I guess if you
 #include <boost/units/systems/si/plane_angle.hpp>
that will be enough for your application. Building a system requieres
some additional code.
There is no runtime penalty for having the si system defined.

> (most stuff does happen
> in radian, so guess it would cut down on conversions) as the base unit
> and supply degree as well? How would I then supply degree? All the
> examples define other units (such as feet instead of meters) in
> seperate systems using make_system, wouldn't that lead to the problems
> I observe above? Maybe a very stupid question, but I haven't quite
> wrapped my head around this: how do I then represent a value in
> degrees, or is this irrelevant?

I don't understand the question. But I think this what you are doing.
I think this will clarify things:
the type
boost::units::quantity<boost::units::si::plane_angle>
olds quantities in radians

while the type
boost::units::quantity<boost::units::angle::plane_angle>
olds quantities in degrees.

This can be useful if you want to avoid converting always to radians.
But the optimization is limited,
because unless you do simple operation like summing angles, all
trigonometric functions will convert to radians first one way or the
other.

Hope it helps,
Alfredo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net