more Boost::Units questions

My biggest issue is that my recent software development experience has involved using Java much more than C++ and I'm trying to learn how to use a C++ library that makes pretty heavy use of templates. Is there a tutorial somewhere that shows examples of doing basic things with this library? The kind of things I'm trying to figure out are: - What is the difference between a "measurement" and a "quantity"? Is it that the former doesn't specify a unit? - Does the library provide functions for converting between different units? ... for example, converting from miles to kilometers. If not, is there a recommended way to do this? - Can I only add quantities that have the same unit or can I add ones with different units and the library will handle conversions for me? - How can I extract the numeric value from a quantity? - How can I extract the unit type from a quantity? Thanks! --- Mark Volkmann

My biggest issue is that my recent software development experience has involved using Java much more than C++ and I'm trying to learn how to use a C++ library that makes pretty heavy use of templates. Is there a tutorial somewhere that shows examples of doing basic things with this library? The kind of things
The examples directory has simple, functional demonstrations of just about every type of functionality that anyone has wanted to date.
- What is the difference between a "measurement" and a "quantity"? Is it that the former doesn't specify a unit?
measurement is just a toy class to demonstrate that you can integrate units with non-trivial UDTs.
- Does the library provide functions for converting between different units? ... for example, converting from miles to kilometers. If not, is there a recommended way to do this?
Dimensionally consistent quantities can be converted by explicit construction or through conversion_factor. See the examples I listed earlier.
- Can I only add quantities that have the same unit or can I add ones with different units and the library will handle conversions for me?
Any dimensionally-consistent quantities may be combined, but you need to stipulate the conversion path yourself - the library specifically disables implicit conversions because these are impossible to make consistent.
- How can I extract the numeric value from a quantity?
The value() member function.
- How can I extract the unit type from a quantity?
Did you look at the documentation for class quantity? Use the unit_type typedef. I really strongly recommend you go through the example code. Just about everything is in there and is reasonably well commented. Matthias

On Jan 9, 2009, at 5:33 PM, Matthias Schabel wrote:
My biggest issue is that my recent software development experience has involved using Java much more than C++ and I'm trying to learn how to use a C++ library that makes pretty heavy use of templates. Is there a tutorial somewhere that shows examples of doing basic things with this library? The kind of things
The examples directory has simple, functional demonstrations of just about every type of functionality that anyone has wanted to date.
Okay, I've looked at the code related to conversions. conversion_factor.cpp sets up a bunch of double values that I assume are used to convert values in one unit to values in another. However, they aren't used in any code.
- Does the library provide functions for converting between different units? ... for example, converting from miles to kilometers. If not, is there a recommended way to do this?
Dimensionally consistent quantities can be converted by explicit construction or through conversion_factor. See the examples I listed earlier.
I don't see any example that actually performs a conversion using conversion_factor. Can you show me how to use that to convert miles to kilometers? It looks like kilometers isn't defined in any of the .hpp files. I can just multiply meters by 1000 myself, but if there's something in the library that hides this detail, I'd like to know about it. --- Mark Volkmann

AMDG Mark Volkmann wrote:
On Jan 9, 2009, at 5:33 PM, Matthias Schabel wrote:
My biggest issue is that my recent software development experience has involved using Java much more than C++ and I'm trying to learn how to use a C++ library that makes pretty heavy use of templates. Is there a tutorial somewhere that shows examples of doing basic things with this library? The kind of things
The examples directory has simple, functional demonstrations of just about every type of functionality that anyone has wanted to date.
Okay, I've looked at the code related to conversions. conversion_factor.cpp sets up a bunch of double values that I assume are used to convert values in one unit to values in another. However, they aren't used in any code.
conversion_factor isn't used for conversions. It just returns the value that would be used for a conversion.
I don't see any example that actually performs a conversion using conversion_factor. Can you show me how to use that to convert miles to kilometers? It looks like kilometers isn't defined in any of the .hpp files. I can just multiply meters by 1000 myself, but if there's something in the library that hides this detail, I'd like to know about it.
http://www.boost.org/doc/html/boost_units/Examples.html#boost_units.Examples... The example uses SI and CGS, but converting between any units is similar. In Christ, Steven Watanabe

On Jan 12, 2009, at 3:48 PM, Steven Watanabe wrote:
AMDG
Mark Volkmann wrote:
On Jan 9, 2009, at 5:33 PM, Matthias Schabel wrote:
My biggest issue is that my recent software development experience has involved using Java much more than C++ and I'm trying to learn how to use a C++ library that makes pretty heavy use of templates. Is there a tutorial somewhere that shows examples of doing basic things with this library? The kind of things
The examples directory has simple, functional demonstrations of just about every type of functionality that anyone has wanted to date.
Okay, I've looked at the code related to conversions. conversion_factor.cpp sets up a bunch of double values that I assume are used to convert values in one unit to values in another. However, they aren't used in any code.
conversion_factor isn't used for conversions. It just returns the value that would be used for a conversion.
I thought that was the case. However, I haven't yet worked out how to
create a conversion_factor for miles to kilometers. That should be
just a few lines I think. Here's what I tried:
#include
I don't see any example that actually performs a conversion using conversion_factor. Can you show me how to use that to convert miles to kilometers? It looks like kilometers isn't defined in any of the .hpp files. I can just multiply meters by 1000 myself, but if there's something in the library that hides this detail, I'd like to know about it.
http://www.boost.org/doc/html/boost_units/Examples.html#boost_units.Examples... The example uses SI and CGS, but converting between any units is similar.
Thanks. I've read that page several times already and still don't get
it. I suspect my lack of experience with C++ templates is the issue. I
see this:
This example demonstrates the various allowed conversions between
SI and CGS units.
Defining some quantities
quantitysi::length L1 =
quantitysi::length,int(int(2.5)*si::meters);
quantitysi::length,int
L2(quantitysi::length,double(2.5*si::meters));
* explicit casting of a quantity to a different value_type :
quantitysi::length,int L3 =
static_cast
(L1);
However, I don't know what to specify in place of cgs::length in order to use miles instead of centimeters. I wish that page contained an example of converting from some U.S. unit (such as miles) to some SI unit (such as meters). --- Mark Volkmann

