# Boost.Units Example: Convert between nautical mile and imperial foot

6 messages
Open this post in threaded view
|

## Boost.Units Example: Convert between nautical mile and imperial foot

 Hi,I am trying to use Boost.Units to convert between different units.I have implemented the example code for the "Radar Beam Height" [1] section but trying to convert between nautical mile and imperial foot result in a compiler error:I have:typedef boost::units::quantity Meters;typedef boost::units::quantity Feet;typedef boost::units::quantity NMiles;double func(){    // Working    const Meters si1(300.0*nautical::miles);    const Meters si2(300.0*imperial::feet);    const Feet feet1(300.0*imperial::feet);    const Feet feet2(300.0*boost::units::si::meters);    const NMiles mile1(300.0*nautical::miles);    const NMiles mile2(300.0*boost::units::si::meters);    // BROKEN    const Feet feet3(300.0*nautical::miles);    const NMiles mile3(300.0*imperial::feet);}The first few works, converting between meters and the new units, but converting between the two does not work. To see the issue see the code on Compiler Explorer here:https://gcc.godbolt.org/z/MpvuMyHow can I get this working?Regards, _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users
Open this post in threaded view
|

## Re: Boost.Units Example: Convert between nautical mile and imperial foot

 On 27/09/2018 00:57, Carel Combrink wrote: > I have implemented the example code for the "Radar Beam Height" [1] > section but trying to convert between nautical mile and imperial foot > result in a compiler error: [...] > The first few works, converting between meters and the new units, but > converting between the two does not work. > > To see the issue see the code on Compiler Explorer here: > https://gcc.godbolt.org/z/MpvuMyYou've defined a conversion between imperial feet and meters, and a conversion between nautical miles and meters. But you haven't defined a direct conversion between imperial feet and nautical miles.  It's apparently not smart enough to do the conversion through an unspecified intermediate unit.  (This might be intentional, since it could be ambiguous or lossy.) You can either write your code to first convert the value into meters explicitly before converting it to the other unit:      const Meters si1(300.0*nautical::miles);      const NMiles mile4(si1); Or you can explicitly define the conversion you want (using the previously defined conversions as a base to avoid duplicating the factors): BOOST_UNITS_DEFINE_CONVERSION_FACTOR(      imperial::length_base_unit,      nautical::length_base_unit,      double,      boost::units::conversion_factor(imperial::foot, si::meter) *          boost::units::conversion_factor(si::meter, nautical::mile)); _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users
Open this post in threaded view
|

## Re: Boost.Units Example: Convert between nautical mile and imperial foot

 AMDG On 09/26/2018 07:30 PM, Gavin Lambert via Boost-users wrote: > On 27/09/2018 00:57, Carel Combrink wrote: >> I have implemented the example code for the "Radar Beam Height" [1] >> section but trying to convert between nautical mile and imperial foot >> result in a compiler error: > [...] >> The first few works, converting between meters and the new units, but >> converting between the two does not work. >> >> To see the issue see the code on Compiler Explorer here: >> https://gcc.godbolt.org/z/MpvuMy> > You've defined a conversion between imperial feet and meters, and a > conversion between nautical miles and meters. > > But you haven't defined a direct conversion between imperial feet and > nautical miles.  It's apparently not smart enough to do the conversion > through an unspecified intermediate unit.  (This might be intentional, > since it could be ambiguous or lossy.) > The library has no way to know what the correct intermediate unit is.  You can use BOOST_UNITS_DEFAULT_CONVERSION to specify. (Yes, I know this is kind of clunky). > You can either write your code to first convert the value into meters > explicitly before converting it to the other unit: > >     const Meters si1(300.0*nautical::miles); >     const NMiles mile4(si1); > > Or you can explicitly define the conversion you want (using the > previously defined conversions as a base to avoid duplicating the factors): > > BOOST_UNITS_DEFINE_CONVERSION_FACTOR( >     imperial::length_base_unit, >     nautical::length_base_unit, >     double, >     boost::units::conversion_factor(imperial::foot, si::meter) * >         boost::units::conversion_factor(si::meter, nautical::mile)); > In Christ, Steven Watanabe _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users
Open this post in threaded view
|

## Re: Boost.Units Example: Convert between nautical mile and imperial foot

 On 27/09/2018 13:56, Steven Watanabe wrote: > The library has no way to know what the correct > intermediate unit is.   Yes, that's what I said. > You can use BOOST_UNITS_DEFAULT_CONVERSION to specify. > (Yes, I know this is kind of clunky). Interesting.  I was not aware of that (and that's not how I initially read the [minimal] docs for it, although now that you've said that I can see how it can be interpreted that way). Oddly, given these: BOOST_UNITS_DEFAULT_CONVERSION(imperial::length_base_unit, si::length); BOOST_UNITS_DEFAULT_CONVERSION(nautical::length_base_unit, si::length); Only one of these two lines actually needs to be defined in order to make both direct conversions (ft -> nmi and nmi -> ft) compile.  Given that all three base units are independent, how does this work? _______________________________________________ Boost-users mailing list [hidden email] https://lists.boost.org/mailman/listinfo.cgi/boost-users