kex docs Standard Library 0.4.0-alpha kex.run ↗

Units.SI

module Units.SI

SI units: metres, grams, watts, volts and the rest, with prefixes and dimensional arithmetic.

Opt-in: nothing here is in scope until using Units.SI.

using Units.SI

main do
  IO.printLine(3.kilo.watt.to(String))          # prints: 3000.0 W
  IO.printLine(5000.meter.kilo.to(String))      # prints: 5.0 km
  IO.printLine((100.meter / 10.sec).to(String)) # prints: 10.0 m/s
end

Every value is a Measure from the prelude, so the arithmetic, conversion and comparison described there apply unchanged. What this module adds is the SI vocabulary, the prefixes, and a table of which dimension results from multiplying or dividing two others, so 2.newton * 3.meter answers in joules and 100.meter / 10.sec in metres per second.

type SIUnit

The SI units this module names.

Each carries its dimension (:length, :mass, :power, …) and its symbol, which is what a Measure built from it displays with.

Variants

  • Meter
  • Gram
  • Kilogram
  • Kelvin
  • Liter
  • Newton
  • Joule
  • Watt
  • Volt
  • Ampere
  • Ohm
  • Coulomb

type SIPrefix

A decimal prefix applied to a unit, for display.

A display prefix carries the unit it will display, for example Kilo(Watt * Hour). Pass one to to(String, in:) to render a measure at that scale.

1500.watt.to(String, in: Kilo(Watt))   # => Just("1.5 kW")

Variants

  • Kilo(Unit)
  • Mega(Unit)
  • Giga(Unit)
  • Milli(Unit)
  • Micro(Unit)
  • Nano(Unit)

make SIPrefix implements Unit

factor

factor(@Kilo(unit))

kind

kind(@Kilo(unit))

symbol

symbol(@Kilo(unit))

make SIUnit implements Unit

kind

kind(@Meter)

symbol

symbol(@Meter)

make UnitDefinition implements Unit

make Measure

function meter

value metres, as a Measure.

meter(value)
Parameters
value Number
the quantity

Returns: Measure — the length

Examples
5000.meter.to(String)        # => "5000.0 m"
5000.meter.kilo.to(String)   # => "5.0 km"

function gram

value grams, as a Measure.

gram(value)
Parameters
value Number
the quantity

Returns: Measure — the mass

Examples
500.gram.to(String)   # => "500.0 g"

function kilogram

value kilograms, as a Measure.

kilogram(value)
Parameters
value Number
the quantity

Returns: Measure — the mass

Examples
1.kilogram.to(String)   # => "1.0 kg"

function kelvin

value kelvin, as a Measure.

kelvin(value)
Parameters
value Number
the quantity

Returns: Measure — the temperature

Examples
300.kelvin.to(String)   # => "300.0 K"

function liter

value litres, as a Measure.

liter(value)
Parameters
value Number
the quantity

Returns: Measure — the volume

Examples
2.liter.to(String)   # => "2.0 L"

function newton

value newtons, as a Measure.

newton(value)
Parameters
value Number
the quantity

Returns: Measure — the force

Examples

Force times distance is energy

(2.newton * 3.meter).to(String)   # => "6.0 J"

function joule

value joules, as a Measure.

joule(value)
Parameters
value Number
the quantity

Returns: Measure — the energy

Examples
100.joule.to(String)   # => "100.0 J"

function watt

value watts, as a Measure.

watt(value)
Parameters
value Number
the quantity

Returns: Measure — the power

Examples
3.kilo.watt.to(String)                 # => "3000.0 W"
1500.watt.to(String, in: Kilo(Watt))   # => Just("1.5 kW")

Power times time is energy

(3.kilo.watt * 2.hour).to(String)   # => "21600000.0 J"

function volt

value volts, as a Measure.

volt(value)
Parameters
value Number
the quantity

Returns: Measure — the voltage

Examples
12.volt.to(String)   # => "12.0 V"

function ampere

value amperes, as a Measure.

ampere(value)
Parameters
value Number
the quantity

Returns: Measure — the current

Examples

Voltage times current is power

(12.volt * 2.ampere).to(String)   # => "24.0 W"

function ohm

value ohms, as a Measure.

ohm(value)
Parameters
value Number
the quantity

Returns: Measure — the resistance

Examples
470.ohm.to(String)   # => "470.0 Ω"

function coulomb

value coulombs, as a Measure.

coulomb(value)
Parameters
value Number
the quantity

Returns: Measure — the charge

Examples
3.coulomb.to(String)   # => "3.0 C"

make SIUnit

*

*(@Watt, @Hour)

function to

Renders a measure as its value followed by its unit symbol.

to(measure, String)
Parameters
String Type
the target type

Returns: String — the rendered measure

Examples
5000.meter.to(String)   # => "5000.0 m"
12.volt.to(String)      # => "12.0 V"

function mega

The same measure, displayed with the mega- prefix.

mega(measure)
Parameters
measure Measure
the measure to rescale for display

Returns: Measure — the same quantity, displayed in mega-units

Examples
5000000.watt.mega.to(String)   # => "5.0 MW"

function giga

The same measure, displayed with the giga- prefix.

giga(measure)
Parameters
measure Measure
the measure to rescale for display

Returns: Measure — the same quantity, displayed in giga-units

function milli

The same measure, displayed with the milli- prefix.

milli(measure)
Parameters
measure Measure
the measure to rescale for display

Returns: Measure — the same quantity, displayed in milli-units

Examples
0.5.watt.milli.to(String)   # => "500.0 mW"

function micro

The same measure, displayed with the micro- prefix.

micro(measure)
Parameters
measure Measure
the measure to rescale for display

Returns: Measure — the same quantity, displayed in micro-units

function nano

The same measure, displayed with the nano- prefix.

nano(measure)
Parameters
measure Measure
the measure to rescale for display

Returns: Measure — the same quantity, displayed in nano-units

function per

Divides one measure by another, naming the resulting dimension.

The spelled-out form of /: 100.meter.per(10.sec) and 100.meter / 10.sec are the same call. Metres over seconds is speed, energy over time is power, force over area is pressure: the dimension table decides, and the symbol follows it.

per(measure, other) : Measure -> Measure -> Measure
Parameters
measure Measure
the numerator
other Measure
the denominator

Returns: Measure — the quotient, in its derived unit

Examples
100.meter.per(10.sec).to(String)   # => "10.0 m/s"

function times

Multiplies one measure by another, naming the resulting dimension.

The spelled-out form of *. Force times distance is energy, voltage times current is power, power times time is energy.

times(measure, other) : Measure -> Measure -> Measure
Parameters
measure Measure
the first factor
other Measure
the second factor

Returns: Measure — the product, in its derived unit

Examples
2.newton.times(3.meter).to(String)   # => "6.0 J"
(12.volt * 2.ampere).to(String)      # => "24.0 W"

make Measure

Prefixes work both on an existing measure (5000.meter.kilo) and at the beginning of a postfix unit expression (3.kilo.watt).

make Integer

make Float

make Measure

*

*(other)

/

/(other)

product

product(other)

quotient

quotient(other)

productKind

productKind(mass, acceleration)

quotientKind

quotientKind(length, time)

productSymbol

productSymbol(force, _, _)

quotientSymbol

quotientSymbol(speed, left, _)