Skip to content
FutureBlitz

    Install

    Import std.math.units

    std.math.units

    Quantities with units of length, mass, and time, which the compiler keeps apart.

    Typed units: a Quantity is a value in SI's base units, meters, kilograms, and seconds, with the exponents of its length, mass, and time as Const type arguments, so the compiler keeps Meters apart from Seconds and works out what Meters / Seconds is. Each unit is an Alias of its Quantity: Meters is Quantity<1, 0, 0>, and MetersPerSecond Quantity<1, 0, -1>. + and - take quantities of the same units, * and / any two, adding or taking away their exponents, and a Double scales one.

    Examples

    
    Expect(Kilometers(1).Value == 1000.0 && IsClose(Centimeters(250).Value, 2.5))
    Expect(IsClose(Millimeters(5).Value, 0.005) && IsClose(Grams(1500).Value, 1.5))
    Expect(Tonnes(2).Value == 2000.0 && IsClose(Milliseconds(500).Value, 0.5))
    Expect(Minutes(2).Value == 120.0 && Hours(1).Value == 3600.0)
    Expect(IsClose(KilometersPerHour(36).Value, 10.0) && Abs(Meters(-3)).Value == 3.0)

    std/math/units.fb

    Types

    Quantity struct

    Public Struct Quantity<Const L: Int, Const M: Int, Const T: Int> Implements Comparable, Hashable, Printable, Defaultable

    A value of so many meters, kilograms, and seconds, to the powers L, M, and T: 5 m/s^2 is a Quantity<1, 0, -2> of Value 5.

    Examples

    Local distance = Kilometers(3)
    Local time = Minutes(10)
    Local speed: MetersPerSecond = distance / time
    Expect(IsClose(speed.Value, 5.0))
    Local total: Meters = distance + Meters(500)
    Expect(total.Value == 3500.0)
    // distance + time doesn't compile: they're different units.

    std/math/units.fb, line 38

    • Public Field Value: Double field #

      The value in the base units, and the units they make.

    • Public Static Method Zero(): Quantity<L, M, T> static method #

      A quantity of 0 of these units.

    • Public Static Method DefaultValue(): Quantity<L, M, T> static method #

      Zero, in its units.

    • Public Method ToString(): String method #

      The value and its units, as SI writes them: "9.8 m/s^2", "3 kg m/s^2", "50 1/s", or "4 kg/(m s^2)", and the value alone without any.

    • Operator +(a: Quantity<L, M, T>, b: Quantity<L, M, T>): Quantity<L, M, T> operator #

      a plus b.

    • Operator -(a: Quantity<L, M, T>, b: Quantity<L, M, T>): Quantity<L, M, T> operator #

      a minus b.

    • Operator -(a: Quantity<L, M, T>): Quantity<L, M, T> operator #

      a negated.

    • Operator *(a: Quantity<L, M, T>, scale: Double): Quantity<L, M, T> operator #

      a times scale.

    • Operator *(scale: Double, a: Quantity<L, M, T>): Quantity<L, M, T> operator #

      scale times a.

    • Operator /(a: Quantity<L, M, T>, scale: Double): Quantity<L, M, T> operator #

      a divided by scale.

    • Operator *<Const L2: Int, Const M2: Int, Const T2: Int>(a: Quantity<L, M, T>, b: Quantity<L2, M2, T2>): Quantity<L + L2, M + M2, T + T2> operator #

      a times b, whose exponents are the sum of a's and b's.

    • Operator /<Const L2: Int, Const M2: Int, Const T2: Int>(a: Quantity<L, M, T>, b: Quantity<L2, M2, T2>): Quantity<L - L2, M - M2, T - T2> operator #

      a divided by b, whose exponents are the difference of a's and b's.

    • Operator <(a: Quantity<L, M, T>, b: Quantity<L, M, T>): Bool operator #

      Whether a is less than b, in the same units.

    Meters alias

    Public Alias Meters = Quantity<1, 0, 0>

    The base units, and those made from them.

    std/math/units.fb, line 126

    Kilograms alias

    Public Alias Kilograms = Quantity<0, 1, 0>

    Kilograms, of mass.

    std/math/units.fb, line 128

    Seconds alias

    Public Alias Seconds = Quantity<0, 0, 1>

    Seconds, of time.

    std/math/units.fb, line 130

    SquareMeters alias

    Public Alias SquareMeters = Quantity<2, 0, 0>

    Square meters, of area.

    std/math/units.fb, line 132

    CubicMeters alias

    Public Alias CubicMeters = Quantity<3, 0, 0>

    Cubic meters, of volume.

    std/math/units.fb, line 134

    MetersPerSecond alias

    Public Alias MetersPerSecond = Quantity<1, 0, -1>

    Meters per second, of speed.

    std/math/units.fb, line 136

    MetersPerSecondSquared alias

    Public Alias MetersPerSecondSquared = Quantity<1, 0, -2>

    Meters per second squared, of acceleration.

    std/math/units.fb, line 138

    Hertz alias

    Public Alias Hertz = Quantity<0, 0, -1>

    Hertz, a time each second, of frequency.

    std/math/units.fb, line 140

    Newtons alias

    Public Alias Newtons = Quantity<1, 1, -2>

    Newtons, of force.

    std/math/units.fb, line 142

    NewtonSeconds alias

    Public Alias NewtonSeconds = Quantity<1, 1, -1>

    Newton seconds, of momentum.

    std/math/units.fb, line 144

    Joules alias

    Public Alias Joules = Quantity<2, 1, -2>

    Joules, of energy.

    std/math/units.fb, line 146

    Watts alias

    Public Alias Watts = Quantity<2, 1, -3>

    Watts, of power.

    std/math/units.fb, line 148

    Pascals alias

    Public Alias Pascals = Quantity<-1, 1, -2>

    Pascals, of pressure.

    std/math/units.fb, line 150

    KilogramsPerCubicMeter alias

    Public Alias KilogramsPerCubicMeter = Quantity<-3, 1, 0>

    Kilograms per cubic meter, of density.

    std/math/units.fb, line 152

    Functions

    Abs function

    Public Function Abs<Const L: Int, Const M: Int, Const T: Int>(q: Quantity<L, M, T>): Quantity<L, M, T>

    How big a quantity is, of the same units, as the scalar Abs has it: 0 for -0.

    std/math/units.fb, line 120

    Kilometers function

    Public Function Kilometers(value: Double): Meters

    Other units, as the base units' quantities.

    std/math/units.fb, line 155

    Centimeters function

    Public Function Centimeters(value: Double): Meters

    The meters of value centimeters.

    std/math/units.fb, line 157

    Millimeters function

    Public Function Millimeters(value: Double): Meters

    The meters of value millimeters.

    std/math/units.fb, line 159

    Grams function

    Public Function Grams(value: Double): Kilograms

    The kilograms of value grams.

    std/math/units.fb, line 161

    Tonnes function

    Public Function Tonnes(value: Double): Kilograms

    The kilograms of value tonnes.

    std/math/units.fb, line 163

    Milliseconds function

    Public Function Milliseconds(value: Double): Seconds

    The seconds of value milliseconds.

    std/math/units.fb, line 165

    Minutes function

    Public Function Minutes(value: Double): Seconds

    The seconds of value minutes.

    std/math/units.fb, line 167

    Hours function

    Public Function Hours(value: Double): Seconds

    The seconds of value hours.

    std/math/units.fb, line 169

    KilometersPerHour function

    Public Function KilometersPerHour(value: Double): MetersPerSecond

    The meters per second of value kilometers per hour.

    std/math/units.fb, line 171

    Report an issue with this page or with FutureBlitz on GitHub.