units.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <!DOCTYPE html
  2. PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  3. <HTML><HEAD><LINK rel="stylesheet" href="manpage.css"><TITLE>units</TITLE><META name="xsl-processor" content="SAXON 5.5 from Michael Kay of ICL"><META name="generator" content="$Id: units.html,v 1.1 2002/10/24 12:32:38 zahn Exp $"></HEAD><BODY><DIV class="header"><DIV class="navbar" align="center"><A HREF="#SECTb2ab3">NAME</A> &middot; <A HREF="#SECTb2ab5">SYNOPSIS</A> &middot; <A HREF="#SECTb2ab7">DESCRIPTION</A> &middot; <A HREF="#SECTb2ab9">COMMANDS</A> &middot; <A HREF="#SECTb2ac11">Unit String Format</A> &middot; <A HREF="#SECTb2ac13">SI UNITS</A> &middot; <A HREF="#SECTb2ac15">REFERENCES</A> &middot; <A HREF="#SECTb2ac17">KEYWORDS</A></DIV><HR class="navsep"></DIV><DIV class="body">
  4. <h2><a name="SECTb2ab3">NAME</a></h2><p class="namesection"><B class="names">units - </B><BR>Convert and manipulate quantities with units</p>
  5. <h2><a name="SECTb2ab5">SYNOPSIS</a></h2><PRE class="syntax">
  6. package require units ?1.0?
  7. units::convert <i class="m">value</i> <i class="m">targetUnits</i>
  8. units::reduce <i class="m">unitString</i>
  9. units::new <i class="m">name</i> <i class="m">baseUnits</i>
  10. </PRE>
  11. <h2><a name="SECTb2ab7">DESCRIPTION</a></h2><p>
  12. This library provides a conversion facility from a variety of
  13. scientific and engineering shorthand notations into floating point
  14. numbers. This allows application developers to easily convert
  15. values with different units into uniformly scaled numbers.
  16. </p><p>
  17. The units conversion facility is also able to convert between
  18. compatible units. If, for example, a application is expecting a value
  19. in <tt class="samp">ohms</tt> (Resistance), and the user specifies units of
  20. <tt class="samp">milliwebers/femtocoulomb</tt>, the conversion routine will
  21. handle it appropriately. An error will be generated if an incorrect
  22. conversion is attempted.
  23. </p><p>
  24. Values are scaled from one set of units to another by dimensional
  25. analysis. Both the value units and the target units are reduced into
  26. primitive units and a scale factor. Units are checked for
  27. compatibility, and the scale factors are applied by multiplication and
  28. division. This technique is extremely flexible and quite robust.
  29. </p><p>
  30. New units and new unit abbreviations can be defined in terms of
  31. existing units and abbreviations. It is also possible to define a new
  32. primitive unit, although that will probably be unnecessary. New units
  33. will most commonly be defined to accommodate non-SI measurement
  34. systems, such as defining the unit <tt class="samp">inch</tt> as <tt class="samp">"2.54
  35. cm"</tt>.
  36. </p>
  37. <h2><a name="SECTb2ab9">COMMANDS</a></h2><dl class="commandlist">
  38. <dt>units::convert <i class="m">value</i> <i class="m">targetUnits</i> </dt>
  39. <dd>
  40. Converts the value string into a floating point number, scaled to the
  41. specified <i class="m">targetUnits</i>. The <i class="m">value</i> string may contain a
  42. number and units. If units are specified, then they must be
  43. compatible with the <i class="m">targetUnits</i>. If units are not specified
  44. for the <i class="m">value</i>, then it will be scaled to the target units. For
  45. example, scaling <tt class="samp">"1.0"</tt> to <tt class="samp">"millimeter"</tt> would
  46. result in <tt class="samp">"1000.0"</tt>.
  47. </dd>
  48. <dt>units::reduce <i class="m">unitString</i> </dt>
  49. <dd>
  50. Returns a unit string consisting of a scale factor followed by a space
  51. separated list of sorted and reduced primitive units. The reduced
  52. unit string may include a forward-slash (separated from the
  53. surrounding primitive subunits by spaces) indicating that the
  54. remaining subunits are in the denominator. Generates an error if the
  55. <i class="m">unitString</i> is invalid.
  56. </dd>
  57. <dt>units::new <i class="m">name</i> <i class="m">baseUnits</i> </dt>
  58. <dd>
  59. Creates a new unit conversion with the specified name. The new unit
  60. <i class="m">name</i> must be only alphabetic (upper or lower case) letters.
  61. The <i class="m">baseUnits</i> string can consist of any valid units conversion
  62. string, including constant factors, numerator and denominator parts,
  63. units with prefixes, and exponents. The baseUnits may contain any
  64. number of subunits, but it must reduce to primitive units. BaseUnits
  65. could also be the string <tt class="samp">"-primitive"</tt> to represent a new
  66. kind of quantity which cannot be derived from other units. But you
  67. probably would not do that unless you have discovered some kind of new
  68. univeral property.
  69. </dd>
  70. </dl>
  71. <h2><a name="SECTb2ac11">Unit String Format</a></h2><p>
  72. Value and unit string format is quite flexible. It is possible to
  73. define virtually any combination of units, prefixes, and powers.
  74. Valid unit strings must conform to these rules.
  75. </p><ul>
  76. <li>
  77. A unit string consists of an optional scale factor followed by zero or
  78. more subunits. The scale factor must be a valid floating point
  79. number, and may or may not be separated from the subunits. The scale
  80. factor could be negative.
  81. </li>
  82. <li>
  83. Subunits are separated form each other by one or more separator
  84. characters, which are space (" "), hyphen ("-"), asterisk ("*"), and
  85. forward-slash ("/"). Sure, go ahead and complain about using a minus
  86. sign ("-") to represent multiplication. It just isn't sound
  87. mathematics, and, by rights, we should require everyone to use the
  88. asterisk ("*") to separate all units. But the bottom line is that
  89. complex unit strings like <tt class="samp">"m-kg/s^2"</tt> are pleasantly
  90. readable.
  91. </li>
  92. <li>
  93. The forward-slash seperator ("/") indicates that following subunits are
  94. in the denominator. There can be at most one forward-slash separator.
  95. </li>
  96. <li>
  97. Subunits can be floating point scale factors, but with the exception
  98. of the leading scale factor, they must be surrounded by valid
  99. separators. Subunit scale factors cannot be negative. (Remember that
  100. the hyphen is a unit separator.)
  101. </li>
  102. <li>
  103. Subunits can be valid units or abbreviations. They may include a
  104. prefix. They may include a plural suffix "s" or "es". They may also
  105. include a power string denoted by a circumflex ("^"), followed by a
  106. integer, after the unit name (or plural suffix, if there is one).
  107. Negative exponents are not allowed. (Remember that the hyphen is a
  108. unit separator.)
  109. </li>
  110. </ul>
  111. <h3><a name="SECTb2ac11b3"> Example valid unit strings </a></h3>
  112. <PRE class="example">
  113. Unit String Reduced Unit String
  114. ------------------------------------------------------------
  115. meter 1.0 meter
  116. kilometer 1000.0 meter
  117. km 1000.0 meter
  118. km/s 1000.0 meter / second
  119. /microsecond 1000000.0 / second
  120. /us 1000000.0 / second
  121. kg-m/s^2 1000.0 gram meter / second second
  122. 30second 30.0 second
  123. 30 second 30.0 second
  124. 30 seconds 30.0 second
  125. 200*meter/20.5*second 9.75609756098 meter / second
  126. </PRE>
  127. <h2><a name="SECTb2ac13">SI UNITS</a></h2><p>
  128. The standard SI units are predefined according to <em>NIST Special
  129. Publication 330</em>. Standard units for both SI Base Units (Table
  130. 1) and SI Derived Units with Special Names (Tables 3a and 3b) are
  131. included here for reference. Each standard unit name and abbreviation
  132. are included in this package.
  133. </p>
  134. <h3><a name="SECTb2ac13b2">SI Base Units</a></h3>
  135. <PRE class="example">
  136. Quantity Unit Name Abbr.
  137. ---------------------------------------------
  138. Length meter m
  139. Mass kilogram kg
  140. Time second s
  141. Current ampere A
  142. Temperature kelvin K
  143. Amount mole mol
  144. Luminous Intensity candela cd
  145. </PRE>
  146. <h3><a name="SECTb2ac13b3">SI Derived Units with Special Names</a></h3>
  147. <PRE class="example">
  148. Quantity Unit Name Abbr. Units Base Units
  149. --------------------------------------------------------------------
  150. plane angle radian rad m/m m/m
  151. solid angle steradian sr m^2/m^2 m^2/m^2
  152. frequency hertz Hz /s
  153. force newton N m-kg/s^2
  154. pressure pascal Pa N/m^2 kg/m-s^2
  155. energy, work joule J N-m m^2-kg/s^2
  156. power, radiant flux watt W J/s m^2-kg/s^3
  157. electric charge coulomb C s-A
  158. electric potential volt V W/A m^2-kg/s^3-A
  159. capacitance farad F C/V s^4-A^2/m^2-kg
  160. electric resistance ohm V/A m^2-kg/s^3-A^2
  161. electric conductance siemens S A/V s^3-A^2/m^2-kg
  162. magnetic flux weber Wb V-s m^2-kg/s^2-A
  163. magnetic flux density tesla T Wb/m^2 kg/s^2-A
  164. inductance henry H Wb/A m^2-kg/s^2-A^2
  165. luminous flux lumen lm cd-sr
  166. illuminance lux lx lm/m^2 cd-sr/m^2
  167. activity (of a
  168. radionuclide) bacquerel Bq /s
  169. absorbed dose gray Gy J/kg m^2/s^2
  170. dose equivalent sievert Sv J/kg m^2/s^2
  171. </PRE>
  172. <p>
  173. Note that the SI unit kilograms is actually implemented as grams
  174. because 1e-6 kilogram = 1 milligram, not 1 microkilogram. The
  175. abbreviation for Electric Resistance (ohms), which is the omega
  176. character, is not supported. Also note that there is no support for
  177. Celsius temperature. The units conversion routines can only scale
  178. values with multiplication and division, and the abbreviation "C"
  179. (which is the closest one could get to "degrees C" using the degrees
  180. symbol) is already used by Electric Potential.
  181. </p>
  182. <p>
  183. SI Units can have a multiple or sub-multiple prefix. The prefix or its
  184. abbreviation should appear before the unit, without spaces. Compound
  185. prefixes are not allowed, and a prefix should never be used alone.
  186. These prefixes are defined in Table 5 of <em>Special Publication
  187. 330</em>.
  188. </p>
  189. <h3><a name="SECTb2ac13b5">SI Prefixes</a></h3>
  190. <PRE class="example">
  191. Prefix Name Abbr. Factor
  192. ---------------------------------------
  193. yotta Y 1e24
  194. zetta Z 1e21
  195. exa E 1e18
  196. peta P 1e15
  197. tera T 1e12
  198. giga G 1e9
  199. mega M 1e6
  200. kilo k 1e3
  201. hecto h 1e2
  202. deka da 1e1
  203. deca 1e1
  204. deci d 1e-1
  205. centi c 1e-2
  206. milli m 1e-3
  207. micro u 1e-6
  208. nano n 1e-9
  209. pico p 1e-12
  210. femto f 1e-15
  211. atto a 1e-18
  212. zepto z 1e-21
  213. yocto y 1e-24
  214. </PRE>
  215. <p>
  216. Note that we define the same prefix with both the USA ("deka") and
  217. non-USA ("deca") spellings. Also note that we take the liberty of
  218. allowing "micro" to be typed as a "u" instead of the Greek character
  219. mu.
  220. </p><p>
  221. Many non-SI units are commonly used in applications. Appendix B.8 of
  222. <em>NIST Special Publication 811</em> lists many non-SI conversion
  223. factors. It is not possible to include all possible unit definitions
  224. in this package. In some cases, many different conversion factors
  225. exist for a given unit, depending on the context. (The appendix lists
  226. over 40 conversions for British thermal units!) Application specific
  227. conversions can always be added using the <b class="command">new</b>
  228. command, but some well known and often used conversions are included
  229. in this package.
  230. </p>
  231. <h3><a name="SECTb2ac13b8">Non-SI Units</a></h3>
  232. <PRE class="example">
  233. Unit Name Abbr. Base Units
  234. --------------------------------------------------
  235. angstrom 1.0E-10 m
  236. astronomicalUnit AU 1.495979E11 m
  237. atmosphere 1.01325E5 Pa
  238. bar 1.0E5 Pa
  239. calorie 4.1868 J
  240. curie 3.7E10 Bq
  241. day 8.64E4 s
  242. degree 1.745329E-2 rad
  243. erg 1.0E-7 J
  244. faraday 9.648531 C
  245. fermi 1.0E-15 m
  246. foot ft 3.048E-1 m
  247. gauss 1.0E-4 T
  248. gilbert 7.957747E-1 A
  249. grain gr 6.479891E-5 kg
  250. hectare ha 1.0E4 m^2
  251. hour h 3.6E3 s
  252. inch in 2.54E-2 m
  253. lightYear 9.46073E15 m
  254. liter L 1.0E-3 m^3
  255. maxwell Mx 1.0E-8 Wb
  256. mho 1.0 S
  257. micron 1.0E-6 m
  258. mil 2.54E-5 m
  259. mile mi 1.609344E3 m
  260. minute min 6.0E1 s
  261. parsec pc 3.085E16 m
  262. pica 4.233333E-3 m
  263. pound lb 4.535924E-1 kg
  264. revolution 6.283185 rad
  265. revolutionPerMinute rpm 1.047198E-1 rad/s
  266. yard 9.144E-1 m
  267. year yd 3.1536E7 s
  268. </PRE>
  269. <h3><a name="SECTb2ac13b9">Quantities and Derived Units with Special Names</a></h3>
  270. <p>
  271. This units conversion package is limited specifically to unit
  272. reduction, comparison, and scaling. This package does not consider
  273. any of the quantity names for either base or derived units. A similar
  274. implementation or an extension in a typed or object-oriented language
  275. might introduce user defined types for the quantities. Quantity type
  276. checking could be used, for example, to ensure that all
  277. <tt class="samp">"length"</tt> values properly reduced to <tt class="samp">"meters"</tt>, or
  278. that all <tt class="samp">"velocity"</tt> values properly reduced to
  279. <tt class="samp">"meters/second"</tt>.
  280. </p>
  281. <p>
  282. A C implementation of this package has been created to work in
  283. conjunction with the Simplified Wrapper Interface Generator
  284. (http://www.swig.org). That package (units.i) exploits SWIG's typemap
  285. system to automatically convert script quantity strings into floating
  286. point quantities. Function arguments are specified as quantity types
  287. (e.g., <tt class="samp">typedef float Length</tt>), and target units (expected
  288. by the C application code) are specified in an associative array.
  289. Default units are also defined for each quantity type, and are applied
  290. to any unit-less quantity strings.
  291. </p>
  292. <p>
  293. A units system enhanced with quantity type checking might benefit from
  294. inclusion of other derived types which are expressed in terms of
  295. special units, as illustrated in Table 2 of <em>NIST Publication
  296. 330</em>. The quantity <tt class="samp">"area"</tt>, for example, could be
  297. defined as units properly reducing to <tt class="samp">"meter^2"</tt>, although
  298. the utility of defining a unit named <tt class="samp">"square meter"</tt> is
  299. arguable.
  300. </p>
  301. <h2><a name="SECTb2ac15">REFERENCES</a></h2><p>
  302. The unit names, abbreviations, and conversion values are derived from
  303. those published by the United States Department of Commerce Technology
  304. Administration, National Institute of Standards and Technology (NIST)
  305. in <em>NIST Special Publication 330: The International System of
  306. Units (SI)</em> and <em>NIST Special Publication 811: Guide for
  307. the Use of the International System of Units (SI)</em>. Both of
  308. these publications are available (as of December 2000) from
  309. http://physics.nist.gov/cuu/Reference/contents.html
  310. </p><p>
  311. The ideas behind implementation of this package is based in part on
  312. code written in 1993 by Adrian Mariano which performed dimensional
  313. analysis of unit strings using fixed size tables of C structs.
  314. </p>
  315. <h2><a name="SECTb2ac17">KEYWORDS</a></h2><P class="keywords"><A class="keyword" href="keyword-index.html#KW-unit">unit</A>, <A class="keyword" href="keyword-index.html#KW-convert">convert</A></P>
  316. </DIV><DIV class="footer"><HR class="navsep"><!-- footer.html: Standard navigational footer --><div class="navbar" align="center">
  317. <a class="navaid" href="index.html">Table of Contents</a>
  318. &middot; <a class="navaid" href="command-index.html">Alphabetical Index</a>
  319. &middot; <a class="navaid" href="keyword-index.html">Keyword Index</a>
  320. &middot; <a class="navaid" href="category-index.html">Category Index</a>
  321. &middot; <a class="navaid" href="../About.html">About...</a>
  322. </div></DIV></BODY></HTML>