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

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Boost - Users mailing list
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<boost::units::si::length> Meters;
typedef boost::units::quantity<imperial::length> Feet;
typedef boost::units::quantity<nautical::length> 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:

How can I get this working?


Regards,

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

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

Boost - Users mailing list
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.)

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
Reply | Threaded
Open this post in threaded view
|

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

Boost - Users mailing list
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
Reply | Threaded
Open this post in threaded view
|

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

Boost - Users mailing list
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
Reply | Threaded
Open this post in threaded view
|

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

Boost - Users mailing list
 
> The library has no way to know what the correct
> intermediate unit is. 

Yes, that's what I said.
I expected as much. 

What lead me to believe that this was possible was the wording in the docs:
"These units include conversions between themselves and the meter.", 
I was not sure what the "themselves" referred to, my assumption/hope was clearly wrong.
 

> You can use BOOST_UNITS_DEFAULT_CONVERSION to specify.
> (Yes, I know this is kind of clunky).

Oddly, given these:

BOOST_UNITS_DEFAULT_CONVERSION(imperial::length_base_unit, si::length);
BOOST_UNITS_DEFAULT_CONVERSION(nautical::length_base_unit, si::length);
 
Can this please be added to the docs on that specific example. 

Thanks Steven  and Gavin for the help, exactly what I was looking for. 
Regards,

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

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

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
AMDG

On 09/26/2018 08:29 PM, Gavin Lambert via Boost-users wrote:

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

If you have only the first, then
ft -> nmi is reduced to meters -> nmi which
can be handled directly.  The library doesn't
care about the direction of the conversion.

In Christ,
Steven Watanabe
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users