www.delorie.com/gnu/docs/units/units_7.html   search  
Buy GNU books!

Units: A Unit Conversion Program

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7. Defining new units

All of the units and prefixes that units can convert are defined in the units data file. If you want to add your own units, you can supply your own file.

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.

Unit names must not contain any of the operator characters `+', `-', `*', `/', `|', `^' or the parentheses. They cannot begin with a digit or a decimal point (`.'), nor can they end with a digit (except for zero). 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. When adding new units, be sure to use the `-c' option to check that the new units reduce properly. If you define any units which contain `+' characters, carefully check them because the `-c' option will not catch non-conformable sums. If you create a loop in the units definitions, then units will hang when invoked with the `-c' options. You will need to use the `--check-verbose' option which prints out each unit as it checks them. The program will still hang, but the last unit printed will be the unit which caused the infinite loop.

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

m       !               # The meter is a primitive unit
sec     !               # The second is a 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 which 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'.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003