Boost logo

Boost Users :

Subject: Re: [Boost-users] more Boost::Units questions
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2009-01-12 17:23:05


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 <boost/units/base_units/us/mile.hpp>
> ...
> #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 <boost/units/systems/si/prefixes.hpp>

double mile_to_km = conversion_factor(mile(), si::kilo*si::meter);

>> http://www.boost.org/doc/html/boost_units/Examples.html#boost_units.Examples.ConversionExample
>>
>> 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


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