Boost logo

Boost :

From: Michael Fawcett (michael.fawcett_at_[hidden])
Date: 2007-02-16 18:52:37


On 2/16/07, Deane Yang <deane_yang_at_[hidden]> wrote:
> Ah, the dumb mathematician (me) finally gets it. The thing I never saw
> before is "aircraft.altitude". The point is that the different
> quantities (of the same dimension) come from member data of different
> objects, so they're all in different units. And now you want to compare
> or add them together.
>
> I'd still use explicit casts to put everything into the same set of
> units (I like to be able to see explicitly what's going on), but I
> definitely have to concede that a reasonable desire is to have the
> conversions be done automatically and behind the scenes.
>
> I would still prefer to write something like
>
> if (aircraft.altitude > ground_elevation + min_agl_offset)
>
> as
>
> if (unit_cast<elevation_type>(aircraft.altitude) > ground_elevation +
> min_agl_offset)

Same here, I was only wondering aloud. In fact I hope Matthias will
*only* provide the compile-time version in this first iteration.

> So I know and can see easily that what units the computation takes place
> in. If you have a clever dimensions library that handles everything
> without explicit casting, then you don't know what units the computation
> uses. Admittedly, this is probably a misplaced worry of mine, but I can
> certainly envision some kind of iterative computation, where mixing the
> wrong units and having the library make a bad (unseen) choice of units
> to do the computation leads to invalid results. I have a strong aversion
> to hidden choices and effects like this.

Well, I have a question to Matthias concerning similar things. How
does one use a unitless number? Do you use dimensionless?

I posted two formulas that are from a radar handbook. The first is:

h = R^2 / 2ka

where h is the beam height, R is radar range, k is the refractivity
(usually 4/3), and a is the Earth's radius. R and a must be the same
unit, and h is returned in that unit. How would you code this using
mcs::units?

// Is this correct? How would I know what system's dimensionless to use?
template <typename T>
T radar_beam_height(T range, T earth_radius,
quantity<SomeSystem::dimensionless> refractivity =
quantity<SomeSystem::dimensionless>(4/3))
{
   const quantity<SomeSystem::dimensionless> two(2);
   return quantity<T>( pow(range, 2) / ( two * refractivity * range ) );
}

With calling code like:
// What's the radar beam height at 300 nautical miles using the mean
Earth radius?
quantity<SI::length> height =
radar_beam_height(quantity<SI::length>(1028971200),
quantity<SI::length>(6371008.7714));

The other question I have is very open-ended. An approximation for
the above formula where R is given in nautical miles, and h is
returned as feet is:

h = (R / 1.23)^2

How would you go about implementing that with mcs::units? I have no
idea what unit 1.23 is, or how they even derived that number, other
than it has the Earth radius and conversion from nautical miles to
feet built-in.

quantity<imperial::feet> radar_beam_height(quantity<nautical::miles> range)
{
   // This is probably not actually dimensionless, it is probably something like
   // nautical miles / (nautical miles * sqrt(ft)), but that seems meaningless
   const quantity<SomeSystem::dimensionless> c(1.23);

   using namespace std;
   return quantity<imperial::feet>(pow(range / c, 2) );
}

Many thanks,

--Mike


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk