1 ;;; calc-units.el --- unit conversion functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is autoloaded from calc-ext.el.
37 ;;; Units table last updated 9-Jan-91 by Ulrich Mueller (ulm@vsnhd1.cern.ch)
38 ;;; with some additions by Przemek Klosowski (przemek@rrdstrad.nist.gov)
39 ;;; Updated April 2002 by Jochen Küpper
41 ;;; Updated August 2007, using
42 ;;; CODATA (http://physics.nist.gov/cuu/Constants/index.html)
43 ;;; NIST (http://physics.nist.gov/Pubs/SP811/appenB9.html)
44 ;;; ESUWM (Encyclopaedia of Scientific Units, Weights and
45 ;;; Measures, by François Cardarelli)
46 ;;; All conversions are exact unless otherwise noted.
48 (defvar math-standard-units
51 ( in
"254*10^(-2) cm" "Inch" nil
55 ( mi
"5280 ft" "Mile" )
56 ( au
"149597870691. m" "Astronomical Unit" nil
58 ;; (approx) NASA JPL (http://neo.jpl.nasa.gov/glossary/au.html)
59 ( lyr
"c yr" "Light Year" )
60 ( pc
"3.0856775854*10^16 m" "Parsec" nil
61 "3.0856775854 10^16 m (*)") ;; (approx) ESUWM
62 ( nmi
"1852 m" "Nautical Mile" )
63 ( fath
"6 ft" "Fathom" )
64 ( fur
"660 ft" "Furlong")
65 ( mu
"1 um" "Micron" )
66 ( mil
"(1/1000) in" "Mil" )
67 ( point
"(1/72) in" "Point (1/72 inch)" )
68 ( Ang
"10^(-10) m" "Angstrom" )
69 ( mfi
"mi+ft+in" "Miles + feet + inches" )
71 ( texpt
"(100/7227) in" "Point (TeX conventions)" )
72 ( texpc
"12 texpt" "Pica" )
73 ( texbp
"point" "Big point (TeX conventions)" )
74 ( texdd
"(1238/1157) texpt" "Didot point" )
75 ( texcc
"12 texdd" "Cicero" )
76 ( texsp
"(1/65536) texpt" "Scaled TeX point" )
79 ( hect
"10000 m^2" "*Hectare" )
81 ( acre
"(1/640) mi^2" "Acre" )
82 ( b
"10^(-28) m^2" "Barn" )
85 ( L
"10^(-3) m^3" "*Liter" )
87 ( gal
"4 qt" "US Gallon" )
90 ( cup
"8 ozfl" "Cup" )
91 ( ozfl
"2 tbsp" "Fluid Ounce" )
92 ( floz
"2 tbsp" "Fluid Ounce" )
93 ( tbsp
"3 tsp" "Tablespoon" )
94 ;; ESUWM defines a US gallon as 231 in^3.
95 ;; That gives the following exact value for tsp.
96 ( tsp
"492892159375*10^(-11) ml" "Teaspoon" nil
98 ( vol
"tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" nil
99 "tsp+tbsp+ozfl+cup+pt+qt+gal")
100 ( galC
"galUK" "Canadian Gallon" )
101 ( galUK
"454609*10^(-5) L" "UK Gallon" nil
107 ( min
"60 s" "Minute" )
108 ( hr
"60 min" "Hour" )
109 ( day
"24 hr" "Day" )
110 ( wk
"7 day" "Week" )
111 ( hms
"wk+day+hr+min+s" "Hours, minutes, seconds" )
112 ( yr
"36525*10^(-2) day" "Year (Julian)" nil
117 ( mph
"mi/hr" "*Miles per hour" )
118 ( kph
"km/hr" "Kilometers per hour" )
119 ( knot
"nmi/hr" "Knot" )
120 ( c
"299792458 m/s" "Speed of light" ) ;;; CODATA
123 ( ga
"980665*10^(-5) m/s^2" "*\"g\" acceleration" nil
124 "9.80665 m / s^2") ;; CODATA
128 ( lb
"16 oz" "Pound (mass)" )
129 ( oz
"28349523125*10^(-9) g" "Ounce (mass)" nil
130 "28.349523125 g") ;; ESUWM
131 ( ton
"2000 lb" "Ton" )
132 ( tpo
"ton+lb+oz" "Tons + pounds + ounces (mass)" )
133 ( t
"1000 kg" "Metric ton" )
134 ( tonUK
"10160469088*10^(-7) kg" "UK ton" nil
135 "1016.0469088 kg") ;; ESUWM
136 ( lbt
"12 ozt" "Troy pound" )
137 ( ozt
"311034768*10^(-7) g" "Troy ounce" nil
138 "31.10347680 g") ;; ESUWM, 1/12 exact value for lbt
139 ( ct
"(2/10) g" "Carat" nil
141 ( u
"1.660538782*10^(-27) kg" "Unified atomic mass" nil
142 "1.660538782 10^-27 kg (*)");;(approx) CODATA
145 ( N
"m kg/s^2" "*Newton" )
146 ( dyn
"10^(-5) N" "Dyne" )
147 ( gf
"ga g" "Gram (force)" )
148 ( lbf
"ga lb" "Pound (force)" )
149 ( kip
"1000 lbf" "Kilopound (force)" )
150 ( pdl
"138254954376*10^(-12) N" "Poundal" nil
151 "0.138254954376 N") ;; ESUWM
155 ( erg
"10^(-7) J" "Erg" )
156 ( cal
"41868*10^(-4) J" "International Table Calorie" nil
158 ( calth
"4184*10^(-3) J" "Thermochemical Calorie" nil
160 ( Cal
"1000 cal" "Large Calorie")
161 ( Btu
"105505585262*10^(-8) J" "International Table Btu" nil
162 "1055.05585262 J") ;; ESUWM
163 ( eV
"ech V" "Electron volt" )
164 ( ev
"eV" "Electron volt" )
165 ( therm
"105506000 J" "EEC therm" )
166 ( invcm
"h c/cm" "Energy in inverse centimeters" )
167 ( Kayser
"invcm" "Kayser (inverse centimeter energy)" )
168 ( men
"100/invcm" "Inverse energy in meters" )
169 ( Hzen
"h Hz" "Energy in Hertz")
170 ( Ken
"k K" "Energy in Kelvins")
171 ( Wh
"W hr" "Watt hour")
172 ( Ws
"W s" "Watt second")
176 ( hp
"550 ft lbf/s" "Horsepower") ;;ESUWM
177 ( hpm
"75 m kgf/s" "Metric Horsepower") ;;ESUWM
180 ( K nil
"*Degree Kelvin" K
)
181 ( dK
"K" "Degree Kelvin" K
)
182 ( degK
"K" "Degree Kelvin" K
)
183 ( dC
"K" "Degree Celsius" C
)
184 ( degC
"K" "Degree Celsius" C
)
185 ( dF
"(5/9) K" "Degree Fahrenheit" F
)
186 ( degF
"(5/9) K" "Degree Fahrenheit" F
)
189 ( Pa
"N/m^2" "*Pascal" )
190 ( bar
"10^5 Pa" "Bar" )
191 ( atm
"101325 Pa" "Standard atmosphere" ) ;; CODATA
192 ( Torr
"(1/760) atm" "Torr")
193 ( mHg
"1000 Torr" "Meter of mercury" )
194 ( inHg
"254*10^(-1) mmHg" "Inch of mercury" nil
196 ( inH2O
"2.490889*10^2 Pa" "Inch of water" nil
197 "2.490889 10^2 Pa (*)") ;;(approx) NIST
198 ( psi
"lbf/in^2" "Pounds per square inch" )
201 ( P
"(1/10) Pa s" "*Poise" )
202 ( St
"10^(-4) m^2/s" "Stokes" )
206 ( C
"A s" "Coulomb" )
207 ( Fdy
"ech Nav" "Faraday" )
208 ( e
"ech" "Elementary charge" )
209 ( ech
"1.602176487*10^(-19) C" "Elementary charge" nil
210 "1.602176487 10^-19 C (*)") ;;(approx) CODATA
215 ( S
"A/V" "Siemens" )
218 ( T
"Wb/m^2" "Tesla" )
219 ( Gs
"10^(-4) T" "Gauss" )
222 ;; Luminous intensity
223 ( cd nil
"*Candela" )
224 ( sb
"10000 cd/m^2" "Stilb" )
225 ( lm
"cd sr" "Lumen" )
226 ( lx
"lm/m^2" "Lux" )
227 ( ph
"10000 lx" "Phot" )
228 ( fc
"lm/ft^2" "Footcandle") ;; ESUWM
229 ( lam
"10000 lm/m^2" "Lambert" )
230 ( flam
"(1/pi) cd/ft^2" "Footlambert") ;; ESUWM
233 ( Bq
"1/s" "*Becquerel" )
234 ( Ci
"37*10^9 Bq" "Curie" ) ;; ESUWM
236 ( Sv
"Gy" "Sievert" )
237 ( R
"258*10^(-6) C/kg" "Roentgen" ) ;; NIST
238 ( rd
"(1/100) Gy" "Rad" )
241 ;; Amount of substance
245 ( rad nil
"*Radian" )
246 ( circ
"2 pi rad" "Full circle" )
247 ( rev
"circ" "Full revolution" )
248 ( deg
"circ/360" "Degree" )
249 ( arcmin
"deg/60" "Arc minute" )
250 ( arcsec
"arcmin/60" "Arc second" )
251 ( grad
"circ/400" "Grade" )
252 ( rpm
"rev/min" "Revolutions per minute" )
255 ( sr nil
"*Steradian" )
257 ;; Other physical quantities
258 ;; The values are from CODATA, and are approximate.
259 ( h
"6.62606896*10^(-34) J s" "*Planck's constant" nil
260 "6.62606896 10^-34 J s (*)")
261 ( hbar
"h / (2 pi)" "Planck's constant" ) ;; Exact
262 ( mu0
"4 pi 10^(-7) H/m" "Permeability of vacuum") ;; Exact
263 ( μ
0 "mu0" "Permeability of vacuum") ;; Exact
264 ( eps0
"1 / (mu0 c^2)" "Permittivity of vacuum" )
265 ( ε
0 "eps0" "Permittivity of vacuum" )
266 ( G
"6.67428*10^(-11) m^3/(kg s^2)" "Gravitational constant" nil
267 "6.67428 10^-11 m^3/(kg s^2) (*)")
268 ( Nav
"6.02214179*10^(23) / mol" "Avogadro's constant" nil
269 "6.02214179 10^23 / mol (*)")
270 ( me
"9.10938215*10^(-31) kg" "Electron rest mass" nil
271 "9.10938215 10^-31 kg (*)")
272 ( mp
"1.672621637*10^(-27) kg" "Proton rest mass" nil
273 "1.672621637 10^-27 kg (*)")
274 ( mn
"1.674927211*10^(-27) kg" "Neutron rest mass" nil
275 "1.674927211 10^-27 kg (*)")
276 ( mmu
"1.88353130*10^(-28) kg" "Muon rest mass" nil
277 "1.88353130 10^-28 kg (*)")
278 ( mμ
"mmu" "Muon rest mass" nil
279 "1.88353130 10^-28 kg (*)")
280 ( Ryd
"10973731.568527 /m" "Rydberg's constant" nil
281 "10973731.568527 /m (*)")
282 ( k
"1.3806504*10^(-23) J/K" "Boltzmann's constant" nil
283 "1.3806504 10^-23 J/K (*)")
284 ( alpha
"7.2973525376*10^(-3)" "Fine structure constant" nil
285 "7.2973525376 10^-3 (*)")
286 ( α
"alpha" "Fine structure constant" nil
287 "7.2973525376 10^-3 (*)")
288 ( muB
"927.400915*10^(-26) J/T" "Bohr magneton" nil
289 "927.400915 10^-26 J/T (*)")
290 ( muN
"5.05078324*10^(-27) J/T" "Nuclear magneton" nil
291 "5.05078324 10^-27 J/T (*)")
292 ( mue
"-928.476377*10^(-26) J/T" "Electron magnetic moment" nil
293 "-928.476377 10^-26 J/T (*)")
294 ( mup
"1.410606662*10^(-26) J/T" "Proton magnetic moment" nil
295 "1.410606662 10^-26 J/T (*)")
296 ( R0
"8.314472 J/(mol K)" "Molar gas constant" nil
297 "8.314472 J/(mol K) (*)")
298 ( V0
"22.710981*10^(-3) m^3/mol" "Standard volume of ideal gas" nil
299 "22.710981 10^-3 m^3/mol (*)")))
302 (defvar math-additional-units nil
303 "*Additional units table for user-defined units.
304 Must be formatted like `math-standard-units'.
305 If you change this, be sure to set `math-units-table' to nil to ensure
306 that the combined units table will be rebuilt.")
308 (defvar math-unit-prefixes
309 '( ( ?Y
(^
10 24) "Yotta" )
310 ( ?Z
(^
10 21) "Zetta" )
311 ( ?E
(^
10 18) "Exa" )
312 ( ?P
(^
10 15) "Peta" )
313 ( ?T
(^
10 12) "Tera" )
314 ( ?G
(^
10 9) "Giga" )
315 ( ?M
(^
10 6) "Mega" )
316 ( ?k
(^
10 3) "Kilo" )
317 ( ?K
(^
10 3) "Kilo" )
318 ( ?h
(^
10 2) "Hecto" )
319 ( ?H
(^
10 2) "Hecto" )
320 ( ?D
(^
10 1) "Deka" )
322 ( ?d
(^
10 -
1) "Deci" )
323 ( ?c
(^
10 -
2) "Centi" )
324 ( ?m
(^
10 -
3) "Milli" )
325 ( ?u
(^
10 -
6) "Micro" )
326 ( ?μ
(^
10 -
6) "Micro" )
327 ( ?n
(^
10 -
9) "Nano" )
328 ( ?p
(^
10 -
12) "Pico" )
329 ( ?f
(^
10 -
15) "Femto" )
330 ( ?a
(^
10 -
18) "Atto" )
331 ( ?z
(^
10 -
21) "zepto" )
332 ( ?y
(^
10 -
24) "yocto" )))
334 (defvar math-standard-units-systems
336 ( si
( ( g
'(/ (var kg var-kg
) 1000) ) ) )
337 ( mks
( ( g
'(/ (var kg var-kg
) 1000) ) ) )
338 ( cgs
( ( m
'(* (var cm var-cm
) 100 ) ) ) )))
340 (defvar math-units-table nil
341 "Internal units table.
342 Derived from `math-standard-units' and `math-additional-units'.
343 Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
345 (defvar math-units-table-buffer-valid nil
)
349 (defun calc-base-units ()
352 (let ((calc-autorange-units nil
))
353 (calc-enter-result 1 "bsun" (math-simplify-units
354 (math-to-standard-units (calc-top-n 1)
357 (defun calc-quick-units ()
360 (let* ((num (- last-command-event ?
0))
361 (pos (if (= num
0) 10 num
))
362 (units (calc-var-value 'var-Units
))
363 (expr (calc-top-n 1)))
364 (unless (and (>= num
0) (<= num
9))
365 (error "Bad unit number"))
366 (unless (math-vectorp units
)
367 (error "No \"quick units\" are defined"))
368 (unless (< pos
(length units
))
369 (error "Unit number %d not defined" pos
))
370 (if (math-units-in-expr-p expr nil
)
371 (calc-enter-result 1 (format "cun%d" num
)
372 (math-convert-units expr
(nth pos units
)))
373 (calc-enter-result 1 (format "*un%d" num
)
375 (math-mul expr
(nth pos units
))))))))
377 (defun math-get-standard-units (expr)
378 "Return the standard units in EXPR."
381 (math-to-standard-units expr nil
))))
383 (defun math-get-units (expr)
384 "Return the units in EXPR."
386 (math-extract-units expr
)))
388 (defun math-make-unit-string (expr)
389 "Return EXPR in string form.
390 If EXPR is nil, return nil."
392 (let ((cexpr (math-compose-expr expr
0)))
393 (replace-regexp-in-string
397 (math-composition-to-string cexpr
))))))
399 (defvar math-default-units-table
400 (make-hash-table :test
'equal
)
401 "A table storing previously converted units.")
403 (defun math-get-default-units (expr)
404 "Get default units to use when converting the units in EXPR."
405 (let* ((units (math-get-units expr
))
406 (standard-units (math-get-standard-units expr
))
407 (default-units (gethash
409 math-default-units-table
)))
410 (if (equal units
(car default-units
))
411 (math-make-unit-string (cadr default-units
))
412 (math-make-unit-string (car default-units
)))))
414 (defun math-put-default-units (expr)
415 "Put the units in EXPR in the default units table."
416 (let* ((units (math-get-units expr
))
417 (standard-units (math-get-standard-units expr
))
418 (default-units (gethash
420 math-default-units-table
)))
423 (puthash standard-units
(list units
) math-default-units-table
))
424 ((not (equal units
(car default-units
)))
425 (puthash standard-units
426 (list units
(car default-units
))
427 math-default-units-table
)))))
430 (defun calc-convert-units (&optional old-units new-units
)
433 (let ((expr (calc-top-n 1))
438 (unless (math-units-in-expr-p expr t
)
439 (let ((uold (or old-units
441 (setq uoldname
(read-string "Old units: "))
442 (if (equal uoldname
"")
446 (if (string-match "\\` */" uoldname
)
447 (setq uoldname
(concat "1" uoldname
)))
448 (math-read-expr uoldname
))))))
449 (when (eq (car-safe uold
) 'error
)
450 (error "Bad format in units expression: %s" (nth 1 uold
)))
451 (setq expr
(math-mul expr uold
))))
453 (setq defunits
(math-get-default-units expr
))
457 (concat "Old units: "
469 (string= new-units
"")
471 (setq new-units defunits
)))
472 (when (string-match "\\` */" new-units
)
473 (setq new-units
(concat "1" new-units
)))
474 (setq units
(math-read-expr new-units
))
475 (when (eq (car-safe units
) 'error
)
476 (error "Bad format in units expression: %s" (nth 2 units
)))
477 (math-put-default-units units
)
478 (let ((unew (math-units-in-expr-p units t
))
479 (std (and (eq (car-safe units
) 'var
)
480 (assq (nth 1 units
) math-standard-units-systems
))))
482 (calc-enter-result 1 "cvun" (math-simplify-units
483 (math-to-standard-units expr
486 (error "No units specified"))
487 (calc-enter-result 1 "cvun"
490 (and uoldname
(not (equal uoldname
"1"))))))))))
492 (defun calc-autorange-units (arg)
495 (calc-change-mode 'calc-autorange-units arg nil t
)
496 (message (if calc-autorange-units
497 "Adjusting target unit prefix automatically"
498 "Using target units exactly"))))
500 (defun calc-convert-temperature (&optional old-units new-units
)
503 (let ((expr (calc-top-n 1))
508 (setq uold
(or old-units
509 (let ((units (math-single-units-in-expr-p expr
)))
512 (list 'var
(car units
)
513 (intern (concat "var-"
516 (error "Not a pure temperature expression"))
518 (setq uoldname
(read-string
519 "Old temperature units: ")))))))
520 (when (eq (car-safe uold
) 'error
)
521 (error "Bad format in units expression: %s" (nth 2 uold
)))
522 (or (math-units-in-expr-p expr nil
)
523 (setq expr
(math-mul expr uold
)))
524 (setq defunits
(math-get-default-units expr
))
525 (setq unew
(or new-units
529 (concat "Old temperature units: "
532 "New temperature units")
538 (setq unew
(math-read-expr (if (string= unew
"") defunits unew
)))
539 (when (eq (car-safe unew
) 'error
)
540 (error "Bad format in units expression: %s" (nth 2 unew
)))
541 (math-put-default-units unew
)
542 (let ((ntemp (calc-normalize
544 (math-convert-temperature expr uold unew
546 (if (Math-zerop ntemp
)
547 (setq ntemp
(list '* ntemp unew
)))
548 (let ((calc-simplify-mode 'none
))
549 (calc-enter-result 1 "cvtm" ntemp
))))))
551 (defun calc-remove-units ()
554 (calc-enter-result 1 "rmun" (math-simplify-units
555 (math-remove-units (calc-top-n 1))))))
557 (defun calc-extract-units ()
560 (calc-enter-result 1 "rmun" (math-simplify-units
561 (math-extract-units (calc-top-n 1))))))
563 ;; The variables calc-num-units and calc-den-units are local to
564 ;; calc-explain-units, but are used by calc-explain-units-rec,
565 ;; which is called by calc-explain-units.
566 (defvar calc-num-units
)
567 (defvar calc-den-units
)
569 (defun calc-explain-units ()
572 (let ((calc-num-units nil
)
573 (calc-den-units nil
))
574 (calc-explain-units-rec (calc-top-n 1) 1)
575 (and calc-den-units
(string-match "^[^(].* .*[^)]$" calc-den-units
)
576 (setq calc-den-units
(concat "(" calc-den-units
")")))
579 (message "%s per %s" calc-num-units calc-den-units
)
580 (message "%s" calc-num-units
))
582 (message "1 per %s" calc-den-units
)
583 (message "No units in expression"))))))
585 (defun calc-explain-units-rec (expr pow
)
586 (let ((u (math-check-unit-name expr
))
588 (if (and u
(not (math-zerop pow
)))
589 (let ((name (or (nth 2 u
) (symbol-name (car u
)))))
590 (if (eq (aref name
0) ?\
*)
591 (setq name
(substring name
1)))
592 (if (string-match "[^a-zA-Zα-ωΑ-Ω0-9']" name
)
593 (if (string-match "^[a-zA-Zα-ωΑ-Ω0-9' ()]*$" name
)
594 (while (setq pos
(string-match "[ ()]" name
))
595 (setq name
(concat (substring name
0 pos
)
596 (if (eq (aref name pos
) 32) "-" "")
597 (substring name
(1+ pos
)))))
598 (setq name
(concat "(" name
")"))))
599 (or (eq (nth 1 expr
) (car u
))
600 (setq name
(concat (nth 2 (assq (aref (symbol-name
603 (if (and (string-match "[^a-zA-Zα-ωΑ-Ω0-9']" name
)
604 (not (memq (car u
) '(mHg gf
))))
607 (cond ((or (math-equal-int pow
1)
608 (math-equal-int pow -
1)))
609 ((or (math-equal-int pow
2)
610 (math-equal-int pow -
2))
611 (if (equal (nth 4 u
) '((m .
1)))
612 (setq name
(concat "Square-" name
))
613 (setq name
(concat name
"-squared"))))
614 ((or (math-equal-int pow
3)
615 (math-equal-int pow -
3))
616 (if (equal (nth 4 u
) '((m .
1)))
617 (setq name
(concat "Cubic-" name
))
618 (setq name
(concat name
"-cubed"))))
620 (setq name
(concat name
"^"
621 (math-format-number (math-abs pow
))))))
623 (setq calc-num-units
(if calc-num-units
624 (concat calc-num-units
" " name
)
626 (setq calc-den-units
(if calc-den-units
627 (concat calc-den-units
" " name
)
629 (cond ((eq (car-safe expr
) '*)
630 (calc-explain-units-rec (nth 1 expr
) pow
)
631 (calc-explain-units-rec (nth 2 expr
) pow
))
632 ((eq (car-safe expr
) '/)
633 (calc-explain-units-rec (nth 1 expr
) pow
)
634 (calc-explain-units-rec (nth 2 expr
) (- pow
)))
635 ((memq (car-safe expr
) '(neg + -
))
636 (calc-explain-units-rec (nth 1 expr
) pow
))
637 ((and (eq (car-safe expr
) '^
)
638 (math-realp (nth 2 expr
)))
639 (calc-explain-units-rec (nth 1 expr
)
640 (math-mul pow
(nth 2 expr
))))))))
642 (defun calc-simplify-units ()
645 (calc-with-default-simplification
646 (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1))))))
648 (defun calc-view-units-table (n)
650 (and n
(setq math-units-table-buffer-valid nil
))
651 (let ((win (get-buffer-window "*Units Table*")))
654 math-units-table-buffer-valid
)
656 (bury-buffer (window-buffer win
))
657 (let ((curwin (selected-window)))
659 (switch-to-buffer nil
)
660 (select-window curwin
)))
661 (math-build-units-table-buffer nil
))))
663 (defun calc-enter-units-table (n)
665 (and n
(setq math-units-table-buffer-valid nil
))
666 (math-build-units-table-buffer t
)
667 (message "%s" (substitute-command-keys "Type \\[calc] to return to the Calculator")))
669 (defun calc-define-unit (uname desc
&optional disp
)
670 (interactive "SDefine unit name: \nsDescription: \nP")
671 (if disp
(setq disp
(read-string "Display definition: ")))
673 (let ((form (calc-top-n 1))
674 (unit (assq uname math-additional-units
)))
676 (setq math-additional-units
677 (cons (setq unit
(list uname nil nil nil nil
))
678 math-additional-units
)
679 math-units-table nil
))
680 (setcar (cdr unit
) (and (not (and (eq (car-safe form
) 'var
)
681 (eq (nth 1 form
) uname
)))
682 (not (math-equal-int form
1))
683 (math-format-flat-expr form
0)))
684 (setcar (cdr (cdr unit
)) (and (not (equal desc
""))
687 (setcar (cdr (cdr (cdr (cdr unit
)))) disp
))))
688 (calc-invalidate-units-table))
690 (defun calc-undefine-unit (uname)
691 (interactive "SUndefine unit name: ")
693 (let ((unit (assq uname math-additional-units
)))
695 (if (assq uname math-standard-units
)
696 (error "\"%s\" is a predefined unit name" uname
)
697 (error "Unit name \"%s\" not found" uname
)))
698 (setq math-additional-units
(delq unit math-additional-units
)
699 math-units-table nil
)))
700 (calc-invalidate-units-table))
702 (defun calc-invalidate-units-table ()
703 (setq math-units-table nil
)
704 (let ((buf (get-buffer "*Units Table*")))
706 (with-current-buffer buf
708 (goto-char (point-min))
709 (if (looking-at "Calculator Units Table")
710 (let ((inhibit-read-only t
))
711 (insert "(Obsolete) "))))))))
713 (defun calc-get-unit-definition (uname)
714 (interactive "SGet definition for unit: ")
716 (math-build-units-table)
717 (let ((unit (assq uname math-units-table
)))
719 (error "Unit name \"%s\" not found" uname
))
720 (let ((msg (nth 2 unit
)))
722 (if (string-match "^\\*" msg
)
723 (setq msg
(substring msg
1)))
724 (setq msg
(symbol-name uname
)))
727 (calc-enter-result 0 "ugdf" (nth 1 unit
))
728 (message "Derived unit: %s" msg
))
729 (calc-enter-result 0 "ugdf" (list 'var uname
732 (symbol-name uname
)))))
733 (message "Base unit: %s" msg
))))))
735 (defun calc-permanent-units ()
739 (set-buffer (find-file-noselect (substitute-in-file-name
740 calc-settings-file
)))
741 (goto-char (point-min))
742 (if (and (search-forward ";;; Custom units stored by Calc" nil t
)
746 (search-forward "\n;;; End of custom units" nil t
)))
750 (delete-region pos
(point)))
751 (goto-char (point-max))
754 (insert ";;; Custom units stored by Calc on " (current-time-string) "\n")
755 (if math-additional-units
757 (insert "(setq math-additional-units '(\n")
758 (let ((list math-additional-units
))
760 (insert " (" (symbol-name (car (car list
))) " "
761 (if (nth 1 (car list
))
762 (if (stringp (nth 1 (car list
)))
763 (prin1-to-string (nth 1 (car list
)))
764 (prin1-to-string (math-format-flat-expr
765 (nth 1 (car list
)) 0)))
768 (prin1-to-string (nth 2 (car list
)))
770 (setq list
(cdr list
))))
772 (insert ";;; (no custom units defined)\n"))
773 (insert ";;; End of custom units\n")
777 ;; The variable math-cu-unit-list is local to math-build-units-table,
778 ;; but is used by math-compare-unit-names, which is called (indirectly)
779 ;; by math-build-units-table.
780 ;; math-cu-unit-list is also local to math-convert-units, but is used
781 ;; by math-convert-units-rec, which is called by math-convert-units.
782 (defvar math-cu-unit-list
)
784 (defun math-build-units-table ()
786 (let* ((combined-units (append math-additional-units
787 math-standard-units
))
788 (math-cu-unit-list (mapcar 'car combined-units
))
790 (message "Building units table...")
791 (setq math-units-table-buffer-valid nil
)
792 (setq tab
(mapcar (function
796 (if (stringp (nth 1 x
))
797 (let ((exp (math-read-plain-expr
799 (if (eq (car-safe exp
) 'error
)
800 (error "Format error in definition of %s in units table: %s"
807 (list (cons (car x
) 1)))
810 (let ((math-units-table tab
))
811 (mapc 'math-find-base-units tab
))
812 (message "Building units table...done")
813 (setq math-units-table tab
))))
815 ;; The variables math-fbu-base and math-fbu-entry are local to
816 ;; math-find-base-units, but are used by math-find-base-units-rec,
817 ;; which is called by math-find-base-units.
818 (defvar math-fbu-base
)
819 (defvar math-fbu-entry
)
821 (defun math-find-base-units (math-fbu-entry)
822 (if (eq (nth 4 math-fbu-entry
) 'boom
)
823 (error "Circular definition involving unit %s" (car math-fbu-entry
)))
824 (or (nth 4 math-fbu-entry
)
826 (setcar (nthcdr 4 math-fbu-entry
) 'boom
)
827 (math-find-base-units-rec (nth 1 math-fbu-entry
) 1)
829 (error "Dimensionless definition for unit %s" (car math-fbu-entry
)))
830 (while (eq (cdr (car math-fbu-base
)) 0)
831 (setq math-fbu-base
(cdr math-fbu-base
)))
832 (let ((b math-fbu-base
))
834 (if (eq (cdr (car (cdr b
))) 0)
835 (setcdr b
(cdr (cdr b
)))
837 (setq math-fbu-base
(sort math-fbu-base
'math-compare-unit-names
))
838 (setcar (nthcdr 4 math-fbu-entry
) math-fbu-base
)
841 (defun math-compare-unit-names (a b
)
842 (memq (car b
) (cdr (memq (car a
) math-cu-unit-list
))))
844 (defun math-find-base-units-rec (expr pow
)
845 (let ((u (math-check-unit-name expr
)))
847 (let ((ulist (math-find-base-units u
)))
849 (let ((p (* (cdr (car ulist
)) pow
))
850 (old (assq (car (car ulist
)) math-fbu-base
)))
852 (setcdr old
(+ (cdr old
) p
))
854 (cons (cons (car (car ulist
)) p
) math-fbu-base
))))
855 (setq ulist
(cdr ulist
)))))
856 ((math-scalarp expr
))
857 ((and (eq (car expr
) '^
)
858 (integerp (nth 2 expr
)))
859 (math-find-base-units-rec (nth 1 expr
) (* pow
(nth 2 expr
))))
861 (math-find-base-units-rec (nth 1 expr
) pow
)
862 (math-find-base-units-rec (nth 2 expr
) pow
))
864 (math-find-base-units-rec (nth 1 expr
) pow
)
865 (math-find-base-units-rec (nth 2 expr
) (- pow
)))
866 ((eq (car expr
) 'neg
)
867 (math-find-base-units-rec (nth 1 expr
) pow
))
869 (math-find-base-units-rec (nth 1 expr
) pow
))
870 ((eq (car expr
) 'var
)
871 (or (eq (nth 1 expr
) 'pi
)
872 (error "Unknown name %s in defining expression for unit %s"
873 (nth 1 expr
) (car math-fbu-entry
))))
874 (t (error "Malformed defining expression for unit %s" (car math-fbu-entry
))))))
877 (defun math-units-in-expr-p (expr sub-exprs
)
879 (if (eq (car expr
) 'var
)
880 (math-check-unit-name expr
)
882 (memq (car expr
) '(* / ^
)))
883 (or (math-units-in-expr-p (nth 1 expr
) sub-exprs
)
884 (math-units-in-expr-p (nth 2 expr
) sub-exprs
))))))
886 (defun math-only-units-in-expr-p (expr)
888 (if (eq (car expr
) 'var
)
889 (math-check-unit-name expr
)
890 (if (memq (car expr
) '(* /))
891 (and (math-only-units-in-expr-p (nth 1 expr
))
892 (math-only-units-in-expr-p (nth 2 expr
)))
893 (and (eq (car expr
) '^
)
894 (and (math-only-units-in-expr-p (nth 1 expr
))
895 (math-realp (nth 2 expr
))))))))
897 (defun math-single-units-in-expr-p (expr)
898 (cond ((math-scalarp expr
) nil
)
899 ((eq (car expr
) 'var
)
900 (math-check-unit-name expr
))
902 (let ((u1 (math-single-units-in-expr-p (nth 1 expr
)))
903 (u2 (math-single-units-in-expr-p (nth 2 expr
))))
904 (or (and u1 u2
'wrong
)
908 (if (math-units-in-expr-p (nth 2 expr
) nil
)
910 (math-single-units-in-expr-p (nth 1 expr
))))
913 (defun math-check-unit-name (v)
914 (and (eq (car-safe v
) 'var
)
915 (or (assq (nth 1 v
) (or math-units-table
(math-build-units-table)))
916 (let ((name (symbol-name (nth 1 v
))))
917 (and (> (length name
) 1)
918 (assq (aref name
0) math-unit-prefixes
)
919 (or (assq (intern (substring name
1)) math-units-table
)
920 (and (eq (aref name
0) ?M
)
922 (eq (aref name
1) ?e
)
923 (eq (aref name
2) ?g
)
924 (assq (intern (substring name
3))
925 math-units-table
))))))))
927 ;; The variable math-which-standard is local to math-to-standard-units,
928 ;; but is used by math-to-standard-rec, which is called by
929 ;; math-to-standard-units.
930 (defvar math-which-standard
)
932 (defun math-to-standard-units (expr math-which-standard
)
933 (math-to-standard-rec expr
))
935 (defun math-to-standard-rec (expr)
936 (if (eq (car-safe expr
) 'var
)
937 (let ((u (math-check-unit-name expr
))
942 (setq expr
(math-to-standard-rec (nth 1 u
)))
943 (let ((st (assq (car u
) math-which-standard
)))
945 (setq expr
(nth 1 st
))
946 (setq expr
(list 'var
(car u
)
947 (intern (concat "var-"
953 (nth 1 (assq (aref (symbol-name base
) 0)
960 (if (Math-primp expr
)
963 (mapcar 'math-to-standard-rec
(cdr expr
))))))
965 (defun math-apply-units (expr units ulist
&optional pure
)
966 (setq expr
(math-simplify-units expr
))
970 (or (math-numberp expr
)
971 (error "Incompatible units"))
973 (setq value
(math-div expr
(nth 1 (car ulist
)))
974 value
(math-floor (let ((calc-internal-prec
975 (1- calc-internal-prec
)))
976 (math-normalize value
)))
977 new
(math-add new
(math-mul value
(car (car ulist
))))
978 expr
(math-sub expr
(math-mul value
(nth 1 (car ulist
))))
980 (math-add new
(math-mul (math-div expr
(nth 1 (car ulist
)))
984 (math-simplify-units (list '* expr units
)))))
986 (defvar math-decompose-units-cache nil
)
987 (defun math-decompose-units (units)
988 (let ((u (math-check-unit-name units
)))
989 (and u
(eq (car-safe (nth 1 u
)) '+)
990 (setq units
(nth 1 u
))))
991 (setq units
(calcFunc-expand units
))
992 (and (eq (car-safe units
) '+)
993 (let ((entry (list units calc-internal-prec calc-prefer-frac
)))
994 (or (equal entry
(car math-decompose-units-cache
))
998 (while (eq (car-safe utemp
) '+)
999 (setq ulist
(cons (math-decompose-unit-part (nth 2 utemp
))
1001 utemp
(nth 1 utemp
)))
1002 (setq ulist
(cons (math-decompose-unit-part utemp
) ulist
)
1004 (while (setq utemp
(cdr utemp
))
1005 (unless (equal (nth 2 (car utemp
)) (nth 2 (car ulist
)))
1006 (error "Inconsistent units in sum")))
1007 (setq math-decompose-units-cache
1012 (not (Math-lessp (nth 1 x
)
1014 (cdr math-decompose-units-cache
))))
1016 (defun math-decompose-unit-part (unit)
1018 (math-is-multiple (math-simplify-units (math-to-standard-units
1022 ;; The variable math-fcu-u is local to math-find-compatible-unit,
1023 ;; but is used by math-find-compatible-rec which is called by
1024 ;; math-find-compatible-unit.
1027 (defun math-find-compatible-unit (expr unit
)
1028 (let ((math-fcu-u (math-check-unit-name unit
)))
1030 (math-find-compatible-unit-rec expr
1))))
1032 (defun math-find-compatible-unit-rec (expr pow
)
1033 (cond ((eq (car-safe expr
) '*)
1034 (or (math-find-compatible-unit-rec (nth 1 expr
) pow
)
1035 (math-find-compatible-unit-rec (nth 2 expr
) pow
)))
1036 ((eq (car-safe expr
) '/)
1037 (or (math-find-compatible-unit-rec (nth 1 expr
) pow
)
1038 (math-find-compatible-unit-rec (nth 2 expr
) (- pow
))))
1039 ((and (eq (car-safe expr
) '^
)
1040 (integerp (nth 2 expr
)))
1041 (math-find-compatible-unit-rec (nth 1 expr
) (* pow
(nth 2 expr
))))
1043 (let ((u2 (math-check-unit-name expr
)))
1044 (if (equal (nth 4 math-fcu-u
) (nth 4 u2
))
1045 (cons expr pow
))))))
1047 ;; The variables math-cu-new-units and math-cu-pure are local to
1048 ;; math-convert-units, but are used by math-convert-units-rec,
1049 ;; which is called by math-convert-units.
1050 (defvar math-cu-new-units
)
1051 (defvar math-cu-pure
)
1053 (defun math-convert-units (expr math-cu-new-units
&optional math-cu-pure
)
1054 (if (eq (car-safe math-cu-new-units
) 'var
)
1055 (let ((unew (assq (nth 1 math-cu-new-units
)
1056 (math-build-units-table))))
1057 (if (eq (car-safe (nth 1 unew
)) '+)
1058 (setq math-cu-new-units
(nth 1 unew
)))))
1059 (math-with-extra-prec 2
1060 (let ((compat (and (not math-cu-pure
)
1061 (math-find-compatible-unit expr math-cu-new-units
)))
1062 (math-cu-unit-list nil
)
1063 (math-combining-units nil
))
1065 (math-simplify-units
1066 (math-mul (math-mul (math-simplify-units
1067 (math-div expr
(math-pow (car compat
)
1069 (math-pow math-cu-new-units
(cdr compat
)))
1070 (math-simplify-units
1071 (math-to-standard-units
1072 (math-pow (math-div (car compat
) math-cu-new-units
)
1075 (when (setq math-cu-unit-list
(math-decompose-units math-cu-new-units
))
1076 (setq math-cu-new-units
(nth 2 (car math-cu-unit-list
))))
1077 (when (eq (car-safe expr
) '+)
1078 (setq expr
(math-simplify-units expr
)))
1079 (if (math-units-in-expr-p expr t
)
1080 (math-convert-units-rec expr
)
1081 (math-apply-units (math-to-standard-units
1082 (list '/ expr math-cu-new-units
) nil
)
1083 math-cu-new-units math-cu-unit-list math-cu-pure
))))))
1085 (defun math-convert-units-rec (expr)
1086 (if (math-units-in-expr-p expr nil
)
1087 (math-apply-units (math-to-standard-units
1088 (list '/ expr math-cu-new-units
) nil
)
1089 math-cu-new-units math-cu-unit-list math-cu-pure
)
1090 (if (Math-primp expr
)
1093 (mapcar 'math-convert-units-rec
(cdr expr
))))))
1095 (defun math-convert-temperature (expr old new
&optional pure
)
1096 (let* ((units (math-single-units-in-expr-p expr
))
1098 (if (or (null units
)
1099 (equal (nth 1 old
) (car units
)))
1100 (math-check-unit-name old
)
1101 (error "Inconsistent temperature units"))
1103 (unew (math-check-unit-name new
)))
1104 (unless (and (consp unew
) (nth 3 unew
))
1105 (error "Not a valid temperature unit"))
1106 (unless (and (consp uold
) (nth 3 uold
))
1107 (error "Not a pure temperature expression"))
1108 (let ((v (car uold
)))
1109 (setq expr
(list '/ expr
(list 'var v
1110 (intern (concat "var-"
1111 (symbol-name v
)))))))
1112 (or (eq (nth 3 uold
) (nth 3 unew
))
1113 (cond ((eq (nth 3 uold
) 'K
)
1114 (setq expr
(list '- expr
'(/ 27315 100)))
1115 (if (eq (nth 3 unew
) 'F
)
1116 (setq expr
(list '+ (list '* expr
'(/ 9 5)) 32))))
1117 ((eq (nth 3 uold
) 'C
)
1118 (if (eq (nth 3 unew
) 'F
)
1119 (setq expr
(list '+ (list '* expr
'(/ 9 5)) 32))
1120 (setq expr
(list '+ expr
'(/ 27315 100)))))
1122 (setq expr
(list '* (list '- expr
32) '(/ 5 9)))
1123 (if (eq (nth 3 unew
) 'K
)
1124 (setq expr
(list '+ expr
'(/ 27315 100)))))))
1127 (list '* expr new
))))
1131 (defun math-simplify-units (a)
1132 (let ((math-simplifying-units t
)
1133 (calc-matrix-mode 'scalar
))
1135 (defalias 'calcFunc-usimplify
'math-simplify-units
)
1137 ;; The function created by math-defsimplify uses the variable
1138 ;; math-simplify-expr, and so is used by functions in math-defsimplify
1139 (defvar math-simplify-expr
)
1141 (math-defsimplify (+ -
)
1142 (and math-simplifying-units
1143 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1144 (let* ((units (math-extract-units (nth 1 math-simplify-expr
)))
1145 (ratio (math-simplify (math-to-standard-units
1146 (list '/ (nth 2 math-simplify-expr
) units
) nil
))))
1147 (if (math-units-in-expr-p ratio nil
)
1149 (calc-record-why "*Inconsistent units" math-simplify-expr
)
1151 (list '* (math-add (math-remove-units (nth 1 math-simplify-expr
))
1152 (if (eq (car math-simplify-expr
) '-
)
1153 (math-neg ratio
) ratio
))
1157 (math-simplify-units-prod))
1159 (defun math-simplify-units-prod ()
1160 (and math-simplifying-units
1161 calc-autorange-units
1162 (Math-realp (nth 1 math-simplify-expr
))
1163 (let* ((num (math-float (nth 1 math-simplify-expr
)))
1164 (xpon (calcFunc-xpon num
))
1165 (unitp (cdr (cdr math-simplify-expr
)))
1167 (pow (if (eq (car math-simplify-expr
) '*) 1 -
1))
1169 (and (eq (car-safe unit
) '*)
1170 (setq unitp
(cdr unit
)
1172 (and (eq (car-safe unit
) '^
)
1173 (integerp (nth 2 unit
))
1174 (setq pow
(* pow
(nth 2 unit
))
1177 (and (setq u
(math-check-unit-name unit
))
1180 (>= xpon
(if (eq (car u
) 'm
) 1 3)))
1183 (reverse math-unit-prefixes
)
1184 math-unit-prefixes
))
1187 (or (eq (car u
) (nth 1 unit
))
1192 math-unit-prefixes
))))))
1193 (setq xpon
(+ xpon uxpon
))
1195 (or (memq (car (car p
)) '(?d ?D ?h ?H
))
1196 (and (eq (car (car p
)) ?c
)
1197 (not (eq (car u
) 'm
)))
1198 (< xpon
(setq pxpon
(* (nth 2 (nth 1 (car p
)))
1201 (setq pname
(math-build-var-name
1202 (if (eq (car (car p
)) 0)
1204 (concat (char-to-string
1208 (and (/= (car (car p
)) 0)
1210 math-units-table
)))))
1214 (or (not (eq p pref
))
1215 (< xpon
(+ pxpon
(* (math-abs pow
) 3))))
1217 (setcar (cdr math-simplify-expr
)
1218 (let ((calc-prefer-frac nil
))
1219 (calcFunc-scf (nth 1 math-simplify-expr
)
1221 (setcar unitp pname
)
1222 math-simplify-expr
)))))))
1224 (defvar math-try-cancel-units
)
1227 (and math-simplifying-units
1228 (let ((np (cdr math-simplify-expr
))
1229 (math-try-cancel-units 0)
1231 (setq n
(if (eq (car-safe (nth 2 math-simplify-expr
)) '*)
1232 (cdr (nth 2 math-simplify-expr
))
1233 (nthcdr 2 math-simplify-expr
)))
1234 (if (math-realp (car n
))
1236 (setcar (cdr math-simplify-expr
) (math-mul (nth 1 math-simplify-expr
)
1237 (let ((calc-prefer-frac nil
))
1238 (math-div 1 (car n
)))))
1240 (while (eq (car-safe (setq n
(car np
))) '*)
1241 (math-simplify-units-divisor (cdr n
) (cdr (cdr math-simplify-expr
)))
1242 (setq np
(cdr (cdr n
))))
1243 (math-simplify-units-divisor np
(cdr (cdr math-simplify-expr
)))
1244 (if (eq math-try-cancel-units
0)
1245 (let* ((math-simplifying-units nil
)
1246 (base (math-simplify
1247 (math-to-standard-units math-simplify-expr nil
))))
1248 (if (Math-numberp base
)
1249 (setq math-simplify-expr base
))))
1250 (if (eq (car-safe math-simplify-expr
) '/)
1251 (math-simplify-units-prod))
1252 math-simplify-expr
)))
1254 (defun math-simplify-units-divisor (np dp
)
1257 (while (eq (car-safe (setq d
(car dp
))) '*)
1258 (when (setq temp
(math-simplify-units-quotient n
(nth 1 d
)))
1259 (setcar np
(setq n temp
))
1261 (setq dp
(cdr (cdr d
))))
1262 (when (setq temp
(math-simplify-units-quotient n d
))
1263 (setcar np
(setq n temp
))
1266 ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.
1267 (defun math-simplify-units-quotient (n d
)
1270 (when (and (eq (car-safe n
) '^
)
1271 (integerp (nth 2 n
)))
1272 (setq pow1
(nth 2 n
) n
(nth 1 n
)))
1273 (when (and (eq (car-safe d
) '^
)
1274 (integerp (nth 2 d
)))
1275 (setq pow2
(nth 2 d
) d
(nth 1 d
)))
1276 (let ((un (math-check-unit-name n
))
1277 (ud (math-check-unit-name d
)))
1279 (if (and (equal (nth 4 un
) (nth 4 ud
))
1282 (math-to-standard-units (list '/ n d
) nil
)
1283 (list '^
(math-to-standard-units (list '/ n d
) nil
) pow1
))
1290 (and (eq (car (car un
)) (car (car ud1
)))
1291 (setq math-try-cancel-units
1292 (+ math-try-cancel-units
1293 (- (* (cdr (car un
)) pow1
)
1294 (* (cdr (car ud
)) pow2
)))))
1295 (setq ud1
(cdr ud1
)))
1300 (and math-simplifying-units
1301 (math-realp (nth 2 math-simplify-expr
))
1302 (if (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1303 (list (car (nth 1 math-simplify-expr
))
1304 (list '^
(nth 1 (nth 1 math-simplify-expr
))
1305 (nth 2 math-simplify-expr
))
1306 (list '^
(nth 2 (nth 1 math-simplify-expr
))
1307 (nth 2 math-simplify-expr
)))
1308 (math-simplify-units-pow (nth 1 math-simplify-expr
)
1309 (nth 2 math-simplify-expr
)))))
1311 (math-defsimplify calcFunc-sqrt
1312 (and math-simplifying-units
1313 (if (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1314 (list (car (nth 1 math-simplify-expr
))
1315 (list 'calcFunc-sqrt
(nth 1 (nth 1 math-simplify-expr
)))
1316 (list 'calcFunc-sqrt
(nth 2 (nth 1 math-simplify-expr
))))
1317 (math-simplify-units-pow (nth 1 math-simplify-expr
) '(frac 1 2)))))
1319 (math-defsimplify (calcFunc-floor
1329 (and math-simplifying-units
1330 (= (length math-simplify-expr
) 2)
1331 (if (math-only-units-in-expr-p (nth 1 math-simplify-expr
))
1332 (nth 1 math-simplify-expr
)
1333 (if (and (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1334 (or (math-only-units-in-expr-p
1335 (nth 1 (nth 1 math-simplify-expr
)))
1336 (math-only-units-in-expr-p
1337 (nth 2 (nth 1 math-simplify-expr
)))))
1338 (list (car (nth 1 math-simplify-expr
))
1339 (cons (car math-simplify-expr
)
1340 (cons (nth 1 (nth 1 math-simplify-expr
))
1341 (cdr (cdr math-simplify-expr
))))
1342 (cons (car math-simplify-expr
)
1343 (cons (nth 2 (nth 1 math-simplify-expr
))
1344 (cdr (cdr math-simplify-expr
)))))))))
1346 (defun math-simplify-units-pow (a pow
)
1347 (if (and (eq (car-safe a
) '^
)
1348 (math-check-unit-name (nth 1 a
))
1349 (math-realp (nth 2 a
)))
1350 (list '^
(nth 1 a
) (math-mul pow
(nth 2 a
)))
1351 (let* ((u (math-check-unit-name a
))
1352 (pf (math-to-simple-fraction pow
))
1353 (d (and (eq (car-safe pf
) 'frac
) (nth 2 pf
))))
1355 (math-units-are-multiple u d
)
1356 (list '^
(math-to-standard-units a nil
) pow
)))))
1359 (defun math-units-are-multiple (u n
)
1361 (while (and u
(= (%
(cdr (car u
)) n
) 0))
1365 (math-defsimplify calcFunc-sin
1366 (and math-simplifying-units
1367 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1368 (let ((rad (math-simplify-units
1370 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1371 (calc-angle-mode 'rad
))
1372 (and (eq (car-safe rad
) '*)
1373 (math-realp (nth 1 rad
))
1374 (eq (car-safe (nth 2 rad
)) 'var
)
1375 (eq (nth 1 (nth 2 rad
)) 'rad
)
1376 (list 'calcFunc-sin
(nth 1 rad
))))))
1378 (math-defsimplify calcFunc-cos
1379 (and math-simplifying-units
1380 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1381 (let ((rad (math-simplify-units
1383 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1384 (calc-angle-mode 'rad
))
1385 (and (eq (car-safe rad
) '*)
1386 (math-realp (nth 1 rad
))
1387 (eq (car-safe (nth 2 rad
)) 'var
)
1388 (eq (nth 1 (nth 2 rad
)) 'rad
)
1389 (list 'calcFunc-cos
(nth 1 rad
))))))
1391 (math-defsimplify calcFunc-tan
1392 (and math-simplifying-units
1393 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1394 (let ((rad (math-simplify-units
1396 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1397 (calc-angle-mode 'rad
))
1398 (and (eq (car-safe rad
) '*)
1399 (math-realp (nth 1 rad
))
1400 (eq (car-safe (nth 2 rad
)) 'var
)
1401 (eq (nth 1 (nth 2 rad
)) 'rad
)
1402 (list 'calcFunc-tan
(nth 1 rad
))))))
1404 (math-defsimplify calcFunc-sec
1405 (and math-simplifying-units
1406 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1407 (let ((rad (math-simplify-units
1409 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1410 (calc-angle-mode 'rad
))
1411 (and (eq (car-safe rad
) '*)
1412 (math-realp (nth 1 rad
))
1413 (eq (car-safe (nth 2 rad
)) 'var
)
1414 (eq (nth 1 (nth 2 rad
)) 'rad
)
1415 (list 'calcFunc-sec
(nth 1 rad
))))))
1417 (math-defsimplify calcFunc-csc
1418 (and math-simplifying-units
1419 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1420 (let ((rad (math-simplify-units
1422 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1423 (calc-angle-mode 'rad
))
1424 (and (eq (car-safe rad
) '*)
1425 (math-realp (nth 1 rad
))
1426 (eq (car-safe (nth 2 rad
)) 'var
)
1427 (eq (nth 1 (nth 2 rad
)) 'rad
)
1428 (list 'calcFunc-csc
(nth 1 rad
))))))
1430 (math-defsimplify calcFunc-cot
1431 (and math-simplifying-units
1432 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1433 (let ((rad (math-simplify-units
1435 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1436 (calc-angle-mode 'rad
))
1437 (and (eq (car-safe rad
) '*)
1438 (math-realp (nth 1 rad
))
1439 (eq (car-safe (nth 2 rad
)) 'var
)
1440 (eq (nth 1 (nth 2 rad
)) 'rad
)
1441 (list 'calcFunc-cot
(nth 1 rad
))))))
1444 (defun math-remove-units (expr)
1445 (if (math-check-unit-name expr
)
1447 (if (Math-primp expr
)
1450 (mapcar 'math-remove-units
(cdr expr
))))))
1452 (defun math-extract-units (expr)
1453 (if (memq (car-safe expr
) '(* /))
1455 (mapcar 'math-extract-units
(cdr expr
)))
1456 (if (math-check-unit-name expr
) expr
1)))
1458 (defun math-build-units-table-buffer (enter-buffer)
1459 (if (not (and math-units-table math-units-table-buffer-valid
1460 (get-buffer "*Units Table*")))
1461 (let ((buf (get-buffer-create "*Units Table*"))
1462 (uptr (math-build-units-table))
1463 (calc-language (if (eq calc-language
'big
) nil calc-language
))
1464 (calc-float-format '(float 0))
1465 (calc-group-digits nil
)
1466 (calc-number-radix 10)
1467 (calc-twos-complement-mode nil
)
1468 (calc-point-char ".")
1472 (message "Formatting units table...")
1474 (let ((inhibit-read-only t
))
1476 (insert "Calculator Units Table:\n\n")
1477 (insert "(All definitions are exact unless marked with an asterisk (*).)\n\n")
1478 (insert "Unit Type Definition Description\n\n")
1482 (when (eq (car u
) 'm
)
1484 (setq shadowed
(and std
(assq (car u
) math-additional-units
)))
1487 (eq (aref name
0) ?\
*))
1488 (unless (eq uptr math-units-table
)
1490 (setq name
(substring name
1)))
1492 (and shadowed
(insert "("))
1493 (insert (symbol-name (car u
)))
1494 (and shadowed
(insert ")"))
1498 (insert (symbol-name (nth 3 u
))))
1504 (and shadowed
(insert "("))
1508 (insert (math-format-value (nth 1 u
) 80))
1509 (insert (symbol-name (car u
)))))
1510 (and shadowed
(insert ")"))
1516 (insert " (redefined above)")
1518 (insert " (base unit)")))
1520 (setq uptr
(cdr uptr
)))
1521 (insert "\n\nUnit Prefix Table:\n\n")
1522 (setq uptr math-unit-prefixes
)
1525 (insert " " (char-to-string (car u
)))
1526 (if (equal (nth 1 u
) (nth 1 (nth 1 uptr
)))
1527 (insert " " (char-to-string (car (car (setq uptr
(cdr uptr
)))))
1530 (insert "10^" (int-to-string (nth 2 (nth 1 u
))))
1532 (insert " " (nth 2 u
) "\n")
1533 (while (eq (car (car (setq uptr
(cdr uptr
)))) 0)))
1536 (message "Formatting units table...done"))
1537 (setq math-units-table-buffer-valid t
)
1538 (let ((oldbuf (current-buffer)))
1540 (goto-char (point-min))
1541 (set-buffer oldbuf
))
1544 (display-buffer buf
)))
1546 (pop-to-buffer (get-buffer "*Units Table*"))
1547 (display-buffer (get-buffer "*Units Table*")))))
1549 (provide 'calc-units
)
1555 ;; arch-tag: e993314f-3adc-4191-be61-4ef8874881c4
1556 ;;; calc-units.el ends here