Boost logo

Boost Users :

Subject: [Boost-users] [Boost.Units] radians and degrees
From: Diederick C. Niehorster (dcnieho_at_[hidden])
Date: 2010-10-02 14:17:54


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?
In any case, for my usage case, should I simply build a minimal system
with one dimension, plane_angle, define radian (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?
Any pointers much appreciated! Looking forward to understanding this better.
Best and thanks!
Dee

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