Units: A Unit Conversion Program and Scientific Calculator: Defining New Units

Next: Defining Nonlinear Units, Previous: Units Data Files, Up: Defining Your Own Units [Contents][Index]

### 10.2 Defining New Units and Prefixes

A unit is specified on a single line by giving its name and an equivalence. Comments start with a ‘`#`’ character, which can appear anywhere in a line. The backslash character (‘`\`’) acts as a continuation character if it appears as the last character on a line, making it possible to spread definitions out over several lines if desired. A file can be included by giving the command ‘`!include`’ followed by the file’s name. The ‘`!`’ must be the first character on the line. The file will be sought in the same directory as the parent file unless you give a full path. The name of the file to be included cannot contain the comment character ‘`#`’.

Unit names must not contain any of the operator characters ‘`+`’, ‘`-`’, ‘`*`’, ‘`/`’, ‘`|`’, ‘`^`’, ‘`;`’, ‘`~`’, the comment character ‘`#`’, or parentheses. They cannot begin or end with an underscore (‘`_`’), a comma (‘`,`’) or a decimal point (‘`.`’). The figure dash (U+2012), typographical minus (‘-’; U+2212), and en dash (‘–’; U+2013) are converted to the operator ‘`-`’, so none of these characters can appear in unit names. Names cannot begin with a digit, and if a name ends in a digit other than zero, the digit must be preceded by a string beginning with an underscore, and afterwards consisting only of digits, decimal points, or commas. For example, ‘`foo_2`’, ‘`foo_2,1`’, or ‘`foo_3.14`’ are valid names but ‘`foo2`’ or ‘`foo_a2`’ are invalid. You could define nitrous oxide as

    N2O     nitrogen 2  + oxygen

but would need to define nitrogen dioxide as

    NO_2    nitrogen + oxygen 2

Be careful to define new units in terms of old ones so that a reduction leads to the primitive units, which are marked with ‘`!`’ characters. Dimensionless units are indicated by using the string ‘`!dimensionless`’ for the unit definition.

When adding new units, be sure to use the `-c` option to check that the new units reduce properly. If you create a loop in the units definitions, then `units` will hang when invoked with the `-c` option. You will need to use the `--check-verbose` option, which prints out each unit as it is checked. The program will still hang, but the last unit printed will be the unit that caused the infinite loop.

If you define any units that contain ‘`+`’ characters, carefully check them because the `-c` option will not catch non-conformable sums. Be careful with the ‘`-`’ operator as well. When used as a binary operator, the ‘`-`’ character can perform addition or multiplication depending on the options used to invoke `units`. To ensure consistent behavior use ‘`-`’ only as a unary negation operator when writing units definitions. To multiply two units leave a space or use the ‘`*`’ operator with care, recalling that it has two possible precedence values and may require parentheses to ensure consistent behavior. To compute the difference of ‘`foo`’ and ‘`bar`’ write ‘`foo+(-bar)`’ or even ‘`foo+-bar`’.

Here is an example of a short data file that defines some basic units:

    m       !               # The meter is a primitive unit
    sec     !               # The second is a primitive unit
    rad     !dimensionless  # A dimensionless primitive unit
    micro-  1e-6            # Define a prefix
    minute  60 sec          # A minute is 60 seconds
    hour    60 min          # An hour is 60 minutes
    inch    0.0254 m        # Inch defined in terms of meters
    ft      12 inches       # The foot defined in terms of inches
    mile    5280 ft         # And the mile

A unit that ends with a ‘`-`’ character is a prefix. If a prefix definition contains any ‘`/`’ characters, be sure they are protected by parentheses. If you define ‘`half- 1/2`’ then ‘`halfmeter`’ would be equivalent to ‘`1 / (2 meter)`’.

Next: Defining Nonlinear Units, Previous: Units Data Files, Up: Defining Your Own Units [Contents][Index]