Boost logo

Boost Users :

Subject: Re: [Boost-users] more Boost::Units questions
From: Mark Volkmann (mark_at_[hidden])
Date: 2009-01-12 17:05:47


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 <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?

>> 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.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:

   This example demonstrates the various allowed conversions between
SI and CGS units.

   Defining some quantities
   quantity<si::length> L1 =
quantity<si::length,int>(int(2.5)*si::meters);
   quantity<si::length,int>
L2(quantity<si::length,double>(2.5*si::meters));

     * explicit casting of a quantity to a different value_type :

       quantity<si::length,int> L3 =
static_cast<quantity<si::length,int> >(L1);

     * and explicit casting of a quantity to a different unit :

       quantity<cgs::length> L4 = static_cast<quantity<cgs::length>
>(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

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