In this post, we will take a quick look inside Calcpad and see how units actually work. But first, let’s make a brief overview of the theoretical basis.

## Theoretical basis

There are only seven base units in the whole universe. They correspond to the seven physical dimensions:

- mass – kilogram (kg)
- length – meter (m)
- time – second (s)
- electric current – ampere (A)
- temperature – Kelvin (K)
- amount of substance – mole (mol)
- luminous intensity – candela (cd)

All other units are derived from the base ones, using the respective laws of physics. For example, forces are measured in Newtons (N). By definition, force is mass × acceleration, so 1 N = 1 kg·m/s^{2}. Such units are called “composite”. They can be always decomposed to their constituent base units.

Other method to derive units is by adding prefixes. I this way, we can obtain multiples of both base and composite units. For example, kilonewtons – kN = 10^{3}·N, meganewtons – MN = kN = 10^{6}·N and so on. All possible prefixes are listed in the following table:

Name |
deca |
hecto |
kilo |
mega |
giga |
tera |
peta |
exa |
zetta |
yotta |

Symbol |
da |
h |
k |
M |
G |
T |
P |
E |
Z |
Y |

Factor |
10^{1} |
10^{2} |
10^{3} |
10^{6} |
10^{9} |
10^{12} |
10^{15} |
10^{18} |
10^{21} |
10^{24} |

Name |
deci |
centi |
milli |
micro |
nano |
pico |
femto |
atto |
zepto |
yocto |

Symbol |
d |
c |
m |
μ |
n |
p |
f |
a |
z |
y |

Factor |
10^{−1} |
10^{−2} |
10^{−3} |
10^{−6} |
10^{−9} |
10^{−12} |
10^{−15} |
10^{−18} |
10^{−21} |
10^{−24} |

However, not all prefixes are commonly used for all units, and that is why they are not included in Calcpad. Full list of the available units is provided in the user manual:

https://calcpad.eu/help/27/units

## Internal implementation

Any software implementation must allow the units to be compared, converted and scaled. For that purpose, we need to store the information for how units are composed by the base ones. We also need the corresponding scale factors, in order to allow for multiples. Then, we have to define all possible operations like comparison, addition, subtraction, multiplication, division and exponentiation.

To store all this information, we need a special type of data called “structure”. It can contain multiple values of different types in a single variable. In the case of units, we will include a name and two vectors. The first one will store the exponents for all seven physical dimensions (mass, length, time, current, temp, substance, luminosity) and the second one – the respective scaling factors:

`Unit = {name: "name", exponents: [p`_{1}, p_{2}, p_{3}, p_{4}, p_{5}, p_{6}, p_{7}], factors: [f_{1}, f_{2}, f_{3}, f_{4}, f_{5}, f_{6}, f_{7}]}

A base unit can include only one value of “1” for the respective dimension. All of the rest ones must be exactly zero “0”. Composite units can have several nonzero values for the corresponding dimensions. Some examples of unit definitions are given bellow:

**Base units**:

`Kilogram = {name: "kg", exponents: [1, 0, 0, 0, 0, 0, 0], factors: [10`^{3}, 1, 1, 1, 1, 1, 1]}

Meter = {name: "m", exponents: [0, 1, 0, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}

Second = {name: "s", exponents: [0, 0, 1, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}

**Composite units**:

`Newton = {name: "N", exponents: [1, 1, -2, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}`

Pascal = {name: "Pa", exponents: [1, -1, -2, 0, 0, 0, 0], factors: [1, 1, 1, 1, 1, 1, 1]}

**Multiples**:

`Ton = {name: "t", exponents: [1, 0, 0, 0, 0, 0, 0], factors: [10`^{6}, 1, 1, 1, 1, 1, 1]}

Millimeter = {name: "mm", exponents: [0, 1, 0, 0, 0, 0, 0], factors: [1, 10^{-3}, 1, 1, 1, 1, 1]}

Minute = {name: "min", exponents: [0, 0, 1, 0, 0, 0, 0], factors: [1, 1, 60, 1, 1, 1, 1]}

Kilonewton = {name: "kN", exponents: [1, 1, -2, 0, 0, 0, 0], factors: [10^{3}, 1, 1, 1, 1, 1, 1]}

Megapascal = {name: "MPa", exponents: [1, -1, -2, 0, 0, 0, 0], factors: [10^{6}, 1, 1, 1, 1, 1, 1]}

## Units consistency

You can compare, add or subtract only values, which units are consistent. For example, “kg + m” does not make any sense, because both operands represent different physical dimensions. The expression “m^{2} + m^{3}” does not make sense as well, because you are trying to add area to volume. However, “m + mm” is possible, because both represent length and we can be converted to each other. So, units are consistent only if they are of the same dimensions and with the same exponents. What we need to do inside the software, is to check if the exponents vectors match exactly.

## Comparison, addition and subtraction

Even if consistent, units are not necessarily equal, because they can have different scaling factors. So, we still cannot operate directly with the respective values. First, we have to convert all values to the same units. For example:

`3 dm + 2 cm = 3 dm + 0.2 dm = (3 + 0.2) dm = 3.2 dm`

Subtraction and comparison are performed in the same way as the addition. The following algorithm can be used:

- Check if the units are consistent (compare the exponent vectors). If not, report an error. Otherwise, continue to step 2.
- Compare the scale factors. If different, compute the conversion factor for the second unit and use it to multiply the second value.
- Perform the operation and return the result with the units of the first value.

## Multiplication, division and exponentiation

Unlike the previous ones, these operations do not require the units to be consistent. Actually, this are the ways we use to derive new composite units. In the software, these operations are performed by manipulation of the respective exponents as follows:

- Multiplication is performed by addition of the exponents:

`m·m`^{2} = m^{(1 + 2)} = m^{3}

- Division is equivalent to exponent subtraction:

`N/m`^{2} = kg·m·s^{-2}/m^{2} = kg·m^{(1 - 2)}·s^{-2} = kg·m^{-1}·s^{-2} = Pa

- Exponentiation is performed by multiplication of the old and new exponent:

`(m`^{2})^{3} = m^{2·3} = m^{6}

The following algorithm is applied:

- Check if there are different scale factors and compute the conversion factor.
- Perform the required operation (multiplication, division …) with the units and obtain the resulting units.
- Perform the operation with the values, apply the conversion factor and return the result in the calculated units.

## Target units

If you do not specify any target units, the result will be decomposed to the base units. For example:

`100N·10m = 1000 kg·m`^{2}/s^{2}

That is because the program cannot understand the physical meaning of your formulas. Sometimes, the result can be ambiguous and the program will not know what to do. For the above expression:

- If it is bending moment or torque, the result will be in Nm.
- If it is work or energy, the result will be in Joules.

In such cases, you have to specify the target units and if they are compatible, the result will be converted automatically. Always write the target units at the end of the expression, separated by a vertical bar “|”:

`500N·20m|kJ = 10kJ`

`2000N·5m|kNm = 10kNm`

Arithmetic operations with units cost extra computational time than simple numbers. However, Calcpad uses highly optimized arithmetic code that performs very efficiently and much faster than other similar software.