Description

Conversion of units of measurement

Ivan Raikov

Version

• 1.4 The predefined quantities have been put into unit-definitions.scm
• 1.3 Bug fix in unit-convert
• 1.2 Changed unit-convert to return a single numeric value given a single conversion argument
• 1.1 Added information units [patch by Joshua Griffith]
• 1.0 Initial release

Usage

(require-extension unitconv)(include "unit-definitions")

unitconv.egg

Documentation

The unitconv library is an implementation of unit conversion routines based on the paper by Gordon S. Novak:

```Conversion of Units of Measurement.
IEEE Trans. on Software Engineering, vol. 21, no. 8 (Aug. 1995), pp. 651-661.```
(Available online at http://www.cs.utexas.edu/users/novak/units95.html).

Correctness of unit conversion is established by the technique of dimensional analysis: the source and goal units must have the same dimensions. Following Novak, this extension defines a dimension as an 8-vector of integral powers of the following base quantities:

```(define-base-quantity  Unity          0)
(define-base-quantity  Length         dimvals[0])
(define-base-quantity  Time           dimvals[1])
(define-base-quantity  Temperature    dimvals[2])
(define-base-quantity  Mass           dimvals[3])
(define-base-quantity  Current        dimvals[4])
(define-base-quantity  Luminosity     dimvals[5])
(define-base-quantity  Substance      dimvals[6])
(define-base-quantity  Currency       dimvals[7])
(define-base-quantity  Information    dimvals[8])```

The unit conversion routine uses dimension integers to check that the requested unit conversion is legitimate. For example, the conversion from kilograms to meters illegal. Consequently, the dimensionality of each unit must be specified when the unit is declared.

```;; Syntax is (define-unit name quantity factor abbreviation ...)

;; define units of length
(define-unit meter     Length 1.0       m meters)
(define-unit inch      Length 0.0254    in inches)

;; define units of mass and time
(define-unit kilogram  Mass   1.0       kg kilograms)
(define-unit second    Time   1.0       s seconds)

;; define two new derived quantities: acceleration and force
(define-quantity   Acceleration  (/ Length (** Time 2)))
(define-quantity   Force         (* Mass Acceleration))

;; define a unit of force
(define-unit newton       Force (/ (* kilogram meter) (* second second)) nt newtons)
```

Now only conversion between units of the same dimensionality is permitted:

```(unit-convert meter inch 1) ->  39.3700787401575
(unit-convert meter inch 2 3 4) ->  (78.740157480315 118.110236220472 157.48031496063)

(unit-convert meter kilogram 1)
Error: (unitconv) unit-convert : given units are of different dimensions:
source= #(unit meter (m meters) [Length] 1.0) ;
dest=  #(unit kilogram (kg kilograms) [Mass] 1.0)```

Procedures and Macros

procedure: unit-convert:: SRC * DEST * [VAL1 ...] -> (FACTOR1 ... )

Converts the given numeric values expressed in unit SRC to their equivalents in unit DEST.

Arguments SRC, DEST are records of type unit. See the definitions below for information about the units that are defined by this extension, as well as for information about creating new units.

procedure: unit-equal?:: UNIT1 * UNIT2 -> BOOL
Returns true if the two units have the same dimension and factor, false otherwise.
macro: (define-quantity name expr)

Defines a derivative quantity NAME.

EXPR is an S-expression with the following syntax:

```quantity-expr ::=  base-quantity
| derived-quantity
| (* quantity-expr quantity-expr)
| (* quantity-expr integer)
| (/ quantity-expr quantity-expr)
| (/ quantity-expr integer)
| (** quantity-expr integer)```
where
 base-quantity one of the predefined base quantities derived-quantity a quantity created by define-quantity ** exponentiation operator

macro: (define-unit name dims factor . abbrevs)

Defines a variable NAME that holds a unit definition of a unit with the given dimension and factor.

DIMS can be either one of the base quantities (see next section) or a derivative quantity created by define-quantity.

Predefined Quantities

Base Quantities
 Unity (dimensionless) Length Time Temperature Mass Current Luminosity Substance Currency Information
Derived Quantities
 Geometry Area (** Length 2) Volume (** Length 3) Mechanics Velocity (/ Length Time) Acceleration (/ Length (** Time 2)) Force (* Mass Acceleration) Pressure (/ Force Area) Energy (* Force Length) Power (/ Energy Time) Electricity Charge (* Current Time) Potential (/ Energy Charge) Capacitance (/ Charge Potential) Resistance (/ Potential Current) Conductance (/ Current Potential) Inductance (/ (* Potential Time) Current) Chemistry Concentration (/ Substance Volume) Density (/ Mass Volume) Optics Luminance (/ Luminosity Area) Other Frequency (/ Unity Time)

Predefined Units

 SI Unit Prefixes Name Quantity Factor yocto Unity 1e-24 zepto Unity 1e-21 atto Unity 1e-18 femto Unity 1e-15 pico Unity 1e-12 nano Unity 1e-09 micro Unity 1e-06 milli Unity 0.001 centi Unity 0.01 deci Unity 0.1 deca Unity 10.0 hecto Unity 100.0 kilo Unity 1000.0 mega Unity 1000000.0 giga Unity 1000000000.0 tera Unity 1000000000000.0 peta Unity 1e+15 exa Unity 1e+18 zetta Unity 1e+21 yotta Unity 1e+24 Time Multiples twelve Unity 12 sixty Unity 60 Angles radian Unity 1.0 (rad radians) degree Unity (/ pi 180) (deg degrees) Units of Length meter Length 1.0 (m meters) inch Length 0.0254 (in inches) foot Length 0.3048 (ft feet) angstrom Length 1e-10 (ang angstroms) parsec Length 3.083e+16 (parsecs) Units of Area and Volume square-meter Area (* meter meter) (m^2 m2 meter-squared meters-squared square-meters) square-inch Area (* inch inch) (in^2 inch-squared inches-squared square-inches) square-micron Area (* micrometer micrometer) (um^2 micrometer-squared micrometers-squared micron-squared microns-squared square-microns) square-millimeter Area (* millimeter millimeter) (mm^2 millimeter-squared millimeters-squared square-millimeters) cubic-meter Volume (* meter (* meter meter)) (m^3 meter-cubed meters-cubed cubic-meters) liter Volume (* 0.001 cubic-meter) (L litre liters) Units of Mass kilogram Mass 1.0 (kg kilograms) gram Mass 0.001 (g grams) pound Mass (* gram 453.59237) (lb pounds) slug Mass (* pound 32.17405) (slugs) atomic-mass-unit Mass 1.6605402e-27 (amu atomic-mass-units) Units of Time second Time 1.0 (s seconds) hour Time (* sixty (* sixty second)) (h hrs hours) Units of Acceleration meters-per-second-squared Acceleration (/ meter (* second second)) (m/s2 m/s^2 m/sec2 m/sec^2) Units of Frequency hertz Frequency 1.0 (hz) Units of Force newton Force (/ (* kilogram meter) (* second second)) (nt newtons) pound-force Force (/ (* slug foot) (* second second)) (lbf) Units of Power watt Power (/ (* kilogram meter meter) (* second second second)) (W watts) horsepower Power (* 550 (/ (* foot pound-force) second)) (hp) Units of Energy joule Energy (* newton meter) (J joules) electron-volt Energy (* 1.60217733e-19 joule) (ev electron-volts) kilowatt-hour Energy (* kilo (* watt hour)) (kwh kilowatt-hours) calorie Energy (* 4.184 joule) (cal calories) erg Energy (* 1e-07 joule) (ergs) british-thermal-unit Energy (* 1055.056 joule) (btu btus) Units of Current ampere Current 1.0 (A amp amps amperes) Units of Electric Charge coulomb Charge (* ampere second) (C coulombs) Units of Electric Potential volt Potential (/ (* kilogram meter meter) (* ampere second second second)) (V volts) Units of Resistance ohm Resistance (/ volt ampere) (ohms) Units of Capacitance farad Capacitance (/ coulomb volt) (F farads) Units of Conductance siemens Conductance (/ ampere volt) (S mho) Units of Inductance henry Inductance (/ (* meter meter kilogram) (* ampere ampere second second)) (H) Units of Substance mole Substance 1.0 (mol moles) Units of density rho Density (/ kilogram cubic-meter) Units of concentration molarity Concentration (/ mole liter) (M mol/L) parts-per-million Concentration (/ milligram kilogram) (ppm) Units of temperature degK Temperature 1.0 (K)

Examples

```csi> (require-extension unitconv)

;; Unit conversion
csi> (unit-convert meter inch 2 3 4)
(78.740157480315 118.110236220472 157.48031496063)

;; Unit definition
csi> (define-unit  pascal Pressure (/ kilogram (* meter second second)) Pa)
csi> pascal
#(unit pascal (Pa) [Pressure] 1.0)

csi> (define-unit  pound-force-per-square-inch Pressure (/ pound-force (* inch inch)) psi)
csi> pound-force-per-square-inch
#(unit pound-force-per-square-inch (psi) [Pressure] 6894.75760251898)

csi> (unit-convert pound-force-per-square-inch pascal 1850)
12755301.5646601

;; Unit prefix definition
csi> (define millimeter     (make-unit-prefix  milli meter mm millimeters))

csi> (unit-convert inch millimeter 12)
304.8```

```Copyright 2007 Ivan Raikov.

This program is free software: you can redistribute it and/or modify