packages

units::unit

On this page

Units: the static Unit of a known dimension (the symbols, what .to and .value_in take) and the dynamic DynUnit that parsing and unit algebra with run-time exponents give.

Modules

  • catalogue The unit catalogue: SI base and derived units, units accepted with the SI, and common other units with their NIST SP 811 factors (exact where the definition is exact).
  • prefix SI prefixes, quecto (1e-30) to quetta (1e30).

Structs

UnitSource

pub struct Unit<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32>

A unit of a dimension fixed in its type: a symbol and the size of one of it in coherent SI units (km is 1000 m). The dimension parameters are those of Quantity. * and / combine units into new ones with the right dimension; u ** n gives a DynUnit, since n is a value (use u.squared(), u.powi::<n>(), or the symbols m², s² to stay static).

nabi
use units::symbols::{km, h, kg, m, s²};

let kmh = km / h;
assert(kmh.symbol() == "km/h");
assert(kmh.of(36.0) == 10m / 1s);
let newton = kg * m / s²;
assert(newton.dim().symbol() == "N");
  • pub const fn new(symbol: str, scale: f64) -> Unit<L, M, T, I, K, N, J>§

    Source

    A unit with symbol worth scale coherent SI units; the dimension comes from the expected type.

    nabi
    use units::Unit;
    
    let furlong: Unit<1, 0, 0, 0, 0, 0, 0> = Unit::new("fur", 201.168);
    assert(furlong.of(1.0) == 201.168m);
    
  • pub const fn of_quantity(symbol: str, q: Quantity<f64, L, M, T, I, K, N, J>) -> Unit<L, M, T, I, K, N, J>§

    Source

    A unit named symbol the size of q, which also gives the dimension.

    nabi
    use units::Unit;
    
    let furlong = Unit::of_quantity("fur", 220yd);
    assert(furlong.of(8.0).approx_eq(1mi));
    
  • pub const fn symbol(self) -> str§

    Source

    The symbol ("km/h").

    nabi
    use units::symbols::kPa;
    
    assert(kPa.symbol() == "kPa");
    
  • pub const fn scale(self) -> f64§

    Source

    One of this unit in coherent SI units.

    nabi
    use units::symbols::inch;
    
    assert(inch.scale() == 0.0254);
    
  • pub const fn dim(self) -> Dim§

    Source

    The dimension, as a value.

    nabi
    use units::symbols::J;
    
    assert(J.dim().name() == "energy");
    
  • pub const fn of(self, value: f64) -> Quantity<f64, L, M, T, I, K, N, J>§

    Source

    value of this unit, as a quantity: km.of(3.0) is 3km.

    nabi
    use units::symbols::km;
    
    assert(km.of(3.0) == 3000m);
    
  • pub fn array<const R: usize>(self, values: Array<f64, R>) -> Quantity<Array<f64, R>, L, M, T, I, K, N, J>§

    Source

    Each element of values, given in this unit, as one array quantity.

    nabi
    use core::array::Array;
    use units::symbols::km;
    
    let d = km.array(Array::from_list([1.0, 2.5], [2]));
    assert(d.at([1]) == 2.5km);
    
  • pub fn dynamic(self) -> DynUnit§

    Source

    The same unit with its dimension as a value.

    nabi
    use units::symbols::mph;
    
    assert(mph.dynamic().dim.name() == "velocity");
    
  • pub fn named(self, symbol: str) -> Unit<L, M, T, I, K, N, J>§

    Source

    The unit renamed: (kg * m / s²).named("N").

    nabi
    use units::symbols::{kg, m, s²};
    
    assert((kg * m / s²).named("N").symbol() == "N");
    
  • pub fn squared(self) -> Unit<{ 2 * L }, { 2 * M }, { 2 * T }, { 2 * I }, { 2 * K }, { 2 * N }, { 2 * J }>§

    Source

    The square of the unit (cm.squared() is cm²).

    nabi
    use units::symbols::cm;
    
    assert(cm.squared().symbol() == "cm²");
    
  • pub fn cubed(self) -> Unit<{ 3 * L }, { 3 * M }, { 3 * T }, { 3 * I }, { 3 * K }, { 3 * N }, { 3 * J }>§

    Source

    The cube of the unit (cm.cubed() is cm³).

    nabi
    use units::symbols::{cm, mL};
    
    assert((cm.cubed().scale() - mL.scale()).abs() < 1e-21);
    
  • pub fn inv(self) -> Unit<{ -L }, { -M }, { -T }, { -I }, { -K }, { -N }, { -J }>§

    Source

    The reciprocal unit (s.inv() is 1/s).

    nabi
    use units::symbols::min;
    
    assert(min.inv().of(60.0).value == 1.0);
    
  • pub fn powi<const P: i32>(self) -> Unit<{ L * P }, { M * P }, { T * P }, { I * P }, { K * P }, { N * P }, { J * P }>§

    Source

    The unit to the const power P.

    nabi
    use units::symbols::mm;
    
    assert(mm.powi::<3>().symbol() == "mm³");
    
Trait implementations 6
  • impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> Display for Unit<L, M, T, I, K, N, J>
  • impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32, const L2: i32, const M2: i32, const T2: i32, const I2: i32, const K2: i32, const N2: i32, const J2: i32> ops::Mul<Unit<L2, M2, T2, I2, K2, N2, J2>> for Unit<L, M, T, I, K, N, J>
  • impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32, const L2: i32, const M2: i32, const T2: i32, const I2: i32, const K2: i32, const N2: i32, const J2: i32> ops::Div<Unit<L2, M2, T2, I2, K2, N2, J2>> for Unit<L, M, T, I, K, N, J>
  • impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> ops::Mul<DynUnit> for Unit<L, M, T, I, K, N, J>
  • impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> ops::Div<DynUnit> for Unit<L, M, T, I, K, N, J>
  • impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> ops::Pow<i32> for Unit<L, M, T, I, K, N, J>

DynUnitSource

pub struct DynUnit

A unit whose dimension is a value: what DynUnit::parse returns and Measure carries. offset is nonzero only for the temperature scales (degC, degF): a value v of the unit is v * scale + offset in SI. In a product or quotient an offset unit stands for its interval (J/(kg*degC) is J/(kg*K)).

nabi
use units::DynUnit;

let u = try! DynUnit::parse("kg*m/s^2");
assert(u.dim.symbol() == "N" && u.scale == 1.0);
let p = try! DynUnit::parse("psi");
assert((p.scale - 6894.757293168361).abs() < 1e-9);
  • pub symbol: str§

    Source

    The symbol as written or composed ("km/h").

  • pub scale: f64§

    Source

    One of the unit in coherent SI units.

  • pub offset: f64§

    Source

    The SI value of zero of the unit (273.15 for degC).

  • pub dim: Dim§

    Source

    The dimension.

  • pub fn parse(text: str) throws (UnitError) -> DynUnit§

    Source

    Parse a unit expression: symbols or names with SI prefixes (km, µs, kilometre), *, ·, or / between them, powers as ^2, **2, ², ⁻¹ or a trailing digit (m2), parentheses, and 1/ (1/s). Throws a UnitError that points at the bad part and suggests a known unit.

    nabi
    use units::DynUnit;
    
    assert((try! DynUnit::parse("J/(mol·K)")).dim.name() == none);
    assert((try! DynUnit::parse("µm")).scale == 1e-6);
    assert((try! DynUnit::parse("km/h")).scale == 1.0 / 3.6);
    
  • pub fn coherent(dim: Dim) -> DynUnit§

    Source

    The coherent SI unit of dim (scale 1), printed with named derived units.

    nabi
    use units::{Dim, DynUnit};
    
    assert(DynUnit::coherent(Dim::LENGTH * Dim::MASS / Dim::TIME.pow(2)).symbol == "N");
    
  • pub fn to_si(self, v: f64) -> f64§

    Source

    The SI value of v of this unit.

    nabi
    use units::DynUnit;
    
    assert((try! DynUnit::parse("degC")).to_si(25.0) == 298.15);
    
  • pub fn from_si(self, si: f64) -> f64§

    Source

    The value in this unit of the SI value si.

    nabi
    use units::DynUnit;
    
    assert(((try! DynUnit::parse("degF")).from_si(273.15) - 32.0).abs() < 1e-12);
    
  • pub fn interval(self) -> DynUnit§

    Source

    The unit as an interval: the same scale with no offset (degC becomes a kelvin-sized step).

    nabi
    use units::DynUnit;
    
    assert((try! DynUnit::parse("degC")).interval().offset == 0.0);
    
  • pub fn fixed<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32>(self) throws (UnitError) -> Unit<L, M, T, I, K, N, J>§

    Source

    The static unit of this one, its dimension given by the expected type; throws a UnitError::Mismatch when the dimensions differ (and for an offset unit, which has no static form).

    nabi
    use units::{DynUnit, Unit};
    use units::kinds::Velocity;
    
    let kn: Unit<1, 0, -1, 0, 0, 0, 0> = try! (try! DynUnit::parse("knot")).fixed();
    assert(kn.of(1.0).approx_eq(1852m / 3600s));
    
  • pub fn named(self, symbol: str) -> DynUnit§

    Source

    The unit renamed.

    nabi
    use units::DynUnit;
    
    assert((try! DynUnit::parse("kg*m/s^2")).named("N").symbol == "N");
    
Trait implementations 6
  • impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> ops::Mul<Unit<L, M, T, I, K, N, J>> for DynUnit
  • impl<const L: i32, const M: i32, const T: i32, const I: i32, const K: i32, const N: i32, const J: i32> ops::Div<Unit<L, M, T, I, K, N, J>> for DynUnit
  • impl Display for DynUnit
  • impl ops::Mul for DynUnit
  • impl ops::Div for DynUnit
  • impl ops::Pow<i32> for DynUnit