Boost logo

Boost :

From: Geoffrey Romer (geoff.romer_at_[hidden])
Date: 2005-10-09 17:17:27


Would there be any interest in a library for attaching units (physical
or otherwise) to numeric variables, so that the type system could
enforce dimension- and unit-consistency in all calculations, and
convert semi-automatically between compatible units?

To make this a little more concrete, here's a sketch of the kind of
code such a library could enable:

---
quantity<meters, double> distance(1.0);
quantity<newtons, double> force(5.0);
quantity<joules, double> work = distance * force;
quantity<calories, double> work_cal = unit_cast<calories>(work);
work_cal = work;                //Compile error or implicit conversion,
                                //  possibly depending on compiler flag
quantity<watts, double> power = work;
                                //Compile error- incompatible types
---
This library would be somewhat similar to the dimensional-analysis
code given by Abrahams and Gurtovoy in their book[1], but with two
major enhancements:
- it would be defined in terms of specific units (as Abrahams and
Gurtovoy suggest in an exercise), rather than general dimensions, so
it could catch unit-consistency errors as well as
dimension-consistency errors, and it could convert between compatible
units
- it would allow client code to extend the system with arbitrary units
in arbitrary dimensions
I should note that although I gave the example in terms of physical
units for clarity, I intend this library to be useful not just for
physical calculation, but for numerical calculation in general. Almost
any time software deals with numbers, it's really dealing with
*quantities* (numbers that measure something in terms of some unit),
and errors can result if variables with different units are
inappropriately combined. The purpose of this library would be to
allow developers to prevent those errors with strong typing and
compile-time checking, as well as to somewhat ease the task of
converting between units when necessary.
I know there's been a lot of discussion of units for a GUI library, so
I'm especially interested in what people involved in that thread think
of this idea- I want to make something sufficiently powerful to be
useful in a context like that.
[1] http://www.boost.org/libs/mpl/doc/tutorial/tutorial-metafunctions.html

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