Skip to main content

Units

The units library holds quantities (a value with a physical unit) and the functions that convert, compare and inspect units. This introduction gives the concepts you need before you read the entries.

Writing a unit​

A unit is a name such as m, km or h, or a product, quotient or power of names. Each unit operand of quantity, unitConvert, isCompatibleUnit and unitDimension also accepts a string, which is read as a unit: "km/h", "m/s^2", "kg*m".

unitConvert(quantity(36, "km/h"), "m/s")
// ➔ 10 m/s

Write a unit as a string when its name is also the name of a function, such as min (minute) or lb (pound): a bare min is the function min, not the unit.

unitConvert(quantity(2, h), "min")
// ➔ 120 min

Supported units​

A unit is a unit symbol, or a product, quotient or power of unit symbols. In Epsil, write a unit as a name or as a string: quantity(3, m), quantity(9.8, m/s^2) and quantity(9.8, "m/s^2") are quantities. A unit name is not a variable: it is not declared, and it does not need a value.

The unit arguments of unitConvert, isCompatibleUnit and unitDimension are names or strings too. Four unit symbols cannot be written as a name: in is a reserved word, and min, lb and arcsec are the names of library functions. Write them as a string:

unitConvert(quantity(2, "h"), "min")
// ➔ 120 min
[isCompatibleUnit("min", s), unitConvert(quantity(1, "lb"), oz)]
// ➔ [true,16 oz]
quantity(1, lb)
// ➔ 1 Lb

The vector of unitDimension has eight exponents: the seven SI base dimensions, in the order of the table below, and a currency dimension.

SI base units​

SymbolQuantityName
mLengthmeter
kgMasskilogram
sTimesecond
AElectric currentampere
KTemperaturekelvin
molAmount of substancemole
cdLuminous intensitycandela

Named derived SI units​

SymbolQuantityNameIn base units
HzFrequencyhertzs^-1
NForcenewtonkg m s^-2
PaPressurepascalkg m^-1 s^-2
JEnergyjoulekg m^2 s^-2
WPowerwattkg m^2 s^-3
CElectric chargecoulombA s
VVoltagevoltkg m^2 s^-3 A^-1
FCapacitancefaradkg^-1 m^-2 s^4 A^2
ohmResistanceohmkg m^2 s^-3 A^-2
SConductancesiemenskg^-1 m^-2 s^3 A^2
WbMagnetic fluxweberkg m^2 s^-2 A^-1
TMagnetic flux densityteslakg s^-2 A^-1
HInductancehenrykg m^2 s^-2 A^-2
lmLuminous fluxlumencd
lxIlluminanceluxcd m^-2
BqRadioactivitybecquerels^-1
GyAbsorbed dosegraym^2 s^-2
SvEquivalent dosesievertm^2 s^-2
katCatalytic activitykatalmol s^-1

Ω is the same unit as ohm.

SI prefixes​

An SI prefix applies to the SI base units, to the named derived SI units, and to eV, L and bar:

PrefixSymbolFactor
quettaQ10^30
ronnaR10^27
yottaY10^24
zettaZ10^21
exaE10^18
petaP10^15
teraT10^12
gigaG10^9
megaM10^6
kilok10^3
hectoh10^2
decada10^1
PrefixSymbolFactor
decid10^-1
centic10^-2
millim10^-3
microµ, μ10^-6
nanon10^-9
picop10^-12
femtof10^-15
attoa10^-18
zeptoz10^-21
yoctoy10^-24
rontor10^-27
quectoq10^-30

The prefix of mass is put on g, not on kg: mg, µg. The micro prefix is the micro sign µ or the Greek letter μ; u is not a prefix, and ug is not a known unit, so it converts to nothing.

unitConvert(quantity(2, µg), mg)
// ➔ 0.002 mg
unitConvert(quantity(1, keV), eV)
// ➔ 1000 eV
isError(unitConvert(quantity(2, ug), g))
// ➔ true

Non-SI units​

SymbolQuantitySI equivalent
minTime60 s
hTime3600 s
dTime86400 s
wkTime604800 s
haArea10^4 m^2
LVolume10^-3 m^3
tMass10^3 kg
eVEnergy1.602176634e-19 J
DaMass1.6605390666e-27 kg
auLength1.495978707e11 m
ÅLength10^-10 m
inLength0.0254 m
ftLength0.3048 m
ydLength0.9144 m
miLength1609.344 m
lbMass0.45359237 kg
ozMass0.028349523125 kg
galVolume3.785411784e-3 m^3
qtVolume9.46352946e-4 m^3
ptVolume4.73176473e-4 m^3
cupVolume2.365882365e-4 m^3
atmPressure101325 Pa
barPressure10^5 Pa
calEnergy4.184 J
kWhEnergy3.6e6 J
degCTemperatureK - 273.15
degFTemperature(K - 459.67) x 5/9
USDCurrency(currency dimension)
centCurrency0.01 USD

gal, qt, pt and cup are the US liquid measures. USD and cent are the only units of the currency dimension.

degC and degF convert with an offset and a scale, not with a scale alone. unitConvert applies both:

[unitConvert(quantity(100, degC), K), unitConvert(quantity(212, degF), degC)]
// ➔ [373.15 K,100 degC]

Angular units​

SymbolNameIn radians
radradian1
degdegreepi/180
gradgradianpi/200
turnturn2 pi
arcminarcminutepi/10800
arcsecarcsecondpi/648000

Angular units are dimensionless. The trigonometric functions (sin, cos, tan, …) convert a quantity with an angular unit to radians:

[sin(quantity(30, deg)), cos(quantity(100, grad)), cos(quantity(1, turn))]
// ➔ [1/2,0,1]

Dimensionless units​

SymbolScale factor
percent0.01
ppm10^-6
Np1
dBln(10)/20 Np
unitConvert(quantity(25, percent), ppm)
// ➔ 250000 ppm

The logarithmic units dB and Np convert to each other: 1 dB is ln(10)/20 Np, about 0.1151 Np. A level does not convert to a linear unit such as percent:

[unitConvert(quantity(10, dB), Np), unitConvert(quantity(20, dB), Np)]
// ➔ [1.151292546497023 Np,2.302585092994046 Np]
unitConvert(quantity(10, dB), percent)
// ➔ error: Runtime error: expected `percent`, got `dB`

Physics constants​

The physics library defines physical constants as quantities, with the 2018 CODATA recommended values. They take part in quantity arithmetic like any other quantity.

SymbolValueUnitDescription
speedOfLight299792458m/sSpeed of light in vacuum
planckConstant6.62607015e-34J sPlanck constant
mu01.25663706212e-6N/A^2Vacuum permeability
standardGravity9.80665m/s^2Standard acceleration due to gravity
elementaryCharge1.602176634e-19CElementary charge
boltzmannConstant1.380649e-23J/KBoltzmann constant
avogadroConstant6.02214076e23mol^-1Avogadro constant
vacuumPermittivity8.8541878128e-12F/mVacuum permittivity
gravitationalConstant6.67430e-11m^3/(kg s^2)Gravitational constant
stefanBoltzmannConstant5.670374419e-8W/(m^2 K^4)Stefan-Boltzmann constant
gasConstant8.314462618J/(mol K)Molar gas constant
unitSimplify(quantity(2, kg) * standardGravity)
// ➔ 19.6133 N
unitConvert(speedOfLight, km/s)
// ➔ 299792.458 km/s

In LaTeX, \mu_0 is mu0 and \varepsilon_0 is vacuumPermittivity:

$\varepsilon_0$
// ➔ 8.8541878128e-12 F/m

LaTeX parsing​

A quantity written in LaTeX, in a $…$ island of an Epsil program or in a mathfield, is read as a quantity.

