Boost logo

Boost :

From: Phil Richards (news_at_[hidden])
Date: 2004-01-09 13:21:08

On Fri, 09 Jan 2004 08:38:14 -0700, Matthias Schabel wrote:
> This is how it works at the moment, which is fine for linear unit
> transformations. However, to
> convert from Kelvin to Fahrenheit, for example, we need to do an affine
> transformation. What I'm thinking is that the unit_info class will need
> to define both toBase() and fromBase() so that
> struct unit_info<Fahrenheit>
> {
> template<class Y> Y toBase(Y val) { return ((val-32)*(5./9.)); }
> template<class Y> Y fromBase(Y val) { return (val*(9./5.)+32); }
> };

Hmmm. Not sure about that. There is an implicit assumption there that all
quantities represented are "absolute" - that is wrong since it would
require that either "fahrenheit(32) + fahrenheit(32) == fahrenheit(32)" or
that such operations are not permitted. Neither option is correct.

It shouldn't matter what unit system you do you calculation in - you
should always get the same answer - after all:
"celsius(0) + celsius(0) == celsius(0)"

My gut feeling is that you need a convert_unit_relative and
convert_unit_absolute - one that does what you used to do, and one that
does what you want to do now (respectively).

If you are happy with just affine transforms, then this is easiest done by
having a "scaling factor" and a "zero reference" members.

struct unit_info<Fahrenheit>
        template<class Y> Y toBaseScale(Y val) { return Y(5./9.); }
        template<class Y> Y toBaseZero(Y val) { return Y(-32.); }


change name before "@" to "phil" for email

Boost list run by bdawes at, gregod at, cpdaniel at, john at