From: Eric Ford (azsro001_at_[hidden])
Date: 2003-02-28 03:45:36
I decided that I needed a workable units library, so I wrote one. It
allows for weakly typed dimensioned quantities (so a length divided by
a time is automatically converted to a velocity). It also allows
users to use strong typeing for quantities of the same dimension which
shouldn't be confused (so you don't set a mass of apples equal to a
mass of oranges).
I attempted to make it pretty general, allowing for all the standard
SI dimensions, a dimension for money (since that is something lots of
people care about), and I've left one dimension avaliable for users.
Fractional powers of dimnensions are allowed. (It also includes a
compile-time fractions header file that might be useful for other
purposes.) Power users could setup units using their own classes
for the internal numeric type or even provide their own systems of units.
I uploaded the first draft version of it to the vault a ebf_units.zip.
I've included several demonstration programs to show how it can be used.
example1.cpp demonstrates the use of simple SI units, including arithmetic
on such variable and automatic conversion when multiplying or dividing
example2.cpp demonstrates how you could use multiple representations
(e.g., float and double) in the same program. I've included extremely crude
numeric type promtion mainly for demonstration purposes.
example3.cpp demonstrates the use of multiple systems of units in a
single program (e.g., including both standard/si/mks units and
"relativistic units where the speed of light is set to unity).
SIUnits only allows one in a program. Any conversions
between systems must be made explicitly.
example4.cpp demonstrates basic use of "qualifiers". These allow
users to make strongly typed units, so that quantities of the same
dimension, but different meaning can't be confused. (e.g., a mass
of apples and a mass of oranges)
example5.cpp demonstrates how a user can extend this to allow for
some automatic conversions (e.g., automatically convert apples to
fruits, but not vice versa, or add apples and oranges and assign
the result to fruits).
- In the mks_double and mks_single namespaces, time is non-standard in
that it is capitalized unlike all the other dimenions. This is due to
my compiler (g++ 3.0.4 on rh) having conflicts with time (which I
beleive should be in the std namespace). Help on solving this would be appreciated.
- Many more dimensions could be predefined in the standard system
(basically SI). However, I'd prefer not to define every possible unit
(like SIunits), particularlly those that are not frequently used
(e.g., furlong) and/or those which can be easily constructed by the
user (e.g., meters per second).
- Headers for other internal numeric representations (e.g., mks_int
???) could be included.
- Other systems (quantum, natural, planetary, ...) could be included.
- The system tag classes (e.g., mks_tag, which provides for
identifying which system of units is being used and labeling the
dimension of quantities) could be made more intelligent. For example,
SIUnits allows users to set the default unit that they'd like a
quantity with a certain dimension to be displayed as. Since one can
simply divide by whatever unit they want their result in, I don't see
much point. However, if someone wanted this, they should be able to
add such features by replacing the sytem tag class class without touching
Bugfixes, improvements, encouragment, and other feedback would be welcome.
Protect yourself from spam,
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk