units(3tcl)
units π
okay bestie, welcome to units(3tcl) β the Tcl manual page that's literally just "Convert and manipulate quantities with units" and honestly? iconic. π
β¨ NAME β¨
units β unit conversion, and yes that's the whole name, she's mysterious like that π
π¦ 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 π
okay so listen. this library is a conversion facility, and what she does is take a whole buffet of scientific and engineering shorthand notations and turn them into floating point numbers. that's it, that's the job. and it means application devs can just... convert values with different units into uniformly scaled numbers without losing their minds. slay. π
she can also convert between compatible units β like, if your app is expecting a value in ohms (Resistance, yes capital R, she's fancy) and the user rolls up specifying milliwebers/femtocoulomb, the conversion routine will handle it appropriately. bestie is competent. but β and hear me on this β if you attempt an incorrect conversion, an error gets generated. no cap, she will not do it. π
the way she does it is by dimensional analysis. both the value units and the target units get reduced into primitive units plus a scale factor. units get checked for compatibility, and then the scale factors are applied by multiplication and division. this technique is extremely flexible and quite robust β literally built different. π₯
and you can define new units and new unit abbreviations in terms of the existing ones! you can even define a brand new primitive unit, although honestly that'll probably be unnecessary. most commonly you'd define new units to accommodate non-SI measurement systems β like, say, defining the unit inch as 2.54 cm. periodt.
π COMMANDS π
::units::convert value targetUnitsβ takes that value string and turns it into a floating point number, scaled to the targetUnits you specified. the value string may contain a number and units. if units are specified, they've gotta be compatible with targetUnits. if units are not specified for the value, then it just gets scaled to the target units. like:
% ::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β hands you back a unit string made 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) which means the remaining subunits are chillin' in the denominator. if the unitString is invalid she generates an error, no mercy π
% ::units::reduce pascal
1000.0 gram / meter second second
::units::new name baseUnitsβ creates a new unit conversion with the specified name. heads up: the new unit name must be only alphabetic (upper or lower case) letters. the baseUnits string can consist of any valid units conversion string β constant factors, numerator and denominator parts, units with prefixes, exponents, the whole menu. baseUnits may contain any number of subunits, but it must reduce to primitive units. baseUnits could also be the string-primitiveto represent a whole 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. go off i guess?? π
% ::units::new furlong "220 yards"
% ::units::new fortnight "14 days"
% ::units::convert 100m/s furlongs/fortnight
601288.475303
πΈ UNIT STRING FORMAT πΈ
the value and unit string format is quite flexible. you can define virtually any combination of units, prefixes, and powers. but valid unit strings must conform to these rules, bestie, so let's go:
- 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 it may or may not be separated from the subunits. also the scale factor could be negative. yes, negative. she said what she said. π
- subunits are separated from each other by one or more separator characters, which are space (
" "), hyphen ("-"), asterisk ("*"), and forward-slash ("/"). and yeah, 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? that's the vibe check that matters. π - the forward-slash separator (
"/") means that the following subunits are in the denominator. and there can be at most one forward-slash separator. one. uno. don't push it. π - subunits can be floating point scale factors, but with the exception of the leading scale factor, they must be surrounded by valid separators. and subunit scale factors cannot be negative. (remember that the hyphen is a unit separator. yes i'm saying it again. it's important.) π«Ά
- 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 the 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 π§
so here's the glow-up: every unit string on the left reduces to the unit string on the right. obsessed.
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
β¨ SI UNITS β¨
okay so the standard SI units are pre-defined according to NIST Special Publication 330. π We've got standard units for both SI Base Units (Table 1) and SI Derived Units with Special Names (Tables 3a and 3b) right here for reference. Every standard unit name and abbreviation is included in this package. no cap. β¨
β¨ SI BASE UNITS β¨
these are the base units, the main characters of the SI system. π
- 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 β¨
these are the derived units that got their own special names, and they're iconic. π
- plane angle β
radian(rad), units:m/m, base units:m/m - solid angle β
steradian(sr), units:m^2/m^2, base units:m^2/m^2 - frequency β
hertz(Hz), units: (none), base units:/s - force β
newton(N), units: (none), base units:m-kg/s^2 - pressure β
pascal(Pa), units:N/m^2, base units:kg/m-s^2 - energy, work β
joule(J), units:N-m, base units:m^2-kg/s^2 - power, radiant flux β
watt(W), units:J/s, base units:m^2-kg/s^3 - electric charge β
coulomb(C), units: (none), base units:s-A - electric potential β
volt(V), units:W/A, base units:m^2-kg/s^3-A - capacitance β
farad(F), units:C/V, base units:s^4-A^2/m^2-kg - electric resistance β
ohm(no abbreviation), units:V/A, base units:m^2-kg/s^3-A^2 - electric conductance β
siemens(S), units:A/V, base units:s^3-A^2/m^2-kg - magnetic flux β
weber(Wb), units:V-s, base units:m^2-kg/s^2-A - magnetic flux density β
tesla(T), units:Wb/m^2, base units:kg/s^2-A - inductance β
henry(H), units:Wb/A, base units:m^2-kg/s^2-A^2 - luminous flux β
lumen(lm), units: (none), base units:cd-sr - illuminance β
lux(lx), units:lm/m^2, base units:cd-sr/m^2 - activity (of a radionuclide) β
becquerel(Bq), units: (none), base units:/s - absorbed dose β
gray(Gy), units:J/kg, base units:m^2/s^2 - dose equivalent β
sievert(Sv), units:J/kg, base units:m^2/s^2
lowkey important: the SI unitkilogramsis actually implemented asgramsbecause1e-6 kilogram = 1 milligram, not1 microkilogram. Also, the abbreviation for Electric Resistance (ohms), which is the omega character, is not supported. sorry bestie, no omega here. π
and no, there's no support for Celsius or Farenheit temperature. π The units conversion routines can only scale values with multiplication and division, so you can't convert from thermodynamic temperature (kelvins) to absolute degrees Celsius or Farenheit. But conversion of thermodynamic quantities, like thermal expansion (per unit temperature), is easy to add to the units library. so it's not all bad. π«Ά
SI units can have a multiple or sub-multiple prefix. The prefix or its abbreviation goes before the unit, no spaces allowed. Compound prefixes? Not allowed. A prefix should never be used alone. These prefixes are defined in Table 5 of Special Publication 330. periodt. π
β¨ SI PREFIXES β¨
- yotta β abbreviation
Y, factor1e24 - zetta β abbreviation
Z, factor1e21 - exa β abbreviation
E, factor1e18 - peta β abbreviation
P, factor1e15 - tera β abbreviation
T, factor1e12 - giga β abbreviation
G, factor1e9 - mega β abbreviation
M, factor1e6 - kilo β abbreviation
k, factor1e3 - hecto β abbreviation
h, factor1e2 - deka β abbreviation
da, factor1e1 - deca β (no abbreviation), factor
1e1 - deci β abbreviation
d, factor1e-1 - centi β abbreviation
c, factor1e-2 - milli β abbreviation
m, factor1e-3 - micro β abbreviation
u, factor1e-6 - nano β abbreviation
n, factor1e-9 - pico β abbreviation
p, factor1e-12 - femto β abbreviation
f, factor1e-15 - atto β abbreviation
a, factor1e-18 - zepto β abbreviation
z, factor1e-21 - yocto β abbreviation
y, factor1e-24
we define the same prefix with both the USA (deka) and non-USA (deca) spellings. and we take the liberty of allowingmicroto be typed as auinstead of the Greek character mu. so you can just typeuand it's fine. iykyk. π
β¨ NON-SI UNITS β¨
so many non-SI units are used in apps and stuff. Appendix B.8 of NIST Special Publication 811 lists a ton of non-SI conversion factors. We can't include all possible unit definitions in this package. sometimes there are many different conversion factors for a given unit depending on context. (the appendix lists over 40 conversions for British thermal units! π€―) You can always add application specific conversions using the new command, but we've included some well known and often used ones here. π
- angstrom β (no abbreviation), base units
1.0E-10 m - astronomicalUnit β abbreviation
AU, base units1.495979E11 m - atmosphere β (no abbreviation), base units
1.01325E5 Pa - bar β (no abbreviation), base units
1.0E5 Pa - calorie β (no abbreviation), base units
4.1868 J - curie β (no abbreviation), base units
3.7E10 Bq - day β (no abbreviation), base units
8.64E4 s - degree β (no abbreviation), base units
1.745329E-2 rad - erg β (no abbreviation), base units
1.0E-7 J - faraday β (no abbreviation), base units
9.648531 C - fermi β (no abbreviation), base units
1.0E-15 m - foot β abbreviation
ft, base units3.048E-1 m - gauss β (no abbreviation), base units
1.0E-4 T - gilbert β (no abbreviation), base units
7.957747E-1 A - grain β abbreviation
gr, base units6.479891E-5 kg - hectare β abbreviation
ha, base units1.0E4 m^2 - hour β abbreviation
h, base units3.6E3 s - inch β abbreviation
in, base units2.54E-2 m - lightYear β (no abbreviation), base units
9.46073E15 m - liter β abbreviation
L, base units1.0E-3 m^3 - maxwell β abbreviation
Mx, base units1.0E-8 Wb - mho β (no abbreviation), base units
1.0 S - micron β (no abbreviation), base units
1.0E-6 m - mil β (no abbreviation), base units
2.54E-5 m - mile β abbreviation
mi, base units1.609344E3 m - minute β abbreviation
min, base units6.0E1 s - parsec β abbreviation
pc, base units3.085E16 m - pica β (no abbreviation), base units
4.233333E-3 m - pound β abbreviation
lb, base units4.535924E-1 kg - revolution β (no abbreviation), base units
6.283185 rad - revolutionPerMinute β abbreviation
rpm, base units1.047198E-1 rad/s - yard β abbreviation
yd, base units9.144E-1 m - year β (no abbreviation), base units
3.1536E7 s
β¨ QUANTITIES AND DERIVED UNITS WITH SPECIAL NAMES β¨
so this units conversion package is specifically limited to unit reduction, comparison, and scaling. It 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 make sure all length values properly reduce to meters, or that all velocity values properly reduce to meters/second. vibe check. β¨
A C implementation of this package has been created to work with the Simplified Wrapper Interface Generator (http://www.swig.org/). That package (units.i) uses 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 they're applied to any unit-less quantity strings. slay. π
A units system enhanced with quantity type checking might benefit from including other derived types 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 the implementation of this package are 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/. iconic. π
β¨ AUTHORS β¨
Robert W. Techentin β the legend. π
β¨ 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. bestie, we wanna hear from you. π«Ά
When proposing code changes, please provide unified diffs, i.e. the output of diff -u. no cap. π
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. iykyk. β¨
β¨ KEYWORDS β¨
angle, constants, conversion, distance, radians, unit
β¨ COPYRIGHT β¨
Copyright (c) 2000-2005 Mayo Foundation. π
and that's the tea, bestie! this page is from tcllib version 2.2.3, units(3tcl). stay slay, and remember: unit conversions are literally just fancy math. periodt. π β¨
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- 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