AMDG Mark Volkmann wrote:
Okay, I've looked at the code related to conversions. conversion_factor.cpp sets up a bunch of double values that I assume are used to convert values in one unit to values in another. However, they aren't used in any code.
conversion_factor isn't used for conversions. It just returns the value that would be used for a conversion.
I thought that was the case. However, I haven't yet worked out how to create a conversion_factor for miles to kilometers. That should be just a few lines I think. Here's what I tried:
#include
... #typedef bu::us::mile_base_unit::unit_type mile; ... double kilometer = 1000 * si::meter; double mile_to_km = conversion_factor(mile, kilometer); That last line doesn't compile. Do you see what I'm doing wrong?
Only the last line fails? The only line of that which should compile
is the #include.
conversion_factor return the conversion between two different units.
kilometer in your example is not a unit.
#include
http://www.boost.org/doc/html/boost_units/Examples.html#boost_units.Examples...
The example uses SI and CGS, but converting between any units is similar.
Thanks. I've read that page several times already and still don't get it. I suspect my lack of experience with C++ templates is the issue. I see this:
<snip>
However, I don't know what to specify in place of cgs::length in order to use miles instead of centimeters.
I wish that page contained an example of converting from some U.S. unit (such as miles) to some SI unit (such as meters).
The first template argument of quantity<> is supposed to be a unit. Once you have the correct quantity/unit types conversions are easy. typedef bu::us::mile_base_unit::unit_type mile; quantity<mile> m(5.90e4 * si::meters); In Christ, Steven Watanabe

On Jan 12, 2009, at 4:23 PM, Steven Watanabe wrote:
AMDG
Mark Volkmann wrote:
Okay, I've looked at the code related to conversions. conversion_factor.cpp sets up a bunch of double values that I assume are used to convert values in one unit to values in another. However, they aren't used in any code.
conversion_factor isn't used for conversions. It just returns the value that would be used for a conversion.
I thought that was the case. However, I haven't yet worked out how to create a conversion_factor for miles to kilometers. That should be just a few lines I think. Here's what I tried:
#include
... #typedef bu::us::mile_base_unit::unit_type mile; ... double kilometer = 1000 * si::meter; double mile_to_km = conversion_factor(mile, kilometer); That last line doesn't compile. Do you see what I'm doing wrong?
Only the last line fails? The only line of that which should compile is the #include.
conversion_factor return the conversion between two different units. kilometer in your example is not a unit.
#include
double mile_to_km = conversion_factor(mile(), si::kilo*si::meter);
http://www.boost.org/doc/html/boost_units/Examples.html#boost_units.Examples... The example uses SI and CGS, but converting between any units is similar.
Thanks. I've read that page several times already and still don't get it. I suspect my lack of experience with C++ templates is the issue. I see this:
<snip>
However, I don't know what to specify in place of cgs::length in order to use miles instead of centimeters.
I wish that page contained an example of converting from some U.S. unit (such as miles) to some SI unit (such as meters).
The first template argument of quantity<> is supposed to be a unit. Once you have the correct quantity/unit types conversions are easy.
typedef bu::us::mile_base_unit::unit_type mile;
quantity<mile> m(5.90e4 * si::meters);
Oh my goodness, I actually have working code now demonstrating two ways to perform conversions! Thanks! // Convert miles to kilometers. double mile_to_km = conversion_factor(mile(), si::kilo*si::meter); cout << mile_to_km << std::endl; double marathonMiles = 26.2; double marathonKMs = marathonMiles * mile_to_km; cout << "marathonKMs = " << marathonKMs << endl; // Convert kilometers to miles. quantity<mile> miles(10 * si::kilo * si::meter); cout << "10K miles = " << miles.value() << endl; --- Mark Volkmann

Only the last line fails? The only line of that which should compile is the #include.
conversion_factor return the conversion between two different units. kilometer in your example is not a unit.
#include
double mile_to_km = conversion_factor(mile(), si::kilo*si::meter);
#include <iostream>
#include
::unit_type kilometer_type; typedef us ::mile_base_unit ::unit_type mile_type;
const kilometer_type kilometer;
const mile_type mile;
double miles_to_kilometers = conversion_factor(mile,kilometer);
double kilometers_to_miles = conversion_factor(kilometer,mile);
std::cout << "1 " << mile << " = " << miles_to_kilometers << " "
<< kilo*meters << std::endl;
std::cout << "1 " << kilometer << " = " << kilometers_to_miles <<
" " << mile << std::endl;
quantitysi::length q1(1.0*mile);
quantitysi::length q2(1.0*kilo*mile);
quantity
participants (3)
-
Mark Volkmann
-
Matthias Schabel
-
Steven Watanabe