### 5.1 Operators ¶

You can enter more complicated units by combining units with operations such as multiplication, division, powers, addition, subtraction, and parentheses for grouping. You can use the customary symbols for these operators when `units` is invoked with its default options. Additionally, `units` supports some extensions, including high priority multiplication using a space, and a high priority numerical division operator (‘`|`’) that can simplify some expressions.

You multiply units using a space or an asterisk (‘`*`’). The next example shows both forms:

    You have: arabicfoot * arabictradepound * force
    You want: ft lbf
            * 0.7296
            / 1.370614

You can divide units using the slash (‘`/`’) or with ‘`per`’:

    You have: furlongs per fortnight
    You want: m/s
            * 0.00016630986
            / 6012.8727

You can use parentheses for grouping:

    You have: (1/2) kg / (kg/meter)
    You want: league
            * 0.00010356166
            / 9656.0833

White space surrounding operators is optional, so the previous example could have used ‘`(1/2)kg/(kg/meter)`’. As a consequence, however, hyphenated spelled-out numbers (e.g., ‘`forty-two`’) cannot be used; ‘`forty-two`’ is interpreted as ‘`40 - 2`’.

Multiplication using a space has a higher precedence than division using a slash and is evaluated left to right; in effect, the first ‘`/`’ character marks the beginning of the denominator of a unit expression. This makes it simple to enter a quotient with several terms in the denominator: ‘`J / mol K`’. The ‘`*`’ and ‘`/`’ operators have the same precedence, and are evaluated left to right; if you multiply with ‘`*`’, you must group the terms in the denominator with parentheses: ‘`J / (mol * K)`’.

The higher precedence of the space operator may not always be advantageous. For example, ‘`m/s s/day`’ is equivalent to ‘`m / s s day`’ and has dimensions of length per time cubed. Similarly, ‘`1/2 meter`’ refers to a unit of reciprocal length equivalent to 0.5/meter, perhaps not what you would intend if you entered that expression. The get a half meter you would need to use parentheses: ‘`(1/2) meter`’. The ‘`*`’ operator is convenient for multiplying a sequence of quotients. For example, ‘`m/s * s/day`’ is equivalent to ‘`m/day`’. Similarly, you could write ‘`1/2 * meter`’ to get half a meter.

The `units` program supports another option for numerical fractions: you can indicate division of numbers with the vertical bar (‘`|`’), so if you wanted half a meter you could write ‘`1|2 meter`’. You cannot use the vertical bar to indicate division of non-numerical units (e.g., ‘`m|s`’ results in an error message).

Powers of units can be specified using the ‘`^`’ character, as shown in the following example, or by simple concatenation of a unit and its exponent: ‘`cm3`’ is equivalent to ‘`cm^3`’; if the exponent is more than one digit, the ‘`^`’ is required. You can also use ‘`**`’ as an exponent operator.

    You have: cm^3
    You want: gallons
            * 0.00026417205
            / 3785.4118

Concatenation only works with a single unit name: if you write ‘`(m/s)2`’, `units` will treat it as multiplication by 2. When a unit includes a prefix, exponent operators apply to the combination, so ‘`centimeter3`’ gives cubic centimeters. If you separate the prefix from the unit with any multiplication operator (e.g., ‘`centi meter^3`’), the prefix is treated as a separate unit, so the exponent applies only to the unit without the prefix. The second example is equivalent to ‘`centi * (meter^3)`’, and gives a hundredth of a cubic meter, not a cubic centimeter. The `units` program is limited internally to products of 99 units; accordingly, expressions like ‘`meter^100`’ or ‘`joule^34`’ (represented internally as ‘`kg^34 m^68 / s^68`’) will fail.

The ‘`|`’ operator has the highest precedence, so you can write the square root of two thirds as ‘`2|3^1|2`’. The ‘`^`’ operator has the second highest precedence, and is evaluated right to left, as usual:

    You have: 5 * 2^3^2
    You want:
            Definition: 2560

With a dimensionless base unit, any dimensionless exponent is meaningful (e.g., ‘`pi^exp(2.371)`’). Even though angle is sometimes treated as dimensionless, exponents cannot have dimensions of angle:

    You have: 2^radian
                     ^
    Exponent not dimensionless

If the base unit is not dimensionless, the exponent must be a rational number p/q, and the dimension of the unit must be a power of q, so ‘`gallon^2|3`’ works but ‘`acre^2|3`’ fails. An exponent using the slash (‘`/`’) operator (e.g., ‘`gallon^(2/3)`’) is also acceptable; the parentheses are needed because the precedence of ‘`^`’ is higher than that of ‘`/`’. Since `units` cannot represent dimensions with exponents greater than 99, a fully reduced exponent must have q < 100. When raising a non-dimensionless unit to a power, `units` attempts to convert a decimal exponent to a rational number with q < 100. If this is not possible `units` displays an error message:

    You have: ft^1.234
    Base unit not dimensionless; rational exponent required

A decimal exponent must match its rational representation to machine precision, so ‘`acre^1.5`’ works but ‘`gallon^0.666`’ does not.