Unit conversion
for Fun and Profit
1
Public Unlimited Release
Why is this hard?
What do the following mean?
2
2
8 min | 8 minutes? 8 milli-inches? |
8'7" | 8 minutes + 7 seconds? 8 feet + 7 inches? |
8'7" | 8 arc minutes + 7 arc seconds? |
1 oz | Is that a troy ounce? An avoirdupois ounce? |
12 fl oz | Is a fluid ounce volume, mass, or force? |
Why is this hard?
What potential issues can you spot?
3
3
32 °F | Some temperature scales have offsets. |
0.001 in/°F | Is this an absolute or relative value? |
1 mol H2O | Is this convertible to 1 mol H2 + 0.5 mol O2? |
| |
| |
Wall of Fail: Why we are rolling our own library
4
4
Mission statement
5
5
What are units?
A dimension is a characteristic that can be quantified. Examples: length (L), mass (M), time (T).
A unit is a set of scales that lie along one or more dimensions. Examples: meter, Newton, Joule, Watt.
A measurement is a value that quantifies a characteristic on a particular scale (i.e., a unit plus a number).�Examples: 5 m, 12 in, 13.7 N
6
6
What are units?
A prefix is a numeric base and exponent used to adjust a unit's scale for convenience.�Examples: milli- = 10-3, kilo- = 10+3, kibi- = 2+10.
A conversion is an invertible transform that can be applied to a measurement.�Examples: 1 ft = 12 in, 1 N = 1 kg m s-2, 1 N = 1 J / m
A unit system is a set of prefixes, dimensions, and units.
7
7
What are units?
Conversions never change the dimension or character of something being measured; they change the scale(s) used to quantify the characteristic.
Thus, we will never attempt to convert a measurement to units with a different underlying dimension.
8
8
Why is this hard? part deux!
Testing whether units can be converted is easy... but how do we compute conversions?
We apply transforms to scales, but how do we choose which transforms to apply?
To convert lbf to N, we may need to apply:
9
9
lbf → lbm ft/s2 | lbm → slug |
slug → kg | ft → m |
kg m / s2 → N | |
Why is this hard? part deux!
A conversion is not an arc between nodes in a graph of units; it is a family of arcs (i.e., it can apply to any unit that contains its source or target).
We can encode all units U and all conversions C into a UxC matrix T, choose a right-hand side u to generate our desired conversion, and solve
Tc = u
in a least squares sense to find conversions to apply.
10
10
Solving conversions
The matrix T has columns representing conversions and rows representing units; each entry is an exponent of a unit the conversion transforms.
Tc = u
The right hand side, u, has
11
11
Solving conversions
12
12
|
|
|
|
|
|
g | |
sl | |
lbm | |
m | -1 |
in | 1 |
ft | |
s | 1 |
min | |
hr | -1 |
T
c
u
=
g | -1 | | | | | |
sl | 1 | -1 | | | | |
lbm | | 1 | | | | |
m | | | -1 | | | |
in | | | 1 | -1 | | |
ft | | | | 1 | | |
s | | | | | -1 | |
min | | | | | 1 | -1 |
hr | | | | | | 1 |
Solving conversions
13
13
|
|
1 |
|
-1 |
-1 |
g | |
sl | |
lbm | |
m | -1 |
in | 1 |
ft | |
s | 1 |
min | |
hr | -1 |
T
c
u
=
g | -1 | | | | | |
sl | 1 | -1 | | | | |
lbm | | 1 | | | | |
m | | | -1 | | | |
in | | | 1 | -1 | | |
ft | | | | 1 | | |
s | | | | | -1 | |
min | | | | | 1 | -1 |
hr | | | | | | 1 |
What does the units library do?
The units library provides
14
14
How does the units library work?
A unit system is deserialized from JSON using nlohmann_json.
Strings are parsed via a simple PEGTL grammar and dimensions, units, or measurements are created.
Prefixes, dimensions, and units are all implemented using a common base class that holds a name, an optional symbol, optional name/symbol aliases, and an optional description.
15
15