Boost logo

Boost Users :

Subject: Re: [Boost-users] more Boost::Units questions
From: Matthias Schabel (boost_at_[hidden])
Date: 2009-01-12 17:50:50


> 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);

#include <iostream>

#include <boost/units/cmath.hpp>
#include <boost/units/conversion.hpp>
#include <boost/units/io.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/prefixes.hpp>

#include <boost/units/base_units/us/mile.hpp>

int main()
{
     using namespace boost;
     using namespace boost::units;
     using namespace boost::units::si;
        
     typedef
meter_base_unit
::unit_type meter_type;
     typedef
scaled_base_unit<meter_base_unit,scale<10,static_rational<3> >
>::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;
        
     quantity<si::length> q1(1.0*mile);
     quantity<si::length> q2(1.0*kilo*mile);
     quantity<meter_type> q3(1.0*mile);
     quantity<meter_type> q4(1.0*kilo*mile);
     quantity<mile_type> q5(1.0*kilometer);
     quantity<mile_type> q6(1.0*kilo*meter);
        
     std::cout << q1 << std::endl
              << q2 << std::endl
              << q3 << std::endl
              << q4 << std::endl
              << q5 << std::endl
              << q6 << std::endl;
                        
     return 0;
}



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