Go to the first, previous, next, last section, table of contents.

Operating on Units

One special interpretation of algebraic formulas is as numbers with units. For example, the formula `5 m / s^2' can be read "five meters per second squared." The commands in this chapter help you manipulate units expressions in this form. Units-related commands begin with the u prefix key.

Basic Operations on Units

A units expression is a formula which is basically a number multiplied and/or divided by one or more unit names, which may optionally be raised to integer powers. Actually, the value part need not be a number; any product or quotient involving unit names is a units expression. Many of the units commands will also accept any formula, where the command applies to all units expressions which appear in the formula.

A unit name is a variable whose name appears in the unit table, or a variable whose name is a prefix character like `k' (for "kilo") or `u' (for "micro") followed by a name in the unit table. A substantial table of built-in units is provided with Calc; see section Predefined Units. You can also define your own unit names; see section User-Defined Units.

Note that if the value part of a units expression is exactly `1', it will be removed by the Calculator's automatic algebra routines: The formula `1 mm' is "simplified" to `mm'. This is only a display anomaly, however; `mm' will work just fine as a representation of one millimeter.

You may find that Algebraic Mode (see section Algebraic Entry) makes working with units expressions easier. Otherwise, you will have to remember to hit the apostrophe key every time you wish to enter units.

The u s (calc-simplify-units) [usimplify] command simplifies a units expression. It uses a s (calc-simplify) to simplify the expression first as a regular algebraic formula; it then looks for features that can be further simplified by converting one object's units to be compatible with another's. For example, `5 m + 23 mm' will simplify to `5.023 m'. When different but compatible units are added, the righthand term's units are converted to match those of the lefthand term. See section Simplification Modes, for a way to have this done automatically at all times.

Units simplification also handles quotients of two units with the same dimensionality, as in `2 in s/L cm' to `5.08 s/L'; fractional powers of unit expressions, as in `sqrt(9 mm^2)' to `3 mm' and `sqrt(9 acre)' to a quantity in meters; and floor, ceil, round, rounde, roundu, trunc, float, frac, abs, and clean applied to units expressions, in which case the operation in question is applied only to the numeric part of the expression. Finally, trigonometric functions of quantities with units of angle are evaluated, regardless of the current angular mode.

The u c (calc-convert-units) command converts a units expression to new, compatible units. For example, given the units expression `55 mph', typing u c m/s RET produces `24.5872 m/s'. If the units you request are inconsistent with the original units, the number will be converted into your units times whatever "remainder" units are left over. For example, converting `55 mph' into acres produces `6.08e-3 acre / m s'. (Recall that multiplication binds more strongly than division in Calc formulas, so the units here are acres per meter-second.) Remainder units are expressed in terms of "fundamental" units like `m' and `s', regardless of the input units.

One special exception is that if you specify a single unit name, and a compatible unit appears somewhere in the units expression, then that compatible unit will be converted to the new unit and the remaining units in the expression will be left alone. For example, given the input `980 cm/s^2', the command u c ms will change the `s' to `ms' to get `9.8e-4 cm/ms^2'. The "remainder unit" `cm' is left alone rather than being changed to the base unit `m'.

You can use explicit unit conversion instead of the u s command to gain more control over the units of the result of an expression. For example, given `5 m + 23 mm', you can type u c m or u c mm to express the result in either meters or millimeters. (For that matter, you could type u c fath to express the result in fathoms, if you preferred!)

In place of a specific set of units, you can also enter one of the units system names si, mks (equivalent), or cgs. For example, u c si RET converts the expression into International System of Units (SI) base units. Also, u c base converts to Calc's base units, which are the same as si units except that base uses `g' as the fundamental unit of mass whereas si uses `kg'.

The u c command also accepts composite units, which are expressed as the sum of several compatible unit names. For example, converting `30.5 in' to units `mi+ft+in' (miles, feet, and inches) produces `2 ft + 6.5 in'. Calc first sorts the unit names into order of decreasing relative size. It then accounts for as much of the input quantity as it can using an integer number times the largest unit, then moves on to the next smaller unit, and so on. Only the smallest unit may have a non-integer amount attached in the result. A few standard unit names exist for common combinations, such as mfi for `mi+ft+in', and tpo for `ton+lb+oz'. Composite units are expanded as if by a x, so that `(ft+in)/hr' is first converted to `ft/hr+in/hr'.

If the value on the stack does not contain any units, u c will prompt first for the old units which this value should be considered to have, then for the new units. Assuming the old and new units you give are consistent with each other, the result also will not contain any units. For example, u c cm RET in RET converts the number 2 on the stack to 5.08.

The u b (calc-base-units) command is shorthand for u c base; it converts the units expression on the top of the stack into base units. If u s does not simplify a units expression as far as you would like, try u b.

The u c and u b commands treat temperature units (like `degC' and `K') as relative temperatures. For example, u c converts `10 degC' to `18 degF': A change of 10 degrees Celsius corresponds to a change of 18 degrees Fahrenheit.

The u t (calc-convert-temperature) command converts absolute temperatures. The value on the stack must be a simple units expression with units of temperature only. This command would convert `10 degC' to `50 degF', the equivalent temperature on the Fahrenheit scale.

The u r (calc-remove-units) command removes units from the formula at the top of the stack. The u x (calc-extract-units) command extracts only the units portion of a formula. These commands essentially replace every term of the formula that does or doesn't (respectively) look like a unit name by the constant 1, then resimplify the formula.

The u a (calc-autorange-units) command turns on and off a mode in which unit prefixes like k ("kilo") are automatically applied to keep the numeric part of a units expression in a reasonable range. This mode affects u s and all units conversion commands except u b. For example, with autoranging on, `12345 Hz' will be simplified to `12.345 kHz'. Autoranging is useful for some kinds of units (like Hz and m), but is probably undesirable for non-metric units like ft and tbsp. (Composite units are more appropriate for those; see above.)

Autoranging always applies the prefix to the leftmost unit name. Calc chooses the largest prefix that causes the number to be greater than or equal to 1.0. Thus an increasing sequence of adjusted times would be `1 ms, 10 ms, 100 ms, 1 s, 10 s, 100 s, 1 ks'. Generally the rule of thumb is that the number will be adjusted to be in the interval `[1 .. 1000)', although there are several exceptions to this rule. First, if the unit has a power then this is not possible; `0.1 s^2' simplifies to `100000 ms^2'. Second, the "centi-" prefix is allowed to form cm (centimeters), but will not apply to other units. The "deci-," "deka-," and "hecto-" prefixes are never used. Thus the allowable interval is `[1 .. 10)' for millimeters and `[1 .. 100)' for centimeters. Finally, a prefix will not be added to a unit if the resulting name is also the actual name of another unit; `1e-15 t' would normally be considered a "femto-ton," but it is written as `1000 at' (1000 atto-tons) instead because ft would be confused with feet.

The Units Table

The u v (calc-enter-units-table) command displays the units table in another buffer called *Units Table*. Each entry in this table gives the unit name as it would appear in an expression, the definition of the unit in terms of simpler units, and a full name or description of the unit. Fundamental units are defined as themselves; these are the units produced by the u b command. The fundamental units are meters, seconds, grams, kelvins, amperes, candelas, moles, radians, and steradians.

The Units Table buffer also displays the Unit Prefix Table. Note that two prefixes, "kilo" and "hecto," accept either upper- or lower-case prefix letters. `Meg' is also accepted as a synonym for the `M' prefix. Whenever a unit name can be interpreted as either a built-in name or a prefix followed by another built-in name, the former interpretation wins. For example, `2 pt' means two pints, not two pico-tons.

The Units Table buffer, once created, is not rebuilt unless you define new units. To force the buffer to be rebuilt, give any numeric prefix argument to u v.

The u V (calc-view-units-table) command is like u v except that the cursor is not moved into the Units Table buffer. You can type u V again to remove the Units Table from the display. To return from the Units Table buffer after a u v, type M-# c again or use the regular Emacs C-x o (other-window) command. You can also kill the buffer with C-x k if you wish; the actual units table is safely stored inside the Calculator.

The u g (calc-get-unit-definition) command retrieves a unit's defining expression and pushes it onto the Calculator stack. For example, u g in will produce the expression `2.54 cm'. This is the same definition for the unit that would appear in the Units Table buffer. Note that this command works only for actual unit names; u g km will report that no such unit exists, for example, because km is really the unit m with a k ("kilo") prefix. To see a definition of a unit in terms of base units, it is easier to push the unit name on the stack and then reduce it to base units with u b.

