Next: , Up: Unit Expressions


5.1 Operators

You can enter more complicated units by combining units with operations such as powers, multiplication, division, 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.

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. An exponent like ‘2^3^2’ is evaluated right to left as usual. The ‘^’ operator has the second highest precedence. You can also use ‘**’ as an exponent operator.

     You have: cm^3
     You want: gallons
             * 0.00026417205
             / 3785.4118
     
     You have: arabicfoot * arabictradepound * force
     You want: ft lbf
             * 0.7296
             / 1.370614

You multiply units using a space or an asterisk (‘*’). The example above shows both forms. You can divide units using the slash (‘/’) or with ‘per’.

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

When a unit includes a prefix, exponent operators apply to the combination, so ‘centimeter^3’ gives cubic centimeters. If you separate the prefix from the unit with any multiplication operator, such as ‘centi meter^3’, then the prefix is treated as a separate unit, so the exponent does not apply. The second example would be a hundredth of a cubic meter, not a centimeter.

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: ‘W / m^2 Hz’. If you multiply with ‘*’ then you must group the terms in the denominator with parentheses: ‘W / (m^2 * Hz)’.

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 ‘*’ operator is convenient for multiplying a sequence of quotients. With the ‘*’ operator, the example above becomes ‘m/s * s/day’, which is equivalent to ‘m/day’. Similarly, you could write ‘1/2 * meter’ to get half a meter. Alternatively, parentheses can be used for grouping: you could write ‘(1/2) meter’ to get half a meter. See Complicated Unit Expressions, for an illustration of the various options.

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’. This operator has the highest precedence, so you can write the square root of two thirds ‘2|3^1|2’. You cannot use the vertical bar to indicate division of non-numerical units (e.g., ‘m|s’ results in an error message).

     You have: 1|2 inch
     You want: cm
             * 1.27
             / 0.78740157

You can use parentheses for grouping:

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