Units: adding quantities

I have a method that takes a parameter of type quantity<feet> where feet is defined by the following typedef: typedef boost::units::us::foot_base_unit::unit_type feet; I also have this typedef: typedef bu::us::inch_base_unit::unit_type inches; Do I really need to define a typedef like this for every measurement type I want to use? I certainly don't want to specify all those namespaces every time I use one of the measurement types, but this approach seems a bit tedious. I'd like to do something like this: a.setWingspan(195*feet() + 8*inches()); but that doesn't work because there is no match for operator+. What's a good way to add inches to a feet quantity? --- Mark Volkmann

I would not want to write any software that internally calculates with non-metric units. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Mark Volkmann Sent: Friday, January 09, 2009 14:24 To: Boost-users@lists.boost.org Subject: [Boost-users] Units: adding quantities I have a method that takes a parameter of type quantity<feet> where feet is defined by the following typedef: typedef boost::units::us::foot_base_unit::unit_type feet; I also have this typedef: typedef bu::us::inch_base_unit::unit_type inches; Do I really need to define a typedef like this for every measurement type I want to use? I certainly don't want to specify all those namespaces every time I use one of the measurement types, but this approach seems a bit tedious. I'd like to do something like this: a.setWingspan(195*feet() + 8*inches()); but that doesn't work because there is no match for operator+. What's a good way to add inches to a feet quantity? --- Mark Volkmann _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Jan 9, 2009, at 4:41 PM,
I would not want to write any software that internally calculates with non-metric units.
Agreed. I wouldn't either. I'm just trying to learn how to use this library now. The actual units aren't relevant to my questions. Suppose I had a method that accepted meters and I had quantities of meters and centimeters. How can I add two quantities that have different, but related units? Obviously I could get the primitive values, multiple centimeters by 100, add that to the number of meters and create a new quantity. I just want to know if and how the Units library helps with adding quantities of different units.
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org ] On Behalf Of Mark Volkmann Sent: Friday, January 09, 2009 14:24 To: Boost-users@lists.boost.org Subject: [Boost-users] Units: adding quantities
I have a method that takes a parameter of type quantity<feet> where feet is defined by the following typedef:
typedef boost::units::us::foot_base_unit::unit_type feet;
I also have this typedef:
typedef bu::us::inch_base_unit::unit_type inches;
Do I really need to define a typedef like this for every measurement type I want to use? I certainly don't want to specify all those namespaces every time I use one of the measurement types, but this approach seems a bit tedious.
I'd like to do something like this:
a.setWingspan(195*feet() + 8*inches());
but that doesn't work because there is no match for operator+.
What's a good way to add inches to a feet quantity?
--- Mark Volkmann
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
--- Mark Volkmann

I have a method that takes a parameter of type quantity<feet> where feet is defined by the following typedef:
typedef boost::units::us::foot_base_unit::unit_type feet;
I also have this typedef:
typedef bu::us::inch_base_unit::unit_type inches;
Do I really need to define a typedef like this for every measurement type I want to use? I certainly don't want to specify all those namespaces every time I use one of the measurement types, but this approach seems a bit tedious.
If you're not using SI units, you will have to do this for units you use. Of course, you only have to ever do this once, then put it in a header. There are just too many (sometimes inconsistent) definitions for non-standard units and combinations thereof to include "everything" in the Boost.Units library...
I'd like to do something like this:
a.setWingspan(195*feet() + 8*inches());
but that doesn't work because there is no match for operator+.
You need to explicitly cast one length to the type of the other : a.setWingspan(195*feet() + quantity<feet>(8*inches())); or a.setWingspan(195*quantity<inches>(195*feet()) + 8*inches()); There is no way for the compiler to know which way you intended the conversion to go. 1) it is bad design to have the compiler make arbitrary decisions. 2) if I write 1*light_year() + 1*picometer(), precision will be an issue. As we state in the documentation, the main objective of the library is not to make the programmer's life easier, per se. Rather, it is to make it easier to enforce dimensional correctness so that numerical operations are only allowed if everything is dimensionally correct and to do so without incurring runtime overhead. Matthias
participants (3)
-
Mark Volkmann
-
Matthias Schabel
-
peter_foelscheļ¼ agilent.com