Supported input forms​

LaTeXEpsil
5\,\mathrm{m}quantity(5, m)
3\,\text{kg}quantity(3, kg)
9.8\,\mathrm{m/s^{2}}quantity(9.8, m/s^2)
\qty{12}{cm}quantity(12, cm)
\SI{5}{kg}quantity(5, kg)
\unit{m/s}the unit m/s
\si{MHz}the unit MHz
$12\,\mathrm{cm} + 1\,\mathrm{m}$
// ➔ 1.12 m
unitConvert($30\,\mathrm{km/h}$, $\mathrm{m/s}$)
// ➔ 8.333333333333334 m/s

An angle written with the degree, minute and second signs, 9°30'15", is an angle in degrees, not a quantity. Its value is a number of radians:

$9°30'15"$
// ➔ 2281/43200 * pi

Unit parsing rules​

In \mathrm{…} and \text{…}:

  • / is a division;
  • ^ and ^{…} are exponents;
  • \cdot is a multiplication;
  • a run of letters is one unit symbol, which can have a prefix (km, MHz).

A space between two unit symbols is not a multiplication: \mathrm{kg m} is the unknown unit kgm. Write \mathrm{kg\cdot m}.

$5\,\mathrm{kg\cdot m}$
// ➔ 5 kg⋅m

Definitions​

Each definition is listed under its Epsil spelling (the MathJSON name when it has none), with its signature in the engine's type syntax. The Standard Library page is the one-page index of every category.

isCompatibleUnit​

(value, value) -> value

  • argument 1 — A unit, a unit name or a string such as "km/h". Sample: "km".
  • argument 2 — A unit, a unit name or a string such as "km/h". Sample: "mi".

Check if two units have the same dimension and convert into each other (dB and Np do, dB and percent do not)

isCompatibleUnit(km, mi)
// ➔ true
isCompatibleUnit("km", "s")
// ➔ false

Two units are compatible when they have the same dimension vector (see unitDimension). The units are unit symbols or compound units, written as names or as strings.

[isCompatibleUnit(m, km), isCompatibleUnit(m, s), isCompatibleUnit(J, eV)]
// ➔ [true,false,true]
isCompatibleUnit(m/s, km/h)
// ➔ true
MathJSON: IsCompatibleUnit

["IsCompatibleUnit", unit1, unit2]. The units are not evaluated.

["IsCompatibleUnit", "J", "eV"]
// ➔ "True"
["IsCompatibleUnit", ["Divide", "m", "s"], ["Divide", "km", "h"]]
// ➔ "True"

quantity​

(magnitude: value, unit: value) -> quantity

  • magnitude — The numeric value, in the unit that follows. Sample: 3.
  • unit — The unit, a unit name or a string such as "km/h"; a compound unit whose factors cancel gives a plain number. Sample: "m".

A value paired with a physical unit

quantity(3, m) + quantity(50, cm)
// ➔ 3.5 m
quantity(30, "km/h")
// ➔ 30 km/h

The unit is a unit symbol or a compound unit: a product, quotient or power of unit symbols (see Supported units). A compound unit written as a string is read as a unit expression.

quantity(1, "kg/(m*s^2)")
// ➔ 1 kg/(m⋅s^2)

Arithmetic on quantities​

A sum or a difference of quantities of the same dimension is expressed in the unit with the largest scale factor. A sum or a difference of quantities of different dimensions is an incompatible-type error.

quantity(12, cm) + quantity(1, m)
// ➔ 1.12 m
quantity(1, km) - quantity(1, m)
// ➔ 0.999 km
quantity(5, m) + quantity(3, s)
// ➔ error: Runtime error: expected `m`, got `s`

A product or a quotient combines the units. A unit symbol that appears in both cancels exactly, with no conversion factor, and a quotient whose units all cancel is a number:

