xref: /freebsd/usr.bin/units/units.1 (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
1.Dd March 17, 2020
2.Dt UNITS 1
3.Os
4.Sh NAME
5.Nm units
6.Nd conversion calculator
7.Sh SYNOPSIS
8.Nm
9.Op Fl ehqtUVv
10.Op Fl f Ar unitsfile
11.Op Fl o Ar format
12.Op Ar from to
13.Sh DESCRIPTION
14The
15.Nm
16program converts quantities expressed in various scales to
17their equivalents in other scales.
18It can only
19handle multiplicative or affine scale changes.
20.Pp
21.Nm
22can work interactively by prompting
23the user for input
24.Pq see Sx EXAMPLES
25or non-interactively, providing a conversion for given arguments
26.Ar from
27and
28.Ar to .
29.Pp
30The following options are available:
31.Bl -tag -width "from to"
32.It Fl e , Fl -exponential
33Same as
34.Fl o
35.Cm %6e
36(see the description of the
37.Fl o
38flag).
39.It Fl f Ar unitsfile , Fl -file Ar unitsfile
40Specify the name of the units data file to load.
41This option may be specified multiple times.
42.It Fl H Ar historyfile , Fl -history Ar historyfile
43Ignored, for compatibility with GNU units.
44.It Fl h , Fl -help
45Show an overview of options.
46.It Fl o Ar format , Fl -output-format Ar format
47Select the output format string by which numbers are printed.
48Defaults to
49.Dq Li %.8g .
50.It Fl q , Fl -quiet
51Suppress prompting of the user for units and the display of statistics
52about the number of units loaded.
53.It Fl t , Fl -terse
54Only print the result.
55This is used when calling
56.Nm
57from other programs for easy to parse results.
58.It Fl U , Fl -unitsfile
59Print the location of the default unit file if it exists.
60Otherwise, print
61.Dq Units data file not found .
62.It Fl V , Fl -version
63Print the version number
64.Pq which is fixed at Dq Fx units ,
65the path to the units data file and exit.
66.It Fl v , Fl -verbose
67Print the units in the conversion output.
68Be more verbose in general.
69.It Ar from to
70Allow a single unit conversion to be done directly from the command
71line.
72The program will not print prompts.
73It will print out the result of the single specified conversion.
74Both arguments, i.e.,
75.Ar from
76and
77.Ar to ,
78can be just a unit
79.Pq e.g., Dq Cm cm ,
80a quantity
81.Pq e.g., Dq Cm 42 ,
82or a quantity with a unit
83.Pq e.g., Dq Cm 42 cm
84.El
85.Ss Mathematical operators
86.Bl -dash -compact
87.It
88Powers of units can be specified using the
89.Dq Ic ^
90character as shown in the example, or by simple concatenation:
91.Dq Ic cm3
92is
93equivalent to
94.Dq Ic cm^3 .
95See the
96.Sx BUGS
97section
98for details on the limitations of exponent values.
99.It
100Multiplication of units can be specified by using spaces
101.Pq Dq " " ,
102a dash
103.Pq Dq -
104or an asterisk
105.Pq Dq * .
106.It
107Division of units is indicated by the slash
108.Pq Dq Ic / .
109.It
110Division of numbers
111must be indicated using the vertical bar
112.Pq Dq Ic \&| Ns .
113.El
114.Pp
115Note that multiplication has a higher precedence than division,
116so
117.Dq Ic m/s/s
118is the same as
119.Dq Ic m/s^2
120or
121.Dq Ic m/s s .
122.Ss Units
123The conversion information is read from a units data file.
124The default
125file includes definitions for most familiar units, abbreviations and
126metric prefixes.
127Some constants of nature included are:
128.Pp
129.Bl -column -offset indent -compact "mercury"
130.It "pi	ratio of circumference to diameter"
131.It "c	speed of light"
132.It "e	charge on an electron"
133.It "g	acceleration of gravity"
134.It "force	same as g"
135.It "mole	Avogadro's number"
136.It "water	pressure per unit height of water"
137.It "mercury	pressure per unit height of mercury"
138.It "au	astronomical unit"
139.El
140.Pp
141The unit
142.Dq Ic pound
143is a unit of mass.
144Compound names are run together
145so
146.Dq Ic pound force
147is a unit of force.
148The unit
149.Dq Ic ounce
150is also a unit of mass.
151The fluid ounce is
152.Dq Ic floz .
153British units that differ from
154their US counterparts are prefixed with
155.Dq br ,
156and currency is prefixed with its country name:
157.Dq Ic belgiumfranc ,
158.Dq Ic britainpound .
159When searching
160for a unit, if the specified string does not appear exactly as a unit
161name, then
162.Nm
163will try to remove a trailing
164.Dq s
165or a trailing
166.Dq es
167and check again for a match.
168.Ss Units file format
169To find out what units are available read the standard units file.
170If you want to add your own units you can supply your own file.
171A unit is specified on a single line by
172giving its name and an equivalence.
173Be careful to define
174new units in terms of old ones so that a reduction leads to the
175primitive units which are marked with
176.Dq \&!
177characters.
178The
179.Nm
180program will not detect infinite loops that could be caused
181by careless unit definitions.
182Comments in the unit definition file
183begin with a
184.Dq #
185or
186.Dq /
187character at the beginning of a line.
188.Pp
189Prefixes are defined in the same way as standard units, but with
190a trailing dash
191.Pq Dq -
192at the end of the prefix name.
193If a unit is not found
194even after removing trailing
195.Dq s
196or
197.Dq es ,
198then it will be checked against the list of prefixes.
199Prefixes will be removed until a legal
200base unit is identified.
201.Sh ENVIRONMENT
202.Bl -tag -width PATH
203.It Ev PATH
204The colon-separated list of root directories at which
205.Nm
206tries to find
207.Pa /usr/share/misc/definitions.units .
208.Pp
209For example if
210.Ev PATH
211is set to
212.Dq Li /tmp:/:/usr/local ,
213no
214.Fl f
215flags are provided, and
216.Pa /usr/share/misc/definitions.units
217is missing then
218.Nm
219tries to open the following files as the default units file:
220.Pa /tmp/usr/share/misc/definitions.units ,
221.Pa /usr/share/misc/definitions.units ,
222and
223.Pa /usr/local/usr/share/misc/definitions.units .
224.El
225.Sh FILES
226.Bl -tag -width /usr/share/misc/definitions.units -compact
227.It Pa /usr/share/misc/definitions.units
228The standard units file.
229.El
230.Sh EXIT STATUS
231.Ex -std
232.Sh EXAMPLES
233.Bl -tag -width 0n
234.It Sy Example 1 : No Simple conversion of units
235.Pp
236This example shows how to do simple conversions, for example from gigabytes
237to bytes:
238.Bd -literal -offset 2n
239.Li $ Ic units -o %0.f -t '4 gigabytes' bytes
2404294967296
241.Ed
242.Pp
243The
244.Fl o Cm %0.f
245part of the command is required to print the result in a non-scientific
246notation (e.g, 4294967296 instead of 4.29497e+09).
247.It Sy Example 2 : No Interactive usage
248.Pp
249Here is an example of an interactive session where the user is prompted for
250units:
251.Bd -literal -offset 2n
252.Li You have : Ic meters
253.Li You want : Ic feet
254        * 3.2808399
255        / 0.3048
256
257.Li You have : Ic cm^3
258.Li You want : Ic gallons
259        * 0.00026417205
260        / 3785.4118
261
262.Li You have : Ic meters/s
263.Li You want : Ic furlongs/fortnight
264        * 6012.8848
265        / 0.00016630952
266
267.Li You have : Ic 1|2 inch
268.Li You want : Ic cm
269        * 1.27
270        / 0.78740157
271
272.Li You have : Ic 85 degF
273.Li You want : Ic degC
274        29.444444
275.Ed
276.It Sy Example 3 : No Difference between Do Ic \&| Dc No and Do Ic / Dc No division
277.Pp
278The following command shows how to convert half a meter to centimeters.
279.Bd -literal -offset 2n
280.Li $ Ic units '1|2 meter' cm
281        * 50
282        / 0.02
283.Ed
284.Pp
285.Nm
286prints the expected result because the division operator for numbers
287.Pq Dq Li \&|
288was used.
289.Pp
290Using the division operator for units
291.Pq Dq Li /
292would result in an error:
293.Bd -literal -offset 2n
294.Li $ Ic units '1/2 meter' cm
295conformability error
296        0.5 / m
297        0.01 m
298.Ed
299.Pp
300It is because
301.Nm
302interprets
303.Dq Ic 1/2 meter
304as
305.Dq Ic 0.5/meter ,
306which is not conformable to
307.Dq Ic cm .
308.It Sy Example 4 : No Simple units file
309Here is an example of a short units file that defines some basic
310units:
311.Pp
312.Bl -column -offset 2n -compact "minute"
313.It "m	!a!"
314.It "sec	!b!"
315.It "micro-	1e-6"
316.It "minute	60 sec"
317.It "hour	60 min"
318.It "inch	0.0254 m"
319.It "ft	12 inches"
320.It "mile	5280 ft"
321.El
322.It Sy Example 5 : No Viewing units and conversions of the default units file
323The following shell one-liner allows the user to view the contents of the
324default units file:
325.Bd -literal -offset 2n
326.Li $ Ic less \&"$(units -U)"
327.Ed
328.El
329.Sh DIAGNOSTICS
330.Bl -diag
331.It can't find units file '%s'
332The default units file is not in its default location
333.Pq see Sx FILES
334and it is not present in any file tree starting with their roots at directories from
335.Ev PATH
336.Pq see Sx ENVIRONMENT .
337.It cap_rights_limit\&() failed
338See
339.Xr capsicum  4 .
340.It conformability error
341It is not possible to reduce the given units to one common unit:
342they are not conformable.
343Instead of a conversion,
344.Nm
345will display the reduced form for each provided unit:
346.Bd -literal -offset 2n
347.Li You have : Ic ergs/hour
348.Li You want : Ic fathoms kg^2 / day
349conformability error
350        2.7777778e-11 kg m^2 / sec^3
351        2.1166667e-05 kg^2 m / sec
352.Ed
353.It Could not initialize history
354See
355.Xr editline 3 .
356.It dupstr
357.Xr strdup 3
358failed.
359.It memory for prefixes exceeded in line %d
360Over 100 prefixes were defined.
361.It memory for units exceeded in line %d
362Over 1000 prefixes were defined.
363.It memory overflow in unit reduction
364The requested conversion involves too many units
365.Pq see Sx BUGS .
366.It redefinition of prefix '%s' on line %d ignored
367.No ""
368.It redefinition of unit '%s' on line %d ignored
369.No ""
370.It unexpected end of prefix on line %d
371.No ""
372.It unexpected end of unit on line %d
373.No ""
374.It Units data file not found
375The default units file is missing.
376.It unable to enter capability mode
377See
378.Xr capsicum  4 .
379.It unable to open units file '%s'
380One of the user-specified units files cannot be opened.
381.It unit reduces to zero
382.No ""
383.It unknown unit '%s'
384The provided unit cannot be found in the units file.
385.It WARNING: conversion of non-proportional quantities.
386.Nm
387may fail to convert
388.Ar from
389to
390.Ar to
391because the units are not proportional.
392The warning is printed when a quantity is a part of the
393.Ar to
394argument.
395It can be illustrated on an example of conversion from Fahrenheit to Celsius:
396.Bd -literal -offset 2n
397.Li $ Ic units \&"degF" \&"degC"
398         (-> x*0.55555556g -17.777778g)
399         (<- y*1.8g 32g)
400.Li $ Ic units \&"degF" \&"1 degC"
401WARNING: conversion of non-proportional quantities.
402         (-> x*0.55555556g -17.777778g)
403         (<- y*1.8g 32g)
404.Li $ Ic units \&"1 degF" \&"1 degC"
405WARNING: conversion of non-proportional quantities.
406        -17.222222
407.Ed
408.El
409.Sh SEE ALSO
410.Xr bc 1
411.Sh HISTORY
412The
413.Nm
414first appeared in
415.Nx
416and was ported to
417.Fx 2.2.0 .
418.Pp
419The manual page was significantly rewritten in
420.Fx 13.0
421by
422.An Mateusz Piotrowski Aq Mt 0mp@FreeBSD.org .
423.Sh AUTHORS
424.An Adrian Mariano Aq Mt adrian@cam.cornell.edu
425.Sh BUGS
426The effect of including a
427.Dq /
428in a prefix is surprising.
429.Pp
430Exponents entered by the user can be only one digit.
431You can work around this by multiplying several terms.
432.Pp
433The user must use
434.Dq Ic \&|
435to indicate division of numbers and
436.Dq Ic /
437to indicate division of symbols.
438This distinction should not
439be necessary.
440.Pp
441The program contains various arbitrary limits on the length
442of the units converted and on the length of the data file.
443.Pp
444The program should use a hash table to store units so that
445it does not take so long to load the units list and check
446for duplication.
447.Pp
448It is not possible to convert a negative value.
449.Pp
450The
451.Nm
452program
453does not handle reductions of long lists of units very well:
454.Bd -literal -offset 2n
455.Li $ Ic units \&"$(yes m | head -n 154)" \&"$(yes cm | head -n 154)"
456        * 1e+308
457        / 1e-308
458.Li $ Ic units \&"$(yes m | head -n 333)" \&"$(yes cm | head -n 333)"
459        * inf
460        / 0
461.Li $ Ic units \&"$(yes m | head -n 500)" \&"$(yes cm | head -n 500)"
462units: memory overflow in unit reduction
463conformability error
464        1 m^500
465        1 centi cm^499
466.Li $ Ic units \&"$(yes m | head -n 501)" \&"$(yes cm | head -n 501)"
467units: memory overflow in unit reduction
468units: memory overflow in unit reduction
469units: memory overflow in unit reduction
470conformability error
471        1 m^500
472        1 centi cm^499
473.Ed
474