defunit v0.3.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_to_operator "to SI"
@doc_from_operator "from SI"

# Units calculations are done in
DefUnit.core  "m",                :m,     "SI length"
DefUnit.core  "m2",               :m2,    "SI area"
DefUnit.core  "kg",               :kg,    "SI mass"
DefUnit.core  "kgm<sup>3</sup>",  :kgm3,  "SI density"
DefUnit.core  "s",                :s,     "Time"
DefUnit.core  "C",                :c,     "Temperature in Celcius"
DefUnit.core  "ms<sup>-1</sup>",  :ms,    "SI Velocity"
DefUnit.core  "ms<sup>-2</sup>",  :ms2,   "SI Acceleration"
DefUnit.core  "Nm<sup>2</sup>",   :nm2,   "SI Pressure"

# 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 - use <sup> for ordinals
  • 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 - use <sup> for ordinals
  • 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