quantity(5, m) * quantity(3, s)
// ➔ 15 m⋅s
quantity(18, "in") / quantity(12, "in/ft")
// ➔ 3/2 ft
quantity(6, m) / quantity(2, m)
// ➔ 3

A power raises the unit to the exponent, and a number scales the magnitude:

[quantity(3, m)^2, 2 * quantity(3, m), sqrt(quantity(9, m^2))]
// ➔ [9 m^2,6 m,3 m]

Quantities of the same dimension compare by their values in a common unit:

[quantity(100, cm) == quantity(1, m), quantity(10, m) > quantity(5, ft)]
// ➔ [true,true]
MathJSON: Quantity

["Quantity", magnitude, unit]. The magnitude is any numeric expression. The unit is a unit symbol ("m") or a compound unit built with Multiply, Divide and Power.

["Quantity", 3.5, "m"]
["Quantity", 9.8, ["Divide", "m", ["Power", "s", 2]]]
["Quantity", 100, "km"]

A unit written in the string notation is canonicalized to the structured form:

["Quantity", 9.8, "m/s^2"]
// ➔ ["Quantity", 9.8, ["Divide", "m", ["Power", "s", 2]]]
["Quantity", 1, "kg/(m*s^2)"]
// ➔ ["Quantity", 1, ["Divide", "kg", ["Multiply", "m", ["Power", "s", 2]]]]

Add, Subtract, Multiply, Divide and Power apply to quantities:

["Add", ["Quantity", 12, "cm"], ["Quantity", 1, "m"]]
// ➔ ["Quantity", 1.12, "m"]
["Multiply", ["Quantity", 5, "m"], ["Quantity", 3, "s"]]
// ➔ ["Quantity", 15, ["Multiply", "m", "s"]]
["Divide", ["Quantity", 18, "in"], ["Quantity", 12, ["Divide", "in", "ft"]]]
// ➔ ["Quantity", ["Rational", 3, 2], "ft"]
["Power", ["Quantity", 3, "m"], 2]
// ➔ ["Quantity", 9, ["Power", "m", 2]]

In LaTeX, a number followed by a unit in \mathrm{…} or \text{…}, or the \qty and \SI commands of siunitx: 9.8\,\mathrm{m/s^{2}}. See LaTeX parsing.

quantityMagnitude​

(value) -> value

  • argument 1 — The quantity; a value that is not a quantity leaves the call unevaluated. Sample: quantity(3, m).

Extract the numeric value from a quantity

quantityMagnitude(quantity(3, m))
// ➔ 3
[quantityMagnitude(quantity(3.5, m)), quantityMagnitude(speedOfLight)]
// ➔ [3.5,299792458]
MathJSON: QuantityMagnitude

["QuantityMagnitude", quantity].

["QuantityMagnitude", ["Quantity", 3.5, "m"]]
// ➔ 3.5

quantityUnit​

(value) -> value

  • argument 1 — The quantity; a value that is not a quantity leaves the call unevaluated. Sample: quantity(3, m).

Extract the unit from a quantity

quantityUnit(quantity(3, m))
// ➔ m

The unit is a unit symbol or a compound unit expression.

quantityUnit(quantity(3.5, m))
// ➔ m
quantityUnit(quantity(9.8, m/s^2))
// ➔ m / s^2
MathJSON: QuantityUnit

["QuantityUnit", quantity].

["QuantityUnit", ["Quantity", 3.5, "m"]]
// ➔ "m"
["QuantityUnit", ["Quantity", 9.8, ["Divide", "m", ["Power", "s", 2]]]]
// ➔ ["Divide", "m", ["Power", "s", 2]]

unitConvert​

(quantity: value, unit: value) -> value

  • quantity — The quantity to convert. Sample: quantity(5, km).
  • unit — The target unit, a unit name or a string such as "km/h"; a unit of another dimension is an error. Sample: "m".

Convert a quantity to a different compatible unit

