### 5.2 Sums and Differences of Units ¶

You may sometimes want to add values of different units that are outside the SI. You may also wish to use `units` as a calculator that keeps track of units. Sums of conformable units are written with the ‘`+`’ character, and differences with the ‘`-`’ character.

    You have: 2 hours + 23 minutes + 32 seconds
    You want: seconds
            * 8612
            / 0.00011611705

    You have: 12 ft + 3 in
    You want: cm
            * 373.38
            / 0.0026782366

    You have: 2 btu + 450 ft lbf
    You want: btu
            * 2.5782804
            / 0.38785542

The expressions that are added or subtracted must reduce to identical expressions in primitive units, or an error message will be displayed:

    You have: 12 printerspoint - 4 heredium
                                          ^
    Invalid sum of non-conformable units

If you add two values of vastly different scale you may exceed the available precision of floating point (about 15 digits). The effect is that the addition of the smaller value makes no change to the larger value; in other words, the smaller value is treated as if it were zero.

    You have: lightyear + cm

No warning is given, however. As usual, the precedence for ‘`+`’ and ‘`-`’ is lower than that of the other operators. A fractional quantity such as 2 1/2 cups can be given as ‘`(2+1|2) cups`’; the parentheses are necessary because multiplication has higher precedence than addition. If you omit the parentheses, `units` attempts to add ‘`2`’ and ‘`1|2 cups`’, and you get an error message:

    You have: 2+1|2 cups
                       ^
    Invalid sum or difference of non-conformable units

The expression could also be correctly written as ‘`(2+1/2) cups`’. If you write ‘`2 1|2 cups`’ the space is interpreted as multiplication so the result is the same as ‘`1 cup`’.

The ‘`+`’ and ‘`-`’ characters sometimes appears in exponents like ‘`3.43e+8`’. This leads to an ambiguity in an expression like ‘`3e+2 yC`’. The unit ‘`e`’ is a small unit of charge, so this can be regarded as equivalent to ‘`(3e+2) yC`’ or ‘`(3 e)+(2 yC)`’. This ambiguity is resolved by always interpreting ‘`+`’ and ‘`-`’ as part of an exponent if possible.