The u e (calc-explain-units) command displays an English description of the units of the expression on the stack. For example, for the expression `62 km^2 g / s^2 mol K', the description is "Square-Kilometer Gram per (Second-squared Mole Degree-Kelvin)." This command uses the English descriptions that appear in the righthand column of the Units Table.

Predefined Units

Since the exact definitions of many kinds of units have evolved over the years, and since certain countries sometimes have local differences in their definitions, it is a good idea to examine Calc's definition of a unit before depending on its exact value. For example, there are three different units for gallons, corresponding to the US (gal), Canadian (galC), and British (galUK) definitions. Also, note that oz is a standard ounce of mass, ozt is a Troy ounce, and ozfl is a fluid ounce.

The temperature units corresponding to degrees Kelvin and Centigrade (Celsius) are the same in this table, since most units commands treat temperatures as being relative. The calc-convert-temperature command has special rules for handling the different absolute magnitudes of the various temperature scales.

The unit of volume "liters" can be referred to by either the lower-case l or the upper-case L.

The unit A stands for Amperes; the name Ang is used

The unit pt stands for pints; the name point stands for a typographical point, defined by `72 point = 1 in'. There is also tpt, which stands for a printer's point as defined by the TeX typesetting system: `72.27 tpt = 1 in'.

The unit e stands for the elementary (electron) unit of charge; because algebra command could mistake this for the special constant e, Calc provides the alternate unit name ech which is preferable to e.

The name g stands for one gram of mass; there is also gf, one gram of force. (Likewise for lb, pounds, and lbf.) Meanwhile, one "g" of acceleration is denoted ga.

The unit ton is a U.S. ton of `2000 lb', and t is a metric ton of `1000 kg'.

The names s (or sec) and min refer to units of time; arcsec and arcmin are units of angle.

Some "units" are really physical constants; for example, c represents the speed of light, and h represents Planck's constant. You can use these just like other units: converting `.5 c' to `m/s' expresses one-half the speed of light in meters per second. You can also use this merely as a handy reference; the u g command gets the definition of one of these constants in its normal terms, and u b expresses the definition in base units.

Two units, pi and fsc (the fine structure constant, approximately 1/137) are dimensionless. The units simplification commands simply treat these names as equivalent to their corresponding values. However you can, for example, use u c to convert a pure number into multiples of the fine structure constant, or u b to convert this back into a pure number. (When u c prompts for the "old units," just enter a blank line to signify that the value really is unitless.)

User-Defined Units

Calc provides ways to get quick access to your selected "favorite" units, as well as ways to define your own new units.

To select your favorite units, store a vector of unit names or expressions in the Calc variable Units. The u 1 through u 9 commands (calc-quick-units) provide access to these units. If the value on the top of the stack is a plain number (with no units attached), then u 1 gives it the specified units. (Basically, it multiplies the number by the first item in the Units vector.) If the number on the stack does have units, then u 1 converts that number to the new units. For example, suppose the vector `[in, ft]' is stored in Units. Then 30 u 1 will create the expression `30 in', and u 2 will convert that expression to `2.5 ft'.

The u 0 command accesses the tenth element of Units. Only ten quick units may be defined at a time. If the Units variable has no stored value (the default), or if its value is not a vector, then the quick-units commands will not function. The s U command is a convenient way to edit the Units variable; see section Other Operations on Variables.

The u d (calc-define-unit) command records the units expression on the top of the stack as the definition for a new, user-defined unit. For example, putting `16.5 ft' on the stack and typing u d rod defines the new unit `rod' to be equivalent to 16.5 feet. The unit conversion and simplification commands will now treat rod just like any other unit of length. You will also be prompted for an optional English description of the unit, which will appear in the Units Table.

The u u (calc-undefine-unit) command removes a user-defined unit. It is not possible to remove one of the predefined units, however.

If you define a unit with an existing unit name, your new definition will replace the original definition of that unit. If the unit was a predefined unit, the old definition will not be replaced, only "shadowed." The built-in definition will reappear if you later use u u to remove the shadowing definition.

To create a new fundamental unit, use either 1 or the unit name itself as the defining expression. Otherwise the expression can involve any other units that you like (except for composite units like `mfi'). You can create a new composite unit with a sum of other units as the defining expression. The next unit operation like u c or u v will rebuild the internal unit table incorporating your modifications. Note that erroneous definitions (such as two units defined in terms of each other) will not be detected until the unit table is next rebuilt; u v is a convenient way to force this to happen.

Temperature units are treated specially inside the Calculator; it is not possible to create user-defined temperature units.

The u p (calc-permanent-units) command stores the user-defined units in your `.emacs' file, so that the units will still be available in subsequent Emacs sessions. If there was already a set of user-defined units in your `.emacs' file, it is replaced by the new set. (See section General Mode Commands, for a way to tell Calc to use a different file instead of `.emacs'.)


Go to the first, previous, next, last section, table of contents.