This is a library for the Rust programming language that allows you to do math with floating point numbers together with units. For example, it allows you to divide 10 meters by 2 seconds yielding 5 meter/second. The library provides two types, methods on those types, and a handful of functions.
This is an enum with constructors for the base SI units,
other SI units,
USA customary units, and others
(LightYear
, Parsec
, Packet
, Bit
, and Byte
).
Compound units can be constructed with the Compound constructor: Compound(@[Meter], @[Second])
or more typically using multiplication and division: Meter/Second
. Dimensionless numbers are
represented using unit Compound(@[], @[])
.
All of the SI prefixes (e.g. Kilo and Micro) are included as well as the IEC binary prefixes (e.g. Kibi and Mebi). The binary prefixes are base 1024 and typically only used to measure computer RAM; other quantities like disk space and bandwidth should use the SI Prefixes.
Value is a struct with a float and a Unit. It supports all the usual operators, allows Values to be converted from one (compatible) unit to another, and provides normalize functions which add prefixes like Kilo or Milli in order to make the value nice.
Invalid operations (e.g. adding a length to a time) cause the task to fail.
let speed = from_units(30.0, Mile/Hour);
let delta = from_units(2.0, Meter/Second);
let sum = speed + delta; // for binary ops the rhs is converted to the lhs units
info!("sum = %s", sum.to_str()); // prints "34.473873 mi/h"
let sum = sum.convert_to(Kilo*Meter/Second);
info!("speed1 = %s", sum.to_str()); // prints "0.015411 km/s"
let sum = sum.normalize_si();
info!("speed2 = %s", sum.to_str()); // prints "15.411200 m/s"