units(n)
πΈ NAME πΈ
okay bestie, welcome to the manual page for units(n) β and yes it's literally just unit conversion. that's the whole vibe. π
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require units ?2.2.3?
::units::convert value targetUnits
::units::reduce unitString
::units::new name baseUnits
π¦ DESCRIPTION π¦
so this library is literally a conversion facility, no cap. it takes all those scientific and engineering shorthand notations and turns them into floating point numbers. which means app devs can take values that are all wearing different unit outfits and scale them into one uniform number. iconic. π
the units conversion facility can also convert between compatible units. like, say your app is expecting a value in ohms (Resistance), and the user rocks up with milliwebers/femtocoulomb β the conversion routine will handle it appropriately, because it's that girl. but if you try a conversion that doesn't actually work? an error gets generated. periodt. π
values get scaled from one set of units to another by dimensional analysis. both the value units and the target units get reduced into primitive units plus a scale factor. then the units get checked for compatibility, and the scale factors get applied by multiplication and division. this technique is extremely flexible and quite robust, lowkey the main character of the whole page. β¨
you can also define new units and new unit abbreviations in terms of the existing units and abbreviations. it's even possible to define a brand new primitive unit, though honestly you probably won't need to. new units most commonly get defined to accommodate non-SI measurement systems β like defining the unit inch as 2.54 cm. slay. π₯
π COMMANDS π
::units::convert value targetUnits
converts the value string into a floating point number, scaled to the specified targetUnits. the value string may contain a number and units. if units are specified, then they must be compatible with the targetUnits. if units are not specified for the value, then it will be scaled to the target units. for example:
% ::units::convert "2.3 miles" km
3.7014912
% ::units::convert 300m/s miles/hour
671.080887616
% ::units::convert "1.0 m kg/s^2" newton
1.0
% ::units::convert 1.0 millimeter
1000.0
::units::reduce unitString
returns a unit string consisting of a scale factor followed by a space separated list of sorted and reduced primitive units. the reduced unit string may include a forward-slash (separated from the surrounding primitive subunits by spaces) indicating that the remaining subunits are in the denominator. generates an error if the unitString is invalid. like:
% ::units::reduce pascal
1000.0 gram / meter second second
::units::new name baseUnits
creates a new unit conversion with the specified name. the new unit name must be only alphabetic (upper or lower case) letters, okay? no numbers, no nonsense. π
the baseUnits string can consist of any valid units conversion string, including constant factors, numerator and denominator parts, units with prefixes, and exponents. the baseUnits may contain any number of subunits, but it must reduce to primitive units.
BaseUnits could also be the string -primitive to represent a new kind of quantity which cannot be derived from other units. but you probably would not do that unless you have discovered some kind of new universal property. fr fr. π
% ::units::new furlong "220 yards"
% ::units::new fortnight "14 days"
% ::units::convert 100m/s furlongs/fortnight
601288.475303
π UNIT STRING FORMAT π
value and unit string format is quite flexible, bestie. it is possible to define virtually any combination of units, prefixes, and powers. but valid unit strings must conform to these rules:
- a unit string consists of an optional scale factor followed by zero or more subunits. the scale factor must be a valid floating point number, and may or may not be separated from the subunits. the scale factor could be negative. (yes, negative. we said what we said.)
- subunits are separated from each other by one or more separator characters, which are space (
" "), hyphen ("-"), asterisk ("*"), and forward-slash ("/"). sure, go ahead and complain about using a minus sign ("-") to represent multiplication. it just isn't sound mathematics, and, by rights, we should require everyone to use the asterisk ("*") to separate all units. but the bottom line is that complex unit strings likem-kg/s^2are pleasantly readable. and honestly? the readability ate. π - the forward-slash separator (
"/") indicates that following subunits are in the denominator. there can be at most one forward-slash separator. - subunits can be floating point scale factors, but with the exception of the leading scale factor, they must be surrounded by valid separators. subunit scale factors cannot be negative. (remember that the hyphen is a unit separator, so this is not a drill π¨)
- subunits can be valid units or abbreviations. they may include a prefix. they may include a plural suffix
"s"or"es". they may also include a power string denoted by a circumflex ("^"), followed by an integer, after the unit name (or plural suffix, if there is one). negative exponents are not allowed. (and again β remember that the hyphen is a unit separator.)
π EXAMPLE VALID UNIT STRINGS π
here's the glow-up chart, unit string on the left, reduced unit string on the right:
meterβ1.0 meterkilometerβ1000.0 meterkmβ1000.0 meterkm/sβ1000.0 meter / second/microsecondβ1000000.0 / second/usβ1000000.0 / secondkg-m/s^2β1000.0 gram meter / second second30secondβ30.0 second30 secondβ30.0 second30 secondsβ30.0 second200*meter/20.5*secondβ9.75609756098 meter / second
π not me warning you again but: the hyphen is a unit separator. it multiplies. it does not subtract. iykyk.
β¨ SI UNITS β¨
The standard SI units are predefined according to NIST Special Publication 330. Standard units for both SI Base Units (Table 1) and SI Derived Units with Special Names (Tables 3a and 3b) are included here for reference. Each standard unit name and abbreviation are included in this package.
πΈ SI BASE UNITS πΈ
- Length β unit name
meter, abbreviationm - Mass β unit name
kilogram, abbreviationkg - Time β unit name
second, abbreviations - Current β unit name
ampere, abbreviationA - Temperature β unit name
kelvin, abbreviationK - Amount β unit name
mole, abbreviationmol - Luminous Intensity β unit name
candela, abbreviationcd
π¦ SI DERIVED UNITS WITH SPECIAL NAMES π¦
- plane angle β unit name
radian, abbr.rad, unitsm/m, base unitsm/m - solid angle β unit name
steradian, abbr.sr, unitsm^2/m^2, base unitsm^2/m^2 - frequency β unit name
hertz, abbr.Hz, units (none listed), base units/s - force β unit name
newton, abbr.N, units (none listed), base unitsm-kg/s^2 - pressure β unit name
pascal, abbr.Pa, unitsN/m^2, base unitskg/m-s^2 - energy, work β unit name
joule, abbr.J, unitsN-m, base unitsm^2-kg/s^2 - power, radiant flux β unit name
watt, abbr.W, unitsJ/s, base unitsm^2-kg/s^3 - electric charge β unit name
coulomb, abbr.C, units (none listed), base unitss-A - electric potential β unit name
volt, abbr.V, unitsW/A, base unitsm^2-kg/s^3-A - capacitance β unit name
farad, abbr.F, unitsC/V, base unitss^4-A^2/m^2-kg - electric resistance β unit name
ohm, abbr. (none listed), unitsV/A, base unitsm^2-kg/s^3-A^2 - electric conductance β unit name
siemens, abbr.S, unitsA/V, base unitss^3-A^2/m^2-kg - magnetic flux β unit name
weber, abbr.Wb, unitsV-s, base unitsm^2-kg/s^2-A - magnetic flux density β unit name
tesla, abbr.T, unitsWb/m^2, base unitskg/s^2-A - inductance β unit name
henry, abbr.H, unitsWb/A, base unitsm^2-kg/s^2-A^2 - luminous flux β unit name
lumen, abbr.lm, units (none listed), base unitscd-sr - illuminance β unit name
lux, abbr.lx, unitslm/m^2, base unitscd-sr/m^2 - activity (of a radionuclide) β unit name
becquerel, abbr.Bq, units (none listed), base units/s - absorbed dose β unit name
gray, abbr.Gy, unitsJ/kg, base unitsm^2/s^2 - dose equivalent β unit name
sievert, abbr.Sv, unitsJ/kg, base unitsm^2/s^2
Note that the SI unit kilograms is actually implemented as grams because 1e-6 kilogram = 1 milligram, not 1 microkilogram. The abbreviation for Electric Resistance (ohms), which is the omega character, is not supported.
Also note that there is no support for Celsius or Farenheit temperature. The units conversion routines can only scale values with multiplication and division, so it is not possible to convert from thermodynamic temperature (kelvins) to absolute degrees Celsius or Farenheit. Conversion of thermodynamic quantities, such as thermal expansion (per unit temperature), however, are easy to add to the units library.
SI Units can have a multiple or sub-multiple prefix. The prefix or its abbreviation should appear before the unit, without spaces. Compound prefixes are not allowed, and a prefix should never be used alone. These prefixes are defined in Table 5 of Special Publication 330.
π SI PREFIXES π
- yotta β abbr.
Yβ factor1e24 - zetta β abbr.
Zβ factor1e21 - exa β abbr.
Eβ factor1e18 - peta β abbr.
Pβ factor1e15 - tera β abbr.
Tβ factor1e12 - giga β abbr.
Gβ factor1e9 - mega β abbr.
Mβ factor1e6 - kilo β abbr.
kβ factor1e3 - hecto β abbr.
hβ factor1e2 - deka β abbr.
daβ factor1e1 - deca β abbr. (none listed) β factor
1e1 - deci β abbr.
dβ factor1e-1 - centi β abbr.
cβ factor1e-2 - milli β abbr.
mβ factor1e-3 - micro β abbr.
uβ factor1e-6 - nano β abbr.
nβ factor1e-9 - pico β abbr.
pβ factor1e-12 - femto β abbr.
fβ factor1e-15 - atto β abbr.
aβ factor1e-18 - zepto β abbr.
zβ factor1e-21 - yocto β abbr.
yβ factor1e-24
Note that we define the same prefix with both the USA ("deka") and non-USA ("deca") spellings. Also note that we take the liberty of allowing "micro" to be typed as a "u" instead of the Greek character mu.
Many non-SI units are commonly used in applications. Appendix B.8 of NIST Special Publication 811 lists many non-SI conversion factors. It is not possible to include all possible unit definitions in this package. In some cases, many different conversion factors exist for a given unit, depending on the context. (The appendix lists over 40 conversions for British thermal units!) Application specific conversions can always be added using the new command, but some well known and often used conversions are included in this package.
π NON-SI UNITS π
- angstrom β abbr. (none listed) β base units
1.0E-10 m - astronomicalUnit β abbr.
AUβ base units1.495979E11 m - atmosphere β abbr. (none listed) β base units
1.01325E5 Pa - bar β abbr. (none listed) β base units
1.0E5 Pa - calorie β abbr. (none listed) β base units
4.1868 J - curie β abbr. (none listed) β base units
3.7E10 Bq - day β abbr. (none listed) β base units
8.64E4 s - degree β abbr. (none listed) β base units
1.745329E-2 rad - erg β abbr. (none listed) β base units
1.0E-7 J - faraday β abbr. (none listed) β base units
9.648531 C - fermi β abbr. (none listed) β base units
1.0E-15 m - foot β abbr.
ftβ base units3.048E-1 m - gauss β abbr. (none listed) β base units
1.0E-4 T - gilbert β abbr. (none listed) β base units
7.957747E-1 A - grain β abbr.
grβ base units6.479891E-5 kg - hectare β abbr.
haβ base units1.0E4 m^2 - hour β abbr.
hβ base units3.6E3 s - inch β abbr.
inβ base units2.54E-2 m - lightYear β abbr. (none listed) β base units
9.46073E15 m - liter β abbr.
Lβ base units1.0E-3 m^3 - maxwell β abbr.
Mxβ base units1.0E-8 Wb - mho β abbr. (none listed) β base units
1.0 S - micron β abbr. (none listed) β base units
1.0E-6 m - mil β abbr. (none listed) β base units
2.54E-5 m - mile β abbr.
miβ base units1.609344E3 m - minute β abbr.
minβ base units6.0E1 s - parsec β abbr.
pcβ base units3.085E16 m - pica β abbr. (none listed) β base units
4.233333E-3 m - pound β abbr.
lbβ base units4.535924E-1 kg - revolution β abbr. (none listed) β base units
6.283185 rad - revolutionPerMinute β abbr.
rpmβ base units1.047198E-1 rad/s - yard β abbr.
ydβ base units9.144E-1 m - year β abbr. (none listed) β base units
3.1536E7 s
π§ QUANTITIES AND DERIVED UNITS WITH SPECIAL NAMES π§
This units conversion package is limited specifically to unit reduction, comparison, and scaling. This package does not consider any of the quantity names for either base or derived units. A similar implementation or an extension in a typed or object-oriented language might introduce user defined types for the quantities. Quantity type checking could be used, for example, to ensure that all length values properly reduced to meters, or that all velocity values properly reduced to meters/second.
A C implementation of this package has been created to work in conjunction with the Simplified Wrapper Interface Generator (http://www.swig.org/). That package (units.i) exploits SWIG's typemap system to automatically convert script quantity strings into floating point quantities. Function arguments are specified as quantity types (e.g., typedef float Length), and target units (expected by the C application code) are specified in an associative array. Default units are also defined for each quantity type, and are applied to any unit-less quantity strings.
A units system enhanced with quantity type checking might benefit from inclusion of other derived types which are expressed in terms of special units, as illustrated in Table 2 of NIST Publication 330. The quantity area, for example, could be defined as units properly reducing to meter^2, although the utility of defining a unit named square meter is arguable.
π REFERENCES π
The unit names, abbreviations, and conversion values are derived from those published by the United States Department of Commerce Technology Administration, National Institute of Standards and Technology (NIST) in NIST Special Publication 330: The International System of Units (SI) and NIST Special Publication 811: Guide for the Use of the International System of Units (SI). Both of these publications are available (as of December 2000) from http://physics.nist.gov/cuu/Reference/contents.html
The ideas behind implementation of this package is based in part on code written in 1993 by Adrian Mariano which performed dimensional analysis of unit strings using fixed size tables of C structs. After going missing in the late 1990's, Adrian's code has reappeared in the GNU Units program at http://www.gnu.org/software/units/
π AUTHORS π
Robert W. Techentin
π BUGS, IDEAS, FEEDBACK π
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category units of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation.
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.
π KEYWORDS π
angle, constants, conversion, distance, radians, unit
π COPYRIGHT π
Copyright (c) 2000-2005 Mayo Foundation
And that's the whole thing, bestie π the footer says source/version tcllib 2.2.3 and manual units(n) β no date shown, but it's iconic anyway. periodt π«Ά
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2000-2005 Mayo Foundation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms