[Boost.Units] radians and degrees
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
AMDG On 10/2/2010 11:17 AM, Diederick C. Niehorster wrote:
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:
---- <snip> However, why does the declaration initialization two lines above work fine?
The Units library doesn't allow implicit conversions between different units. It does allow explicit conversions, which is why direct initialization works. In Christ, Steven Watanabe
On Oct 2, 11:17 am, "Diederick C. Niehorster"
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
#include #include int main(int argc, char* argv[]) { boost::units::quantityboost::units::si::plane_angle r(.38*boost::units::si::radian); boost::units::quantityboost::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
::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::quantityboost::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
(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::quantityboost::units::si::plane_angle olds quantities in radians while the type boost::units::quantityboost::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
I don't understand the question. But I think this what you are doing. I think this will clarify things: the type boost::units::quantityboost::units::si::plane_angle olds quantities in radians
while the type boost::units::quantityboost::units::angle::plane_angle olds quantities in degrees.
should say "holds" instead of "olds"
Thanks Steven and Alfredo, that got me somewhere!
Best,
Dee
On Sun, Oct 3, 2010 at 16:22, Alfredo Correa
I don't understand the question. But I think this what you are doing. I think this will clarify things: the type boost::units::quantityboost::units::si::plane_angle olds quantities in radians
while the type boost::units::quantityboost::units::angle::plane_angle olds quantities in degrees.
should say "holds" instead of "olds" _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
alfC
-
Alfredo Correa
-
Diederick C. Niehorster
-
Steven Watanabe