defunit v0.2.0 DefUnit

DefUnit defines macros used to create type specs and documentation when working with a "core" set of measurement units, and also defines operators to convert them to and from other units.

Example


use DefUnit

@doc_from_operator "documentation for <~ operator"
@doc_to_operator "documentation for ~> operator"

# Units calculations are done in
DefUnit.core  "m",        :m,     "SI length"
DefUnit.core  "kg",       :kg,    "SI mass"
DefUnit.core  "s",        :s,     "Time"
DefUnit.core  "C",        :c,     "Temperature in Celcius"
DefUnit.core  "ms^{-1}",  :ms,    "Metres per second"

# Units we convert to and from above units
DefUnit.other "feet",     :feet,    0.3048,   :m,   "FPS length and altitude"
DefUnit.other "lbs",      :lbs,     0.453592, :kg,  "FPS mass"

# Units with more complex from/to conversion calculations
DefUnit.other "F", :f,
{
  &((&1 - 32.0) * (5.0 / 9.0)),
  &((&1 * (9.0 / 5.0)) + 32.0)
},
:c, "Temperature in Farhrenheit"

Summary

Macros

core(eq, core_type, description)

Define a 'core' unit.

  • eq is the short name for the unit used in the typedoc - basic LaTeX formatting is supported
  • core_type is the name used in the type spec for this unit
  • description is the description used in the typedoc
other(eq, other_type, ratio, core_type, description)

Define an 'other' unit.

  • eq is the short name for the unit used in the typedoc - basic LaTeX formatting is supported
  • other_type is the name used in the type spec for this unit
  • ratio is either a multiplier to convert this unit to the core unit, or a 2-tuple of from/to conversion functions
  • core_type is the name of the corresponding core type
  • description is the description used in the typedoc