unit.h
unit.h is a simple library for working with units and dimensioned quantities in C.
To use it, simply copy the unit.h file into your project and include it where necessary.
You must also define the UNITH_IMPL macro before importing it in one file so the
implementation is compiled.
Working with dimensioned quantities
unit.h provides the U-macro, which accepts a string representing a unit or compound unit and returns the conversion ratio between it and the corresponding SI unit. This conversion ratio can be used to construct dimensioned quantities like so:
double water_density = 1.0 * U("g/cm^3");
Likewise, the UFMT and UARG macros can be used with printf to display these quantities
like so:
printf("The density of water is " UFMT ".\n", UARG(water_density, "kg/m^3"));
Unit Strings
Units are defined as c-strings. Each string consists of a series of unit names separated
by whitespace, each unit may be raised to an arbitrary (non-)integer power with a
^. There may also be a single slash among the units, which negates the exponenents
of all following units.
In the case where there are no units before the slash, the number 1 must be used before it instead: "1/cm^3".
Misc. Notes
Angular measurements are internally represented using radians and steradians so they can work with trigonometric functions.
SI prefixes may be used before unit names. (E.g. "Mm" is a megameter.)
