The "UNITS" C++ Units Management Framework

The UNITS C++ units management framework provides enforcement of units in C++ mathematical equations. Unless each values units are correctly solved for, the equation will resist being converted to a scalar type.

The UNITS framework:

There may be subtle differences in type promotion within expressions involving units<>, which can result in different results with units checking disabled. Therefore, it is recommended that units checking be either enabled OR disabled identically for both testing and production code. It could be used, for example, for checking that all units are properly accounted for, and then disabled for both automated unit testing and production. However, since the units management is performed at compile time, it is recommended to simply leave it enabled.

How-To

Here is an example of some units managed equations.

#include <units>

// Convert mileage (US Gallons), to MPG (both US and Imperial), and L/100KM.
typedef units::type<float>
                        sif_t;                                  // SI Units Types, over float
sif_t                   sif;                                    // SI Constants
sif_t::us               usf( sif );                             // US/Imperial Constants

sif_t::Mileage          mileage = usf.Mile * 10 / usf.Gallon;
float                   mpg     = mileage / usad.MPG;

float                   eff     = sif_t::Unitless( 1 ) / mileage / sif.L_100KM;

assert.ISEQUALDELTA( double( mpg ),     10.0000, 0.001 );
assert.ISEQUALDELTA( double( eff ),     23.5215, 0.001 );

sif_t::imperial         imf( sif );                             // Imperial Constants
mpg                             = mileage / imf.MPG;
assert.ISEQUALDELTA( double( mpg ),     12.0095, 0.001 );

Examples

See the unit tests units-test.C for many more examples. You can run the unit tests, and see their output here (the UNITS unit tests are implemented using CUT)

See the GNU Make file GNUmakefile for an example of how to enable or disable units.

Download

The latest version is always available in the units-#.#.#.tgz file, here

Copyright (C) 2005 Enbridge Inc.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.