Boost logo

Boost Users :

Subject: Re: [Boost-users] [Units] Units in unit tests
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2017-05-08 14:08:06


AMDG

On 05/08/2017 07:30 AM, Stephan Menzel via Boost-users wrote:
> Hello Steven,
>
> thanks for your great work on Units!
>
> On Mon, May 8, 2017 at 3:11 PM, Steven Watanabe via Boost-users <
> boost-users_at_[hidden]> wrote:
>>
>>
>> The meaning is literally division. (1.0 * meters) / meters = 1.0
>> If the expression exactly cancels out all units, then the
>> result is dimensionless and can be implicitly converted
>> to a double.
>>
>
> OK, I see. From my perspective, having never used Units this was not quite
> intuitive if you don't mind me say so. If you would allow me to get a
> little off topic....
>

  This trick wasn't really a part of the library
design. We originally intended that the primary
way to access the raw value would be though q.value()
or quantity_cast<double>(q). Using q / meters falls
out naturally from the definition of division, though.

> <snip>
> Looks good enough. But getting the value back in centimeters was not. I
> tried:
>
> double raw_value_to_send_back = q / centi*meters;
>
> But this won't compile. So I looked into scaled units and typedefed this:
>
> typedef boost::units::make_scaled_unit<
> boost::units::si::length,
> boost::units::scale<10, boost::units::static_rational<-2> > >::type
> centimeters;
>
> And now I can convert into centimers like this:
>
> double raw_value_to_send_back = quantity<centimeters,
> double>(n_value).value();
>
> But still, I'm not sure I am using it as intended.

  Yes. Casting to centimeters and then extracting
the value is one way to do this.

> Is this the best and
> easiest way to handle such things or am I mistaken and q / centi*meters
> really is supposed to work?
>

  q / centi*meters is parsed as (q/centi)*meters, which
is not what you intend. Ideally, static_cast<double>(q/(centi*meters))
would work, but implementing that requires C++11 (explicit
conversion operators) and Units was originally written in 2006.
static_cast<quantity<dimensionless>>(q/(centi*meters)) should work now.

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