2011-01-21

Precedence in unit-parsing

Operators in mathematical expressions are conventionally parsed left to right in this order of precedence:
1. Parentheses
2. Factorial
3. Exponentiation
4. Multiplication and division
5. Addition and subtraction

But what about when a mathematical expression includes a quantity expression (i.e. the product of a number and a unit)? Here are some considerations, applied to the International System of Units (SI).

1. The binding of the prefix to the unit has the highest precedence This is expressed in the SI Brochure 3.1 as "The grouping formed by a prefix symbol attached to a unit symbol constitutes a new inseparable unit symbol (forming a multiple or submultiple of the unit concerned) that can be raised to a positive or negative power and that can be combined with other unit symbols to form compound unit symbols".

2. When a unit expresion has more than one divisor, how should it be expressed?
The SI Brochure 5.1 incorrectly states "In forming products and quotients of unit symbols the normal rules of algebraic multiplication or division apply...A solidus must not be used more than once in a given expression without brackets to remove ambiguities." This means that the previously commonly-written quantity expression for the acceleration due to Earth gravity (9.81 m/s/s) is not acceptable SI usage, although this meets the mathematical precedence rules and is not ambiguous.The SI uses the example:
"kg/(s3 A), or m kg s–3 A–1, but not m kg/s3/A, nor m kg/s3 A", even though the first three have the same meaning. Examples 3 and 4 may be ambiguous to some humans, but they are not mathematically ambiguous. The authors of the SI Brochure should stick to established mathematics, and not modify it to suit their view of human capacity.

3. A number rather than a prefix is used with a unit
The common metric unit of fuel consumption is 'L/100 km', in which the number '100' acts somewhat like a prefix. It is a dog of a unit, since
(a) the SI Brochure doesn't allow numbers in a unit expression (although it doesn't explicitly deprecate it)
(b) it evaluates left-to-right as 'L/100*km' which has dimension [length]4.
In SI style, it can't be written with a prefix, since 100 km is 105 m, and there is no prefix for 105. NIST writes it as 'L/(100 km)' - which parses correctly, but is still not a properly-formed SI unit expression.

This problem can be fixed with a new precedence rule:
The binding of the number to the unit has a precedence above multiplication/division

4. It is meaningless to factorialise a quantity expression.

5. Addition/subtraction operators are not used in quantity expressions.

So the precedence order for quantity expressions is:
1. Prefix-unit binding
2. Parentheses
3. Exponentiation
4. Number-unit binding
5. Multiplication and division

The Google search engine incorporates a basic calculator which is unit-aware and which abides by the order above: try googling '0.4 gal/mile in L/100 km', '5 km^2 * 0.4 L/ha' or '5 mph in m/10 s'. The Wolfram Alpha 'computational knowledge engine' , which does a pretty good job of parsing quantity expressions, chokes on '0.4 gal/mile in L/100 km', and even '0.4 gal/mile in L/(100 km)', so needs to have its precedence rules tweaked.

No comments: