C++ Units
by Calum Grant
Description
A C++ library to provide numerical quantities with units. This catches many programming errors and provides implicit conversion between compatible quantities.
Download
Latest version: units.hpp
Manual: units.html
Home page: http://calumgrant.net/units
Purpose
Conversion errors are very common programming errors. It is very easy to pass a value in seconds when you mean minutes, or just pass the wrong number to the wrong argument. Units catch such errors at compile time.
Units provide simple automatic conversion, that just do the right thing when you expect them to, so you can assign, add or compare seconds with minutes, meters with inches, or indeed any compatible quantities. A large number of SI and imperial units are provided by the library.
Arithmetic respects units, so you can only add compatible quantities (of the same dimension) together, and when you multiply or divide values, you multiply or divide the units. This catches mistakes in formulae. A number of physical constants are provided with their correct units.
Example
#include "units.hpp" #include <iostream>using namespace units::values;int main() { std::cout << "One mile is " << km(mile(1)) << std::endl; // Output: One mile is 1.60934 km std::cout << "Flow rate is " << m3(mile(1)*inch(80)*foot(9))/s(minute(5)); // Output: Flow rate is 29.9026 (m)^3.(s)^-1 hours h; h = cm(3); // Compile-time error: incompatible units h = 4; // Compile-time error: 4 of what? h = days(4); // Ok: h is 96 hoursreturn 0; }