unitConvert(quantity(5, km), m)
// ➔ 5000 m
unitConvert(quantity(1, h), "min")
// ➔ 60 min

The target unit is a unit symbol or a compound unit, written as a name or as a string (see Supported units). A conversion to a unit of another dimension is an incompatible-type error. The temperature units degC, degF and K convert with an offset.

unitConvert(quantity(1500, m), km)
// ➔ 1.5 km
unitConvert(quantity(36, km/h), m/s)
// ➔ 10 m/s
unitConvert(quantity(100, degC), degF)
// ➔ 212 degF
isError(unitConvert(quantity(5, m), s))
// ➔ true

An exact magnitude stays exact when the conversion multiplies it by an integer. Otherwise the magnitude of the result is a float. A unit with an exact scale converts by its exact factor, so 12 in is exactly 1 ft. A complex magnitude keeps its imaginary part:

unitConvert(quantity(pi, m), cm)
// ➔ 100pi cm
unitConvert(quantity(180, deg), rad)
// ➔ 3.141592653589793 rad
[unitConvert(quantity(12, "in"), ft), unitConvert(quantity(3 + 4i, m), cm)]
// ➔ [1 ft,(300 + 400i) cm]
MathJSON: UnitConvert

["UnitConvert", quantity, unit]. The target unit is not evaluated.

["UnitConvert", ["Quantity", 1500, "m"], "km"]
// ➔ ["Quantity", 1.5, "km"]
["UnitConvert", ["Quantity", 36, ["Divide", "km", "h"]], ["Divide", "m", "s"]]
// ➔ ["Quantity", 10, ["Divide", "m", "s"]]
["UnitConvert", ["Quantity", 5, "m"], "s"]
// ➔ ["Error", "'incompatible-type'"]

unitDimension​

(value) -> value

  • argument 1 — The unit, a unit name or a string such as "km/h"; the result lists its exponents of length, mass, time, current, temperature, amount, luminous intensity and currency. Sample: "N".

Return the dimension vector of a unit

unitDimension(N)
// ➔ [1,1,-2,0,0,0,0,0]
unitDimension("km/h")
// ➔ [1,0,-1,0,0,0,0,0]

The dimension vector has eight exponents: [length, mass, time, current, temperature, amount, luminosity, currency]. The first seven are the SI base dimensions. The unit is a unit symbol or a compound unit, written as a name or as a string.

[unitDimension(m), unitDimension(N), unitDimension(V)]
// ➔ [[1,0,0,0,0,0,0,0],[1,1,-2,0,0,0,0,0],[2,1,-3,-1,0,0,0,0]]
[unitDimension(m/s^2), unitDimension(rad), unitDimension(USD)]
// ➔ [[1,0,-2,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,1]]
MathJSON: UnitDimension

["UnitDimension", unit]. The unit is not evaluated.

["UnitDimension", "V"]
// ➔ ["List", 2, 1, -3, -1, 0, 0, 0, 0]

unitSimplify​

(value) -> value

  • argument 1 — The quantity; it is returned unchanged when no named unit has its dimension, or when it is not a quantity. Sample: quantity(1, kg * m / s^2).

Simplify a quantity unit to a named derived unit if possible

unitSimplify(quantity(1, kg * m / s^2))
// ➔ 1 N

The result is a named derived SI unit when one has the dimension of the unit and the scale factor 1. When there is none, the quantity is returned unchanged.

unitSimplify(quantity(100, kg*m/s^2))
// ➔ 100 N
unitSimplify(quantity(5, kg*m^2/s^2))
// ➔ 5 J
unitSimplify(quantity(3, m))
// ➔ 3 m
MathJSON: UnitSimplify

["UnitSimplify", quantity].

["UnitSimplify", ["Quantity", 100, ["Multiply", "kg", "m", ["Power", "s", -2]]]]
// ➔ ["Quantity", 100, "N"]