1 ;;; calc-units.el --- unit conversion functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
36 ;;; Units table last updated 9-Jan-91 by Ulrich Mueller (ulm@vsnhd1.cern.ch)
37 ;;; with some additions by Przemek Klosowski (przemek@rrdstrad.nist.gov)
38 ;;; Updated April 2002 by Jochen Küpper
40 ;;; Updated August 2007, using
41 ;;; CODATA (http://physics.nist.gov/cuu/Constants/index.html)
42 ;;; NIST (http://physics.nist.gov/Pubs/SP811/appenB9.html)
43 ;;; ESUWM (Encyclopaedia of Scientific Units, Weights and
44 ;;; Measures, by François Cardarelli)
45 ;;; All conversions are exact unless otherwise noted.
47 (defvar math-standard-units
50 ( in "254*10^(-2) cm" "Inch" nil
54 ( mi "5280 ft" "Mile" )
55 ( au "149597870691. m" "Astronomical Unit" nil
57 ;; (approx) NASA JPL (http://neo.jpl.nasa.gov/glossary/au.html)
58 ( lyr "c yr" "Light Year" )
59 ( pc "3.0856775854*10^16 m" "Parsec (**)" nil
60 "3.0856775854 10^16 m (*)") ;; (approx) ESUWM
61 ( nmi "1852 m" "Nautical Mile" )
62 ( fath "6 ft" "Fathom" )
63 ( fur "660 ft" "Furlong")
64 ( mu "1 um" "Micron" )
65 ( mil "(1/1000) in" "Mil" )
66 ( point "(1/72) in" "Point (PostScript convention)" )
67 ( Ang "10^(-10) m" "Angstrom" )
68 ( mfi "mi+ft+in" "Miles + feet + inches" )
70 ( texpt "(100/7227) in" "Point (TeX convention) (**)" )
71 ( texpc "12 texpt" "Pica (TeX convention) (**)" )
72 ( texbp "point" "Big point (TeX convention) (**)" )
73 ( texdd "(1238/1157) texpt" "Didot point (TeX convention) (**)" )
74 ( texcc "12 texdd" "Cicero (TeX convention) (**)" )
75 ( texsp "(1/65536) texpt" "Scaled TeX point (TeX convention) (**)" )
78 ( hect "10000 m^2" "*Hectare" )
80 ( acre "(1/640) mi^2" "Acre" )
81 ( b "10^(-28) m^2" "Barn" )
84 ( L "10^(-3) m^3" "*Liter" )
86 ( gal "4 qt" "US Gallon" )
88 ( pt "2 cup" "Pint (**)" )
89 ( cup "8 ozfl" "Cup" )
90 ( ozfl "2 tbsp" "Fluid Ounce" )
91 ( floz "2 tbsp" "Fluid Ounce" )
92 ( tbsp "3 tsp" "Tablespoon" )
93 ;; ESUWM defines a US gallon as 231 in^3.
94 ;; That gives the following exact value for tsp.
95 ( tsp "492892159375*10^(-11) ml" "Teaspoon" nil
97 ( vol "tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" nil
98 "tsp+tbsp+ozfl+cup+pt+qt+gal")
99 ( galC "galUK" "Canadian Gallon" )
100 ( galUK "454609*10^(-5) L" "UK Gallon" nil
106 ( min "60 s" "Minute" )
107 ( hr "60 min" "Hour" )
108 ( day "24 hr" "Day" )
109 ( wk "7 day" "Week" )
110 ( hms "wk+day+hr+min+s" "Hours, minutes, seconds" )
111 ( yr "36525*10^(-2) day" "Year (Julian)" nil
116 ( mph "mi/hr" "*Miles per hour" )
117 ( kph "km/hr" "Kilometers per hour" )
118 ( knot "nmi/hr" "Knot" )
119 ( c "299792458 m/s" "Speed of light" ) ;;; CODATA
122 ( ga "980665*10^(-5) m/s^2" "*\"g\" acceleration" nil
123 "9.80665 m / s^2") ;; CODATA
127 ( lb "16 oz" "Pound (mass)" )
128 ( oz "28349523125*10^(-9) g" "Ounce (mass)" nil
129 "28.349523125 g") ;; ESUWM
130 ( ton "2000 lb" "Ton" )
131 ( tpo "ton+lb+oz" "Tons + pounds + ounces (mass)" )
132 ( t "1000 kg" "Metric ton" )
133 ( tonUK "10160469088*10^(-7) kg" "UK ton" nil
134 "1016.0469088 kg") ;; ESUWM
135 ( lbt "12 ozt" "Troy pound" )
136 ( ozt "311034768*10^(-7) g" "Troy ounce" nil
137 "31.10347680 g") ;; ESUWM, 1/12 exact value for lbt
138 ( ct "(2/10) g" "Carat" nil
140 ( u "1.660538782*10^(-27) kg" "Unified atomic mass" nil
141 "1.660538782 10^-27 kg (*)");;(approx) CODATA
144 ( N "m kg/s^2" "*Newton" )
145 ( dyn "10^(-5) N" "Dyne" )
146 ( gf "ga g" "Gram (force)" )
147 ( lbf "ga lb" "Pound (force)" )
148 ( kip "1000 lbf" "Kilopound (force)" )
149 ( pdl "138254954376*10^(-12) N" "Poundal" nil
150 "0.138254954376 N") ;; ESUWM
154 ( erg "10^(-7) J" "Erg" )
155 ( cal "41868*10^(-4) J" "International Table Calorie" nil
157 ( calth "4184*10^(-3) J" "Thermochemical Calorie" nil
159 ( Cal "1000 cal" "Large Calorie")
160 ( Btu "105505585262*10^(-8) J" "International Table Btu" nil
161 "1055.05585262 J") ;; ESUWM
162 ( eV "ech V" "Electron volt" )
163 ( ev "eV" "Electron volt" )
164 ( therm "105506000 J" "EEC therm" )
165 ( invcm "h c/cm" "Energy in inverse centimeters" )
166 ( Kayser "invcm" "Kayser (inverse centimeter energy)" )
167 ( men "100/invcm" "Inverse energy in meters" )
168 ( Hzen "h Hz" "Energy in Hertz")
169 ( Ken "k K" "Energy in Kelvins")
170 ( Wh "W hr" "Watt hour")
171 ( Ws "W s" "Watt second")
175 ( hp "550 ft lbf/s" "Horsepower") ;;ESUWM
176 ( hpm "75 m kgf/s" "Metric Horsepower") ;;ESUWM
179 ( K nil "*Degree Kelvin" K )
180 ( dK "K" "Degree Kelvin" K )
181 ( degK "K" "Degree Kelvin" K )
182 ( dC "K" "Degree Celsius" C )
183 ( degC "K" "Degree Celsius" C )
184 ( dF "(5/9) K" "Degree Fahrenheit" F )
185 ( degF "(5/9) K" "Degree Fahrenheit" F )
188 ( Pa "N/m^2" "*Pascal" )
189 ( bar "10^5 Pa" "Bar" )
190 ( atm "101325 Pa" "Standard atmosphere" ) ;; CODATA
191 ( Torr "(1/760) atm" "Torr")
192 ( mHg "1000 Torr" "Meter of mercury" )
193 ( inHg "254*10^(-1) mmHg" "Inch of mercury" nil
195 ( inH2O "2.490889*10^2 Pa" "Inch of water" nil
196 "2.490889 10^2 Pa (*)") ;;(approx) NIST
197 ( psi "lbf/in^2" "Pounds per square inch" )
200 ( P "(1/10) Pa s" "*Poise" )
201 ( St "10^(-4) m^2/s" "Stokes" )
205 ( C "A s" "Coulomb" )
206 ( Fdy "ech Nav" "Faraday" )
207 ( e "ech" "Elementary charge" )
208 ( ech "1.602176487*10^(-19) C" "Elementary charge" nil
209 "1.602176487 10^-19 C (*)") ;;(approx) CODATA
214 ( S "A/V" "Siemens" )
217 ( T "Wb/m^2" "Tesla" )
218 ( Gs "10^(-4) T" "Gauss" )
221 ;; Luminous intensity
222 ( cd nil "*Candela" )
223 ( sb "10000 cd/m^2" "Stilb" )
224 ( lm "cd sr" "Lumen" )
225 ( lx "lm/m^2" "Lux" )
226 ( ph "10000 lx" "Phot" )
227 ( fc "lm/ft^2" "Footcandle") ;; ESUWM
228 ( lam "10000 lm/m^2" "Lambert" )
229 ( flam "(1/pi) cd/ft^2" "Footlambert") ;; ESUWM
232 ( Bq "1/s" "*Becquerel" )
233 ( Ci "37*10^9 Bq" "Curie" ) ;; ESUWM
235 ( Sv "Gy" "Sievert" )
236 ( R "258*10^(-6) C/kg" "Roentgen" ) ;; NIST
237 ( rd "(1/100) Gy" "Rad" )
240 ;; Amount of substance
244 ( rad nil "*Radian" )
245 ( circ "2 pi rad" "Full circle" )
246 ( rev "circ" "Full revolution" )
247 ( deg "circ/360" "Degree" )
248 ( arcmin "deg/60" "Arc minute" )
249 ( arcsec "arcmin/60" "Arc second" )
250 ( grad "circ/400" "Grade" )
251 ( rpm "rev/min" "Revolutions per minute" )
254 ( sr nil "*Steradian" )
256 ;; Other physical quantities
257 ;; The values are from CODATA, and are approximate.
258 ( h "6.62606896*10^(-34) J s" "*Planck's constant" nil
259 "6.62606896 10^-34 J s (*)")
260 ( hbar "h / (2 pi)" "Planck's constant" ) ;; Exact
261 ( mu0 "4 pi 10^(-7) H/m" "Permeability of vacuum") ;; Exact
262 ( μ0 "mu0" "Permeability of vacuum") ;; Exact
263 ( eps0 "1 / (mu0 c^2)" "Permittivity of vacuum" )
264 ( ε0 "eps0" "Permittivity of vacuum" )
265 ( G "6.67428*10^(-11) m^3/(kg s^2)" "Gravitational constant" nil
266 "6.67428 10^-11 m^3/(kg s^2) (*)")
267 ( Nav "6.02214179*10^(23) / mol" "Avogadro's constant" nil
268 "6.02214179 10^23 / mol (*)")
269 ( me "9.10938215*10^(-31) kg" "Electron rest mass" nil
270 "9.10938215 10^-31 kg (*)")
271 ( mp "1.672621637*10^(-27) kg" "Proton rest mass" nil
272 "1.672621637 10^-27 kg (*)")
273 ( mn "1.674927211*10^(-27) kg" "Neutron rest mass" nil
274 "1.674927211 10^-27 kg (*)")
275 ( mmu "1.88353130*10^(-28) kg" "Muon rest mass" nil
276 "1.88353130 10^-28 kg (*)")
277 ( mμ "mmu" "Muon rest mass" nil
278 "1.88353130 10^-28 kg (*)")
279 ( Ryd "10973731.568527 /m" "Rydberg's constant" nil
280 "10973731.568527 /m (*)")
281 ( k "1.3806504*10^(-23) J/K" "Boltzmann's constant" nil
282 "1.3806504 10^-23 J/K (*)")
283 ( alpha "7.2973525376*10^(-3)" "Fine structure constant" nil
284 "7.2973525376 10^-3 (*)")
285 ( α "alpha" "Fine structure constant" nil
286 "7.2973525376 10^-3 (*)")
287 ( muB "927.400915*10^(-26) J/T" "Bohr magneton" nil
288 "927.400915 10^-26 J/T (*)")
289 ( muN "5.05078324*10^(-27) J/T" "Nuclear magneton" nil
290 "5.05078324 10^-27 J/T (*)")
291 ( mue "-928.476377*10^(-26) J/T" "Electron magnetic moment" nil
292 "-928.476377 10^-26 J/T (*)")
293 ( mup "1.410606662*10^(-26) J/T" "Proton magnetic moment" nil
294 "1.410606662 10^-26 J/T (*)")
295 ( R0 "8.314472 J/(mol K)" "Molar gas constant" nil
296 "8.314472 J/(mol K) (*)")
297 ( V0 "22.710981*10^(-3) m^3/mol" "Standard volume of ideal gas" nil
298 "22.710981 10^-3 m^3/mol (*)")
301 ( dB "(ln(10)/20) Np" "decibel")))
304 (defvar math-additional-units nil
305 "Additional units table for user-defined units.
306 Must be formatted like `math-standard-units'.
307 If you change this, be sure to set `math-units-table' to nil to ensure
308 that the combined units table will be rebuilt.")
310 (defvar math-unit-prefixes
311 '( ( ?Y (^ 10 24) "Yotta" )
312 ( ?Z (^ 10 21) "Zetta" )
313 ( ?E (^ 10 18) "Exa" )
314 ( ?P (^ 10 15) "Peta" )
315 ( ?T (^ 10 12) "Tera" )
316 ( ?G (^ 10 9) "Giga" )
317 ( ?M (^ 10 6) "Mega" )
318 ( ?k (^ 10 3) "Kilo" )
319 ( ?K (^ 10 3) "Kilo" )
320 ( ?h (^ 10 2) "Hecto" )
321 ( ?H (^ 10 2) "Hecto" )
322 ( ?D (^ 10 1) "Deka" )
324 ( ?d (^ 10 -1) "Deci" )
325 ( ?c (^ 10 -2) "Centi" )
326 ( ?m (^ 10 -3) "Milli" )
327 ( ?u (^ 10 -6) "Micro" )
328 ( ?μ (^ 10 -6) "Micro" )
329 ( ?n (^ 10 -9) "Nano" )
330 ( ?p (^ 10 -12) "Pico" )
331 ( ?f (^ 10 -15) "Femto" )
332 ( ?a (^ 10 -18) "Atto" )
333 ( ?z (^ 10 -21) "zepto" )
334 ( ?y (^ 10 -24) "yocto" )))
336 (defvar math-standard-units-systems
338 ( si ( ( g '(/ (var kg var-kg) 1000) ) ) )
339 ( mks ( ( g '(/ (var kg var-kg) 1000) ) ) )
340 ( cgs ( ( m '(* (var cm var-cm) 100 ) ) ) )))
342 (defvar math-units-table nil
343 "Internal units table.
344 Derived from `math-standard-units' and `math-additional-units'.
345 Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
347 (defvar math-units-table-buffer-valid nil)
351 (defun calc-base-units ()
354 (let ((calc-autorange-units nil))
355 (calc-enter-result 1 "bsun" (math-simplify-units
356 (math-to-standard-units (calc-top-n 1)
359 (defvar calc-ensure-consistent-units)
361 (defun calc-quick-units ()
364 (let* ((num (- last-command-event ?0))
365 (pos (if (= num 0) 10 num))
366 (units (calc-var-value 'var-Units))
367 (expr (calc-top-n 1)))
368 (unless (and (>= num 0) (<= num 9))
369 (error "Bad unit number"))
370 (unless (math-vectorp units)
371 (error "No \"quick units\" are defined"))
372 (unless (< pos (length units))
373 (error "Unit number %d not defined" pos))
374 (if (math-units-in-expr-p expr nil)
376 (if calc-ensure-consistent-units
377 (math-check-unit-consistency expr (nth pos units)))
378 (calc-enter-result 1 (format "cun%d" num)
379 (math-convert-units expr (nth pos units))))
380 (calc-enter-result 1 (format "*un%d" num)
382 (math-mul expr (nth pos units))))))))
384 (defun math-get-standard-units (expr)
385 "Return the standard units in EXPR."
388 (math-to-standard-units expr nil))))
390 (defun math-get-units (expr)
391 "Return the units in EXPR."
393 (math-extract-units expr)))
395 (defun math-make-unit-string (expr)
396 "Return EXPR in string form.
397 If EXPR is nil, return nil."
399 (let ((cexpr (math-compose-expr expr 0)))
400 (replace-regexp-in-string
404 (math-composition-to-string cexpr))))))
406 (defvar math-default-units-table
407 (make-hash-table :test 'equal)
408 "A table storing previously converted units.")
410 (defun math-get-default-units (expr)
411 "Get default units to use when converting the units in EXPR."
412 (let* ((units (math-get-units expr))
413 (standard-units (math-get-standard-units expr))
414 (default-units (gethash
416 math-default-units-table)))
417 (if (equal units (car default-units))
418 (math-make-unit-string (cadr default-units))
419 (math-make-unit-string (car default-units)))))
421 (defun math-put-default-units (expr &optional comp std)
422 "Put the units in EXPR in the default units table.
423 If COMP or STD is non-nil, put that in the units table instead."
424 (let* ((new-units (or comp std (math-get-units expr)))
425 (standard-units (math-get-standard-units
427 (comp (math-simplify-units expr))
430 (default-units (gethash standard-units math-default-units-table)))
431 (unless (eq standard-units 1)
434 (puthash standard-units (list new-units) math-default-units-table))
435 ((not (equal new-units (car default-units)))
436 (puthash standard-units
437 (list new-units (car default-units))
438 math-default-units-table))))))
440 (defvar calc-allow-units-as-numbers)
442 (defun calc-convert-units (&optional old-units new-units)
445 (let ((expr (calc-top-n 1))
452 (if (or (not (math-units-in-expr-p expr t))
453 (setq unitscancel (and
454 calc-allow-units-as-numbers
455 (eq (math-get-standard-units expr) 1))))
456 (let ((uold (or old-units
461 "(The expression is unitless when simplified) Old Units: ")
462 (read-string "Old units: ")))
463 (if (equal uoldname "")
465 (setq nouold unitscancel)
468 (if (string-match "\\` */" uoldname)
469 (setq uoldname (concat "1" uoldname)))
470 (math-read-expr uoldname))))))
471 (when (eq (car-safe uold) 'error)
472 (error "Bad format in units expression: %s" (nth 1 uold)))
473 (setq expr (math-mul expr uold))))
474 (setq defunits (math-get-default-units expr))
478 (if (and uoldname (not nouold))
479 (concat "Old units: "
490 (string= new-units "")
492 (setq new-units defunits)))
493 (when (string-match "\\` */" new-units)
494 (setq new-units (concat "1" new-units)))
495 (setq units (math-read-expr new-units))
496 (when (eq (car-safe units) 'error)
497 (error "Bad format in units expression: %s" (nth 2 units)))
498 (if calc-ensure-consistent-units
499 (math-check-unit-consistency expr units))
500 (let ((unew (math-units-in-expr-p units t))
501 (std (and (eq (car-safe units) 'var)
502 (assq (nth 1 units) math-standard-units-systems)))
503 (comp (eq (car-safe units) '+)))
504 (unless (or unew std)
505 (error "No units specified"))
506 (let* ((noold (and uoldname (not (equal uoldname "1"))))
509 (math-simplify-units (math-to-standard-units expr (nth 1 std)))
510 (math-convert-units expr units noold))))
512 (math-put-default-units (if noold units res) (if comp units)))
513 (calc-enter-result 1 "cvun" res))))))
515 (defun calc-autorange-units (arg)
518 (calc-change-mode 'calc-autorange-units arg nil t)
519 (message (if calc-autorange-units
520 "Adjusting target unit prefix automatically"
521 "Using target units exactly"))))
523 (defun calc-convert-temperature (&optional old-units new-units)
526 (let ((expr (calc-top-n 1))
531 (setq uold (or old-units
532 (let ((units (math-single-units-in-expr-p expr)))
535 (list 'var (car units)
536 (intern (concat "var-"
539 (error "Not a pure temperature expression"))
541 (setq uoldname (read-string
542 "Old temperature units: ")))))))
543 (when (eq (car-safe uold) 'error)
544 (error "Bad format in units expression: %s" (nth 2 uold)))
545 (or (math-units-in-expr-p expr nil)
546 (setq expr (math-mul expr uold)))
547 (setq defunits (math-get-default-units expr))
548 (setq unew (or new-units
552 (concat "Old temperature units: "
555 "New temperature units")
561 (setq unew (math-read-expr (if (string= unew "") defunits unew)))
562 (when (eq (car-safe unew) 'error)
563 (error "Bad format in units expression: %s" (nth 2 unew)))
564 (math-put-default-units unew)
565 (let ((ntemp (calc-normalize
567 (math-convert-temperature expr uold unew
569 (if (Math-zerop ntemp)
570 (setq ntemp (list '* ntemp unew)))
571 (let ((calc-simplify-mode 'none))
572 (calc-enter-result 1 "cvtm" ntemp))))))
574 (defun calc-remove-units ()
577 (calc-enter-result 1 "rmun" (math-simplify-units
578 (math-remove-units (calc-top-n 1))))))
580 (defun calc-extract-units ()
583 (calc-enter-result 1 "exun" (math-simplify-units
584 (math-extract-units (calc-top-n 1))))))
586 ;; The variables calc-num-units and calc-den-units are local to
587 ;; calc-explain-units, but are used by calc-explain-units-rec,
588 ;; which is called by calc-explain-units.
589 (defvar calc-num-units)
590 (defvar calc-den-units)
592 (defun calc-explain-units ()
595 (let ((calc-num-units nil)
596 (calc-den-units nil))
597 (calc-explain-units-rec (calc-top-n 1) 1)
598 (and calc-den-units (string-match "^[^(].* .*[^)]$" calc-den-units)
599 (setq calc-den-units (concat "(" calc-den-units ")")))
602 (message "%s per %s" calc-num-units calc-den-units)
603 (message "%s" calc-num-units))
605 (message "1 per %s" calc-den-units)
606 (message "No units in expression"))))))
608 (defun calc-explain-units-rec (expr pow)
609 (let ((u (math-check-unit-name expr))
611 (if (and u (not (math-zerop pow)))
612 (let ((name (or (nth 2 u) (symbol-name (car u)))))
613 (if (eq (aref name 0) ?\*)
614 (setq name (substring name 1)))
615 (if (string-match "[^a-zA-Zα-ωΑ-Ω0-9']" name)
616 (if (string-match "^[a-zA-Zα-ωΑ-Ω0-9' ()]*$" name)
617 (while (setq pos (string-match "[ ()]" name))
618 (setq name (concat (substring name 0 pos)
619 (if (eq (aref name pos) 32) "-" "")
620 (substring name (1+ pos)))))
621 (setq name (concat "(" name ")"))))
622 (or (eq (nth 1 expr) (car u))
623 (setq name (concat (nth 2 (assq (aref (symbol-name
626 (if (and (string-match "[^a-zA-Zα-ωΑ-Ω0-9']" name)
627 (not (memq (car u) '(mHg gf))))
630 (cond ((or (math-equal-int pow 1)
631 (math-equal-int pow -1)))
632 ((or (math-equal-int pow 2)
633 (math-equal-int pow -2))
634 (if (equal (nth 4 u) '((m . 1)))
635 (setq name (concat "Square-" name))
636 (setq name (concat name "-squared"))))
637 ((or (math-equal-int pow 3)
638 (math-equal-int pow -3))
639 (if (equal (nth 4 u) '((m . 1)))
640 (setq name (concat "Cubic-" name))
641 (setq name (concat name "-cubed"))))
643 (setq name (concat name "^"
644 (math-format-number (math-abs pow))))))
646 (setq calc-num-units (if calc-num-units
647 (concat calc-num-units " " name)
649 (setq calc-den-units (if calc-den-units
650 (concat calc-den-units " " name)
652 (cond ((eq (car-safe expr) '*)
653 (calc-explain-units-rec (nth 1 expr) pow)
654 (calc-explain-units-rec (nth 2 expr) pow))
655 ((eq (car-safe expr) '/)
656 (calc-explain-units-rec (nth 1 expr) pow)
657 (calc-explain-units-rec (nth 2 expr) (- pow)))
658 ((memq (car-safe expr) '(neg + -))
659 (calc-explain-units-rec (nth 1 expr) pow))
660 ((and (eq (car-safe expr) '^)
661 (math-realp (nth 2 expr)))
662 (calc-explain-units-rec (nth 1 expr)
663 (math-mul pow (nth 2 expr))))))))
665 (defun calc-simplify-units ()
668 (calc-with-default-simplification
669 (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1))))))
671 (defun calc-view-units-table (n)
673 (and n (setq math-units-table-buffer-valid nil))
674 (let ((win (get-buffer-window "*Units Table*")))
677 math-units-table-buffer-valid)
679 (bury-buffer (window-buffer win))
680 (let ((curwin (selected-window)))
682 (switch-to-buffer nil)
683 (select-window curwin)))
684 (math-build-units-table-buffer nil))))
686 (defun calc-enter-units-table (n)
688 (and n (setq math-units-table-buffer-valid nil))
689 (math-build-units-table-buffer t)
690 (message "%s" (substitute-command-keys "Type \\[calc] to return to the Calculator")))
692 (defun calc-define-unit (uname desc &optional disp)
693 (interactive "SDefine unit name: \nsDescription: \nP")
694 (if disp (setq disp (read-string "Display definition: ")))
696 (let ((form (calc-top-n 1))
697 (unit (assq uname math-additional-units)))
699 (setq math-additional-units
700 (cons (setq unit (list uname nil nil nil nil))
701 math-additional-units)
702 math-units-table nil))
703 (setcar (cdr unit) (and (not (and (eq (car-safe form) 'var)
704 (eq (nth 1 form) uname)))
705 (not (math-equal-int form 1))
706 (math-format-flat-expr form 0)))
707 (setcar (cdr (cdr unit)) (and (not (equal desc ""))
710 (setcar (cdr (cdr (cdr (cdr unit)))) disp))))
711 (calc-invalidate-units-table))
713 (defun calc-undefine-unit (uname)
714 (interactive "SUndefine unit name: ")
716 (let ((unit (assq uname math-additional-units)))
718 (if (assq uname math-standard-units)
719 (error "\"%s\" is a predefined unit name" uname)
720 (error "Unit name \"%s\" not found" uname)))
721 (setq math-additional-units (delq unit math-additional-units)
722 math-units-table nil)))
723 (calc-invalidate-units-table))
725 (defun calc-invalidate-units-table ()
726 (setq math-units-table nil)
727 (let ((buf (get-buffer "*Units Table*")))
729 (with-current-buffer buf
731 (goto-char (point-min))
732 (if (looking-at "Calculator Units Table")
733 (let ((inhibit-read-only t))
734 (insert "(Obsolete) "))))))))
736 (defun calc-get-unit-definition (uname)
737 (interactive "SGet definition for unit: ")
739 (math-build-units-table)
740 (let ((unit (assq uname math-units-table)))
742 (error "Unit name \"%s\" not found" uname))
743 (let ((msg (nth 2 unit)))
745 (if (string-match "^\\*" msg)
746 (setq msg (substring msg 1)))
747 (setq msg (symbol-name uname)))
750 (calc-enter-result 0 "ugdf" (nth 1 unit))
751 (message "Derived unit: %s" msg))
752 (calc-enter-result 0 "ugdf" (list 'var uname
755 (symbol-name uname)))))
756 (message "Base unit: %s" msg))))))
758 (defun calc-permanent-units ()
762 (set-buffer (find-file-noselect (substitute-in-file-name
763 calc-settings-file)))
764 (goto-char (point-min))
765 (if (and (search-forward ";;; Custom units stored by Calc" nil t)
769 (search-forward "\n;;; End of custom units" nil t)))
773 (delete-region pos (point)))
774 (goto-char (point-max))
777 (insert ";;; Custom units stored by Calc on " (current-time-string) "\n")
778 (if math-additional-units
780 (insert "(setq math-additional-units '(\n")
781 (let ((list math-additional-units))
783 (insert " (" (symbol-name (car (car list))) " "
784 (if (nth 1 (car list))
785 (if (stringp (nth 1 (car list)))
786 (prin1-to-string (nth 1 (car list)))
787 (prin1-to-string (math-format-flat-expr
788 (nth 1 (car list)) 0)))
791 (prin1-to-string (nth 2 (car list)))
793 (setq list (cdr list))))
795 (insert ";;; (no custom units defined)\n"))
796 (insert ";;; End of custom units\n")
800 ;; The variable math-cu-unit-list is local to math-build-units-table,
801 ;; but is used by math-compare-unit-names, which is called (indirectly)
802 ;; by math-build-units-table.
803 ;; math-cu-unit-list is also local to math-convert-units, but is used
804 ;; by math-convert-units-rec, which is called by math-convert-units.
805 (defvar math-cu-unit-list)
807 (defun math-build-units-table ()
809 (let* ((combined-units (append math-additional-units
810 math-standard-units))
811 (math-cu-unit-list (mapcar 'car combined-units))
813 (message "Building units table...")
814 (setq math-units-table-buffer-valid nil)
815 (setq tab (mapcar (function
819 (if (stringp (nth 1 x))
820 (let ((exp (math-read-plain-expr
822 (if (eq (car-safe exp) 'error)
823 (error "Format error in definition of %s in units table: %s"
830 (list (cons (car x) 1)))
833 (let ((math-units-table tab))
834 (mapc 'math-find-base-units tab))
835 (message "Building units table...done")
836 (setq math-units-table tab))))
838 ;; The variables math-fbu-base and math-fbu-entry are local to
839 ;; math-find-base-units, but are used by math-find-base-units-rec,
840 ;; which is called by math-find-base-units.
841 (defvar math-fbu-base)
842 (defvar math-fbu-entry)
844 (defun math-find-base-units (math-fbu-entry)
845 (if (eq (nth 4 math-fbu-entry) 'boom)
846 (error "Circular definition involving unit %s" (car math-fbu-entry)))
847 (or (nth 4 math-fbu-entry)
849 (setcar (nthcdr 4 math-fbu-entry) 'boom)
850 (math-find-base-units-rec (nth 1 math-fbu-entry) 1)
852 (error "Dimensionless definition for unit %s" (car math-fbu-entry)))
853 (while (eq (cdr (car math-fbu-base)) 0)
854 (setq math-fbu-base (cdr math-fbu-base)))
855 (let ((b math-fbu-base))
857 (if (eq (cdr (car (cdr b))) 0)
858 (setcdr b (cdr (cdr b)))
860 (setq math-fbu-base (sort math-fbu-base 'math-compare-unit-names))
861 (setcar (nthcdr 4 math-fbu-entry) math-fbu-base)
864 (defun math-compare-unit-names (a b)
865 (memq (car b) (cdr (memq (car a) math-cu-unit-list))))
867 (defun math-find-base-units-rec (expr pow)
868 (let ((u (math-check-unit-name expr)))
870 (let ((ulist (math-find-base-units u)))
872 (let ((p (* (cdr (car ulist)) pow))
873 (old (assq (car (car ulist)) math-fbu-base)))
875 (setcdr old (+ (cdr old) p))
877 (cons (cons (car (car ulist)) p) math-fbu-base))))
878 (setq ulist (cdr ulist)))))
879 ((math-scalarp expr))
880 ((and (eq (car expr) '^)
881 (integerp (nth 2 expr)))
882 (math-find-base-units-rec (nth 1 expr) (* pow (nth 2 expr))))
884 (math-find-base-units-rec (nth 1 expr) pow)
885 (math-find-base-units-rec (nth 2 expr) pow))
887 (math-find-base-units-rec (nth 1 expr) pow)
888 (math-find-base-units-rec (nth 2 expr) (- pow)))
889 ((eq (car expr) 'neg)
890 (math-find-base-units-rec (nth 1 expr) pow))
892 (math-find-base-units-rec (nth 1 expr) pow))
893 ((eq (car expr) 'var)
894 (or (eq (nth 1 expr) 'pi)
895 (error "Unknown name %s in defining expression for unit %s"
896 (nth 1 expr) (car math-fbu-entry))))
897 ((equal expr '(calcFunc-ln 10)))
898 (t (error "Malformed defining expression for unit %s" (car math-fbu-entry))))))
901 (defun math-units-in-expr-p (expr sub-exprs)
903 (if (eq (car expr) 'var)
904 (math-check-unit-name expr)
906 (memq (car expr) '(* / ^)))
907 (or (math-units-in-expr-p (nth 1 expr) sub-exprs)
908 (math-units-in-expr-p (nth 2 expr) sub-exprs))))))
910 (defun math-only-units-in-expr-p (expr)
912 (if (eq (car expr) 'var)
913 (math-check-unit-name expr)
914 (if (memq (car expr) '(* /))
915 (and (math-only-units-in-expr-p (nth 1 expr))
916 (math-only-units-in-expr-p (nth 2 expr)))
917 (and (eq (car expr) '^)
918 (and (math-only-units-in-expr-p (nth 1 expr))
919 (math-realp (nth 2 expr))))))))
921 (defun math-single-units-in-expr-p (expr)
922 (cond ((math-scalarp expr) nil)
923 ((eq (car expr) 'var)
924 (math-check-unit-name expr))
926 (let ((u1 (math-single-units-in-expr-p (nth 1 expr)))
927 (u2 (math-single-units-in-expr-p (nth 2 expr))))
928 (or (and u1 u2 'wrong)
932 (if (math-units-in-expr-p (nth 2 expr) nil)
934 (math-single-units-in-expr-p (nth 1 expr))))
937 (defun math-consistent-units-p (expr newunits)
938 "Non-nil if EXPR and NEWUNITS have consistent units."
940 (and (eq (car-safe newunits) 'var)
941 (assq (nth 1 newunits) math-standard-units-systems))
942 (math-numberp (math-get-units (list '/ expr newunits)))))
944 (defun math-check-unit-consistency (expr units)
945 "Give an error if EXPR and UNITS do not have consistent units."
946 (unless (math-consistent-units-p expr units)
947 (error "New units (%s) are inconsistent with current units (%s)"
948 (math-format-value units)
949 (math-format-value (math-get-units expr)))))
951 (defun math-check-unit-name (v)
952 (and (eq (car-safe v) 'var)
953 (or (assq (nth 1 v) (or math-units-table (math-build-units-table)))
954 (let ((name (symbol-name (nth 1 v))))
955 (and (> (length name) 1)
956 (assq (aref name 0) math-unit-prefixes)
957 (or (assq (intern (substring name 1)) math-units-table)
958 (and (eq (aref name 0) ?M)
960 (eq (aref name 1) ?e)
961 (eq (aref name 2) ?g)
962 (assq (intern (substring name 3))
963 math-units-table))))))))
965 ;; The variable math-which-standard is local to math-to-standard-units,
966 ;; but is used by math-to-standard-rec, which is called by
967 ;; math-to-standard-units.
968 (defvar math-which-standard)
970 (defun math-to-standard-units (expr math-which-standard)
971 (math-to-standard-rec expr))
973 (defun math-to-standard-rec (expr)
974 (if (eq (car-safe expr) 'var)
975 (let ((u (math-check-unit-name expr))
980 (setq expr (math-to-standard-rec (nth 1 u)))
981 (let ((st (assq (car u) math-which-standard)))
983 (setq expr (nth 1 st))
984 (setq expr (list 'var (car u)
985 (intern (concat "var-"
991 (nth 1 (assq (aref (symbol-name base) 0)
1000 (and (eq (car-safe expr) 'calcFunc-subscr)
1001 (eq (car-safe (nth 1 expr)) 'var)))
1004 (mapcar 'math-to-standard-rec (cdr expr))))))
1006 (defun math-apply-units (expr units ulist &optional pure)
1007 (setq expr (math-simplify-units expr))
1011 (or (math-numberp expr)
1012 (error "Incompatible units"))
1014 (setq value (math-div expr (nth 1 (car ulist)))
1015 value (math-floor (let ((calc-internal-prec
1016 (1- calc-internal-prec)))
1017 (math-normalize value)))
1018 new (math-add new (math-mul value (car (car ulist))))
1019 expr (math-sub expr (math-mul value (nth 1 (car ulist))))
1021 (math-add new (math-mul (math-div expr (nth 1 (car ulist)))
1022 (car (car ulist)))))
1025 (math-simplify-units (list '* expr units)))))
1027 (defvar math-decompose-units-cache nil)
1028 (defun math-decompose-units (units)
1029 (let ((u (math-check-unit-name units)))
1030 (and u (eq (car-safe (nth 1 u)) '+)
1031 (setq units (nth 1 u))))
1032 (setq units (calcFunc-expand units))
1033 (and (eq (car-safe units) '+)
1034 (let ((entry (list units calc-internal-prec calc-prefer-frac)))
1035 (or (equal entry (car math-decompose-units-cache))
1039 (while (eq (car-safe utemp) '+)
1040 (setq ulist (cons (math-decompose-unit-part (nth 2 utemp))
1042 utemp (nth 1 utemp)))
1043 (setq ulist (cons (math-decompose-unit-part utemp) ulist)
1045 (while (setq utemp (cdr utemp))
1046 (unless (equal (nth 2 (car utemp)) (nth 2 (car ulist)))
1047 (error "Inconsistent units in sum")))
1048 (setq math-decompose-units-cache
1053 (not (Math-lessp (nth 1 x)
1055 (cdr math-decompose-units-cache))))
1057 (defun math-decompose-unit-part (unit)
1059 (math-is-multiple (math-simplify-units (math-to-standard-units
1063 ;; The variable math-fcu-u is local to math-find-compatible-unit,
1064 ;; but is used by math-find-compatible-rec which is called by
1065 ;; math-find-compatible-unit.
1068 (defun math-find-compatible-unit (expr unit)
1069 (let ((math-fcu-u (math-check-unit-name unit)))
1071 (math-find-compatible-unit-rec expr 1))))
1073 (defun math-find-compatible-unit-rec (expr pow)
1074 (cond ((eq (car-safe expr) '*)
1075 (or (math-find-compatible-unit-rec (nth 1 expr) pow)
1076 (math-find-compatible-unit-rec (nth 2 expr) pow)))
1077 ((eq (car-safe expr) '/)
1078 (or (math-find-compatible-unit-rec (nth 1 expr) pow)
1079 (math-find-compatible-unit-rec (nth 2 expr) (- pow))))
1080 ((and (eq (car-safe expr) '^)
1081 (integerp (nth 2 expr)))
1082 (math-find-compatible-unit-rec (nth 1 expr) (* pow (nth 2 expr))))
1084 (let ((u2 (math-check-unit-name expr)))
1085 (if (equal (nth 4 math-fcu-u) (nth 4 u2))
1086 (cons expr pow))))))
1088 ;; The variables math-cu-new-units and math-cu-pure are local to
1089 ;; math-convert-units, but are used by math-convert-units-rec,
1090 ;; which is called by math-convert-units.
1091 (defvar math-cu-new-units)
1092 (defvar math-cu-pure)
1094 (defun math-convert-units (expr math-cu-new-units &optional math-cu-pure)
1095 (if (eq (car-safe math-cu-new-units) 'var)
1096 (let ((unew (assq (nth 1 math-cu-new-units)
1097 (math-build-units-table))))
1098 (if (eq (car-safe (nth 1 unew)) '+)
1099 (setq math-cu-new-units (nth 1 unew)))))
1100 (math-with-extra-prec 2
1101 (let ((compat (and (not math-cu-pure)
1102 (math-find-compatible-unit expr math-cu-new-units)))
1103 (math-cu-unit-list nil)
1104 (math-combining-units nil))
1106 (math-simplify-units
1107 (math-mul (math-mul (math-simplify-units
1108 (math-div expr (math-pow (car compat)
1110 (math-pow math-cu-new-units (cdr compat)))
1111 (math-simplify-units
1112 (math-to-standard-units
1113 (math-pow (math-div (car compat) math-cu-new-units)
1116 (when (setq math-cu-unit-list (math-decompose-units math-cu-new-units))
1117 (setq math-cu-new-units (nth 2 (car math-cu-unit-list))))
1118 (when (eq (car-safe expr) '+)
1119 (setq expr (math-simplify-units expr)))
1120 (if (math-units-in-expr-p expr t)
1121 (math-convert-units-rec expr)
1122 (math-apply-units (math-to-standard-units
1123 (list '/ expr math-cu-new-units) nil)
1124 math-cu-new-units math-cu-unit-list math-cu-pure))))))
1126 (defun math-convert-units-rec (expr)
1127 (if (math-units-in-expr-p expr nil)
1128 (math-apply-units (math-to-standard-units
1129 (list '/ expr math-cu-new-units) nil)
1130 math-cu-new-units math-cu-unit-list math-cu-pure)
1131 (if (Math-primp expr)
1134 (mapcar 'math-convert-units-rec (cdr expr))))))
1136 (defun math-convert-temperature (expr old new &optional pure)
1137 (let* ((units (math-single-units-in-expr-p expr))
1139 (if (or (null units)
1140 (equal (nth 1 old) (car units)))
1141 (math-check-unit-name old)
1142 (error "Inconsistent temperature units"))
1144 (unew (math-check-unit-name new)))
1145 (unless (and (consp unew) (nth 3 unew))
1146 (error "Not a valid temperature unit"))
1147 (unless (and (consp uold) (nth 3 uold))
1148 (error "Not a pure temperature expression"))
1149 (let ((v (car uold)))
1150 (setq expr (list '/ expr (list 'var v
1151 (intern (concat "var-"
1152 (symbol-name v)))))))
1153 (or (eq (nth 3 uold) (nth 3 unew))
1154 (cond ((eq (nth 3 uold) 'K)
1155 (setq expr (list '- expr '(/ 27315 100)))
1156 (if (eq (nth 3 unew) 'F)
1157 (setq expr (list '+ (list '* expr '(/ 9 5)) 32))))
1158 ((eq (nth 3 uold) 'C)
1159 (if (eq (nth 3 unew) 'F)
1160 (setq expr (list '+ (list '* expr '(/ 9 5)) 32))
1161 (setq expr (list '+ expr '(/ 27315 100)))))
1163 (setq expr (list '* (list '- expr 32) '(/ 5 9)))
1164 (if (eq (nth 3 unew) 'K)
1165 (setq expr (list '+ expr '(/ 27315 100)))))))
1168 (list '* expr new))))
1172 (defun math-simplify-units (a)
1173 (let ((math-simplifying-units t)
1174 (calc-matrix-mode 'scalar))
1176 (defalias 'calcFunc-usimplify 'math-simplify-units)
1178 ;; The function created by math-defsimplify uses the variable
1179 ;; math-simplify-expr, and so is used by functions in math-defsimplify
1180 (defvar math-simplify-expr)
1182 (math-defsimplify (+ -)
1183 (and math-simplifying-units
1184 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1185 (let* ((units (math-extract-units (nth 1 math-simplify-expr)))
1186 (ratio (math-simplify (math-to-standard-units
1187 (list '/ (nth 2 math-simplify-expr) units) nil))))
1188 (if (math-units-in-expr-p ratio nil)
1190 (calc-record-why "*Inconsistent units" math-simplify-expr)
1192 (list '* (math-add (math-remove-units (nth 1 math-simplify-expr))
1193 (if (eq (car math-simplify-expr) '-)
1194 (math-neg ratio) ratio))
1198 (math-simplify-units-prod))
1200 (defun math-simplify-units-prod ()
1201 (and math-simplifying-units
1202 calc-autorange-units
1203 (Math-realp (nth 1 math-simplify-expr))
1204 (let* ((num (math-float (nth 1 math-simplify-expr)))
1205 (xpon (calcFunc-xpon num))
1206 (unitp (cdr (cdr math-simplify-expr)))
1208 (pow (if (eq (car math-simplify-expr) '*) 1 -1))
1210 (and (eq (car-safe unit) '*)
1211 (setq unitp (cdr unit)
1213 (and (eq (car-safe unit) '^)
1214 (integerp (nth 2 unit))
1215 (setq pow (* pow (nth 2 unit))
1218 (and (setq u (math-check-unit-name unit))
1221 (>= xpon (if (eq (car u) 'm) 1 3)))
1224 (reverse math-unit-prefixes)
1225 math-unit-prefixes))
1228 (or (eq (car u) (nth 1 unit))
1233 math-unit-prefixes))))))
1234 (setq xpon (+ xpon uxpon))
1236 (or (memq (car (car p)) '(?d ?D ?h ?H))
1237 (and (eq (car (car p)) ?c)
1238 (not (eq (car u) 'm)))
1239 (< xpon (setq pxpon (* (nth 2 (nth 1 (car p)))
1242 (setq pname (math-build-var-name
1243 (if (eq (car (car p)) 0)
1245 (concat (char-to-string
1249 (and (/= (car (car p)) 0)
1251 math-units-table)))))
1255 (or (not (eq p pref))
1256 (< xpon (+ pxpon (* (math-abs pow) 3))))
1258 (setcar (cdr math-simplify-expr)
1259 (let ((calc-prefer-frac nil))
1260 (calcFunc-scf (nth 1 math-simplify-expr)
1262 (setcar unitp pname)
1263 math-simplify-expr)))))))
1265 (defvar math-try-cancel-units)
1268 (and math-simplifying-units
1269 (let ((np (cdr math-simplify-expr))
1270 (math-try-cancel-units 0)
1272 (setq n (if (eq (car-safe (nth 2 math-simplify-expr)) '*)
1273 (cdr (nth 2 math-simplify-expr))
1274 (nthcdr 2 math-simplify-expr)))
1275 (if (math-realp (car n))
1277 (setcar (cdr math-simplify-expr) (math-mul (nth 1 math-simplify-expr)
1278 (let ((calc-prefer-frac nil))
1279 (math-div 1 (car n)))))
1281 (while (eq (car-safe (setq n (car np))) '*)
1282 (math-simplify-units-divisor (cdr n) (cdr (cdr math-simplify-expr)))
1283 (setq np (cdr (cdr n))))
1284 (math-simplify-units-divisor np (cdr (cdr math-simplify-expr)))
1285 (if (eq math-try-cancel-units 0)
1286 (let* ((math-simplifying-units nil)
1287 (base (math-simplify
1288 (math-to-standard-units math-simplify-expr nil))))
1289 (if (Math-numberp base)
1290 (setq math-simplify-expr base))))
1291 (if (eq (car-safe math-simplify-expr) '/)
1292 (math-simplify-units-prod))
1293 math-simplify-expr)))
1295 (defun math-simplify-units-divisor (np dp)
1298 (while (eq (car-safe (setq d (car dp))) '*)
1299 (when (setq temp (math-simplify-units-quotient n (nth 1 d)))
1300 (setcar np (setq n temp))
1302 (setq dp (cdr (cdr d))))
1303 (when (setq temp (math-simplify-units-quotient n d))
1304 (setcar np (setq n temp))
1307 ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.
1308 (defun math-simplify-units-quotient (n d)
1311 (when (and (eq (car-safe n) '^)
1312 (integerp (nth 2 n)))
1313 (setq pow1 (nth 2 n) n (nth 1 n)))
1314 (when (and (eq (car-safe d) '^)
1315 (integerp (nth 2 d)))
1316 (setq pow2 (nth 2 d) d (nth 1 d)))
1317 (let ((un (math-check-unit-name n))
1318 (ud (math-check-unit-name d)))
1320 (if (and (equal (nth 4 un) (nth 4 ud))
1323 (math-to-standard-units (list '/ n d) nil)
1324 (list '^ (math-to-standard-units (list '/ n d) nil) pow1))
1331 (and (eq (car (car un)) (car (car ud1)))
1332 (setq math-try-cancel-units
1333 (+ math-try-cancel-units
1334 (- (* (cdr (car un)) pow1)
1335 (* (cdr (car ud)) pow2)))))
1336 (setq ud1 (cdr ud1)))
1341 (and math-simplifying-units
1342 (math-realp (nth 2 math-simplify-expr))
1343 (if (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
1344 (list (car (nth 1 math-simplify-expr))
1345 (list '^ (nth 1 (nth 1 math-simplify-expr))
1346 (nth 2 math-simplify-expr))
1347 (list '^ (nth 2 (nth 1 math-simplify-expr))
1348 (nth 2 math-simplify-expr)))
1349 (math-simplify-units-pow (nth 1 math-simplify-expr)
1350 (nth 2 math-simplify-expr)))))
1352 (math-defsimplify calcFunc-sqrt
1353 (and math-simplifying-units
1354 (if (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
1355 (list (car (nth 1 math-simplify-expr))
1356 (list 'calcFunc-sqrt (nth 1 (nth 1 math-simplify-expr)))
1357 (list 'calcFunc-sqrt (nth 2 (nth 1 math-simplify-expr))))
1358 (math-simplify-units-pow (nth 1 math-simplify-expr) '(frac 1 2)))))
1360 (math-defsimplify (calcFunc-floor
1370 (and math-simplifying-units
1371 (= (length math-simplify-expr) 2)
1372 (if (math-only-units-in-expr-p (nth 1 math-simplify-expr))
1373 (nth 1 math-simplify-expr)
1374 (if (and (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
1375 (or (math-only-units-in-expr-p
1376 (nth 1 (nth 1 math-simplify-expr)))
1377 (math-only-units-in-expr-p
1378 (nth 2 (nth 1 math-simplify-expr)))))
1379 (list (car (nth 1 math-simplify-expr))
1380 (cons (car math-simplify-expr)
1381 (cons (nth 1 (nth 1 math-simplify-expr))
1382 (cdr (cdr math-simplify-expr))))
1383 (cons (car math-simplify-expr)
1384 (cons (nth 2 (nth 1 math-simplify-expr))
1385 (cdr (cdr math-simplify-expr)))))))))
1387 (defun math-simplify-units-pow (a pow)
1388 (if (and (eq (car-safe a) '^)
1389 (math-check-unit-name (nth 1 a))
1390 (math-realp (nth 2 a)))
1391 (list '^ (nth 1 a) (math-mul pow (nth 2 a)))
1392 (let* ((u (math-check-unit-name a))
1393 (pf (math-to-simple-fraction pow))
1394 (d (and (eq (car-safe pf) 'frac) (nth 2 pf))))
1396 (math-units-are-multiple u d)
1397 (list '^ (math-to-standard-units a nil) pow)))))
1400 (defun math-units-are-multiple (u n)
1402 (while (and u (= (% (cdr (car u)) n) 0))
1406 (math-defsimplify calcFunc-sin
1407 (and math-simplifying-units
1408 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1409 (let ((rad (math-simplify-units
1411 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1412 (calc-angle-mode 'rad))
1413 (and (eq (car-safe rad) '*)
1414 (math-realp (nth 1 rad))
1415 (eq (car-safe (nth 2 rad)) 'var)
1416 (eq (nth 1 (nth 2 rad)) 'rad)
1417 (list 'calcFunc-sin (nth 1 rad))))))
1419 (math-defsimplify calcFunc-cos
1420 (and math-simplifying-units
1421 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1422 (let ((rad (math-simplify-units
1424 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1425 (calc-angle-mode 'rad))
1426 (and (eq (car-safe rad) '*)
1427 (math-realp (nth 1 rad))
1428 (eq (car-safe (nth 2 rad)) 'var)
1429 (eq (nth 1 (nth 2 rad)) 'rad)
1430 (list 'calcFunc-cos (nth 1 rad))))))
1432 (math-defsimplify calcFunc-tan
1433 (and math-simplifying-units
1434 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1435 (let ((rad (math-simplify-units
1437 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1438 (calc-angle-mode 'rad))
1439 (and (eq (car-safe rad) '*)
1440 (math-realp (nth 1 rad))
1441 (eq (car-safe (nth 2 rad)) 'var)
1442 (eq (nth 1 (nth 2 rad)) 'rad)
1443 (list 'calcFunc-tan (nth 1 rad))))))
1445 (math-defsimplify calcFunc-sec
1446 (and math-simplifying-units
1447 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1448 (let ((rad (math-simplify-units
1450 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1451 (calc-angle-mode 'rad))
1452 (and (eq (car-safe rad) '*)
1453 (math-realp (nth 1 rad))
1454 (eq (car-safe (nth 2 rad)) 'var)
1455 (eq (nth 1 (nth 2 rad)) 'rad)
1456 (list 'calcFunc-sec (nth 1 rad))))))
1458 (math-defsimplify calcFunc-csc
1459 (and math-simplifying-units
1460 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1461 (let ((rad (math-simplify-units
1463 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1464 (calc-angle-mode 'rad))
1465 (and (eq (car-safe rad) '*)
1466 (math-realp (nth 1 rad))
1467 (eq (car-safe (nth 2 rad)) 'var)
1468 (eq (nth 1 (nth 2 rad)) 'rad)
1469 (list 'calcFunc-csc (nth 1 rad))))))
1471 (math-defsimplify calcFunc-cot
1472 (and math-simplifying-units
1473 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1474 (let ((rad (math-simplify-units
1476 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1477 (calc-angle-mode 'rad))
1478 (and (eq (car-safe rad) '*)
1479 (math-realp (nth 1 rad))
1480 (eq (car-safe (nth 2 rad)) 'var)
1481 (eq (nth 1 (nth 2 rad)) 'rad)
1482 (list 'calcFunc-cot (nth 1 rad))))))
1485 (defun math-remove-units (expr)
1486 (if (math-check-unit-name expr)
1488 (if (Math-primp expr)
1491 (mapcar 'math-remove-units (cdr expr))))))
1493 (defun math-extract-units (expr)
1495 ((memq (car-safe expr) '(* /))
1497 (mapcar 'math-extract-units (cdr expr))))
1499 (eq (car-safe expr) '^)
1500 (math-check-unit-name (nth 1 expr)))
1502 ((math-check-unit-name expr) expr)
1505 (defun math-build-units-table-buffer (enter-buffer)
1506 (if (not (and math-units-table math-units-table-buffer-valid
1507 (get-buffer "*Units Table*")))
1508 (let ((buf (get-buffer-create "*Units Table*"))
1509 (uptr (math-build-units-table))
1510 (calc-language (if (eq calc-language 'big) nil calc-language))
1511 (calc-float-format '(float 0))
1512 (calc-group-digits nil)
1513 (calc-number-radix 10)
1514 (calc-twos-complement-mode nil)
1515 (calc-point-char ".")
1519 (message "Formatting units table...")
1521 (let ((inhibit-read-only t))
1523 (insert "Calculator Units Table:\n\n")
1524 (insert "(All definitions are exact unless marked with an asterisk (*).)\n\n")
1525 (insert "Unit Type Definition Description\n\n")
1529 (when (eq (car u) 'm)
1531 (setq shadowed (and std (assq (car u) math-additional-units)))
1534 (eq (aref name 0) ?\*))
1535 (unless (eq uptr math-units-table)
1537 (setq name (substring name 1)))
1539 (and shadowed (insert "("))
1540 (insert (symbol-name (car u)))
1541 (and shadowed (insert ")"))
1545 (insert (symbol-name (nth 3 u))))
1551 (and shadowed (insert "("))
1555 (insert (math-format-value (nth 1 u) 80))
1556 (insert (symbol-name (car u)))))
1557 (and shadowed (insert ")"))
1563 (insert " (redefined above)")
1565 (insert " (base unit)")))
1567 (setq uptr (cdr uptr)))
1568 (insert "\n\nUnit Prefix Table:\n\n")
1569 (setq uptr math-unit-prefixes)
1572 (insert " " (char-to-string (car u)))
1573 (if (equal (nth 1 u) (nth 1 (nth 1 uptr)))
1574 (insert " " (char-to-string (car (car (setq uptr (cdr uptr)))))
1577 (insert "10^" (int-to-string (nth 2 (nth 1 u))))
1579 (insert " " (nth 2 u) "\n")
1580 (while (eq (car (car (setq uptr (cdr uptr)))) 0)))
1582 (insert "(**) When in TeX or LaTeX display mode, the TeX specific unit\n"
1583 "names will not use the `tex' prefix; the unit name for a\n"
1584 "TeX point will be `pt' instead of `texpt', for example.\n"
1585 "To avoid conflicts, the unit names for pint and parsec will\n"
1586 "be `pint' and `parsec' instead of `pt' and `pc'."))
1588 (message "Formatting units table...done"))
1589 (setq math-units-table-buffer-valid t)
1590 (let ((oldbuf (current-buffer)))
1592 (goto-char (point-min))
1593 (set-buffer oldbuf))
1596 (display-buffer buf)))
1598 (pop-to-buffer (get-buffer "*Units Table*"))
1599 (display-buffer (get-buffer "*Units Table*")))))
1601 ;;; Logarithmic units functions
1603 (defvar math-logunits '((var dB var-dB)
1606 (defun math-conditional-apply (fn &rest args)
1607 "Evaluate f(args) unless in symbolic mode.
1608 In symbolic mode, return the list (fn args)."
1609 (if calc-symbolic-mode
1613 (defun math-conditional-pow (a b)
1614 "Evaluate a^b unless in symbolic mode.
1615 In symbolic mode, return the list (^ a b)."
1616 (if calc-symbolic-mode
1620 (defun math-extract-logunits (expr)
1621 (if (memq (car-safe expr) '(* /))
1623 (mapcar 'math-extract-logunits (cdr expr)))
1624 (if (memq (car-safe expr) '(^))
1625 (list '^ (math-extract-logunits (nth 1 expr)) (nth 2 expr))
1626 (if (member expr math-logunits) expr 1))))
1628 (defun math-logunits-add (a b neg power)
1629 (let ((aunit (math-simplify (math-extract-logunits a))))
1630 (if (not (eq (car-safe aunit) 'var))
1631 (calc-record-why "*Improper logarithmic unit" aunit)
1632 (let* ((units (math-extract-units a))
1633 (acoeff (math-simplify (math-remove-units a)))
1634 (bcoeff (math-simplify (math-to-standard-units
1635 (list '/ b units) nil))))
1636 (if (math-units-in-expr-p bcoeff nil)
1637 (calc-record-why "*Inconsistent units" nil)
1639 (or (math-lessp acoeff bcoeff)
1640 (math-equal acoeff bcoeff)))
1641 (calc-record-why "*Improper coefficients" nil)
1643 (if (equal aunit '(var dB var-dB))
1644 (let ((coef (if power 10 20)))
1646 (math-conditional-apply 'calcFunc-log10
1649 (math-conditional-pow 10 (math-div acoeff coef))
1650 (math-conditional-pow 10 (math-div bcoeff coef)))
1652 (math-conditional-pow 10 (math-div acoeff coef))
1653 (math-conditional-pow 10 (math-div bcoeff coef)))))))
1654 (let ((coef (if power 2 1)))
1656 (math-conditional-apply 'calcFunc-ln
1659 (math-conditional-apply 'calcFunc-exp (math-mul coef acoeff))
1660 (math-conditional-apply 'calcFunc-exp (math-mul coef bcoeff)))
1662 (math-conditional-apply 'calcFunc-exp (math-mul coef acoeff))
1663 (math-conditional-apply 'calcFunc-exp (math-mul coef bcoeff)))))
1667 (defun calcFunc-lufadd (a b)
1668 (math-logunits-add a b nil nil))
1670 (defun calcFunc-lupadd (a b)
1671 (math-logunits-add a b nil t))
1673 (defun calcFunc-lufsub (a b)
1674 (math-logunits-add a b t nil))
1676 (defun calcFunc-lupsub (a b)
1677 (math-logunits-add a b t t))
1679 (defun calc-lu-plus (arg)
1682 (if (calc-is-inverse)
1683 (if (calc-is-hyperbolic)
1684 (calc-binary-op "lu-" 'calcFunc-lufsub arg)
1685 (calc-binary-op "lu-" 'calcFunc-lupsub arg))
1686 (if (calc-is-hyperbolic)
1687 (calc-binary-op "lu+" 'calcFunc-lufadd arg)
1688 (calc-binary-op "lu+" 'calcFunc-lupadd arg)))))
1690 (defun calc-lu-minus (arg)
1693 (if (calc-is-inverse)
1694 (if (calc-is-hyperbolic)
1695 (calc-binary-op "lu+" 'calcFunc-lufadd arg)
1696 (calc-binary-op "lu+" 'calcFunc-lupadd arg))
1697 (if (calc-is-hyperbolic)
1698 (calc-binary-op "lu-" 'calcFunc-lufsub arg)
1699 (calc-binary-op "lu-" 'calcFunc-lupsub arg)))))
1701 (defun math-logunits-mul (a b power)
1702 (let (logunit coef units number)
1705 (setq logunit (math-simplify (math-extract-logunits a)))
1706 (eq (car-safe logunit) 'var)
1707 (eq (math-simplify (math-extract-units b)) 1))
1708 (setq coef (math-simplify (math-remove-units a))
1709 units (math-extract-units a)
1712 (setq logunit (math-simplify (math-extract-logunits b)))
1713 (eq (car-safe logunit) 'var)
1714 (eq (math-simplify (math-extract-units a)) 1))
1715 (setq coef (math-simplify (math-remove-units b))
1716 units (math-extract-units b)
1718 (t (setq logunit nil)))
1721 ((equal logunit '(var dB var-dB))
1726 (math-mul (if power 10 20)
1727 (math-conditional-apply 'calcFunc-log10 number)))
1734 (math-div (math-conditional-apply 'calcFunc-ln number) (if power 2 1)))
1736 (calc-record-why "*Improper units" nil))))
1738 (defun math-logunits-divide (a b power)
1739 (let ((logunit (math-simplify (math-extract-logunits a))))
1740 (if (not (eq (car-safe logunit) 'var))
1741 (calc-record-why "*Improper logarithmic unit" logunit)
1742 (if (math-units-in-expr-p b nil)
1743 (calc-record-why "*Improper units quantity" b)
1744 (let* ((units (math-extract-units a))
1745 (coef (math-simplify (math-remove-units a))))
1747 ((equal logunit '(var dB var-dB))
1752 (math-mul (if power 10 20)
1753 (math-conditional-apply 'calcFunc-log10 b)))
1760 (math-div (math-conditional-apply 'calcFunc-ln b) (if power 2 1)))
1763 (defun calcFunc-lufmul (a b)
1764 (math-logunits-mul a b nil))
1766 (defun calcFunc-lupmul (a b)
1767 (math-logunits-mul a b t))
1769 (defun calc-lu-times (arg)
1772 (if (calc-is-inverse)
1773 (if (calc-is-hyperbolic)
1774 (calc-binary-op "lu/" 'calcFunc-lufdiv arg)
1775 (calc-binary-op "lu/" 'calcFunc-lupdiv arg))
1776 (if (calc-is-hyperbolic)
1777 (calc-binary-op "lu*" 'calcFunc-lufmul arg)
1778 (calc-binary-op "lu*" 'calcFunc-lupmul arg)))))
1780 (defun calcFunc-lufdiv (a b)
1781 (math-logunits-divide a b nil))
1783 (defun calcFunc-lupdiv (a b)
1784 (math-logunits-divide a b t))
1786 (defun calc-lu-divide (arg)
1789 (if (calc-is-inverse)
1790 (if (calc-is-hyperbolic)
1791 (calc-binary-op "lu*" 'calcFunc-lufmul arg)
1792 (calc-binary-op "lu*" 'calcFunc-lupmul arg))
1793 (if (calc-is-hyperbolic)
1794 (calc-binary-op "lu/" 'calcFunc-lufdiv arg)
1795 (calc-binary-op "lu/" 'calcFunc-lupdiv arg)))))
1797 (defun math-logunits-quant (val ref power)
1798 (let* ((units (math-simplify (math-extract-units val)))
1799 (lunit (math-simplify (math-extract-logunits units))))
1800 (if (not (eq (car-safe lunit) 'var))
1801 (calc-record-why "*Improper logarithmic unit" lunit)
1802 (let ((runits (math-simplify (math-div units lunit)))
1803 (coeff (math-simplify (math-div val units))))
1805 (if (equal lunit '(var dB var-dB))
1808 (math-conditional-pow
1815 (math-conditional-apply 'calcFunc-exp
1821 (defvar calc-lu-field-reference)
1822 (defvar calc-lu-power-reference)
1824 (defun calcFunc-lufquant (val &optional ref)
1826 (setq ref (math-read-expr calc-lu-field-reference)))
1827 (math-logunits-quant val ref nil))
1829 (defun calcFunc-lupquant (val &optional ref)
1831 (setq ref (math-read-expr calc-lu-power-reference)))
1832 (math-logunits-quant val ref t))
1834 (defun calc-lu-quant (arg)
1837 (if (calc-is-hyperbolic)
1838 (if (calc-is-option)
1839 (calc-binary-op "lupq" 'calcFunc-lufquant arg)
1840 (calc-unary-op "lupq" 'calcFunc-lufquant arg))
1841 (if (calc-is-option)
1842 (calc-binary-op "lufq" 'calcFunc-lupquant arg)
1843 (calc-unary-op "lufq" 'calcFunc-lupquant arg)))))
1845 (defun math-logunits-level (val ref db power)
1846 "Compute the value of VAL in decibels or nepers."
1847 (let* ((ratio (math-simplify-units (math-div val ref)))
1848 (ratiou (math-simplify-units (math-remove-units ratio)))
1849 (units (math-simplify (math-extract-units ratio))))
1853 (math-mul (if power 10 20)
1854 (math-conditional-apply 'calcFunc-log10 ratiou))
1857 (math-div (math-conditional-apply 'calcFunc-ln ratiou) (if power 2 1))
1861 (defun calcFunc-dbfield (val &optional ref)
1863 (setq ref (math-read-expr calc-lu-field-reference)))
1864 (math-logunits-level val ref t nil))
1866 (defun calcFunc-dbpower (val &optional ref)
1868 (setq ref (math-read-expr calc-lu-power-reference)))
1869 (math-logunits-level val ref t t))
1871 (defun calcFunc-npfield (val &optional ref)
1873 (setq ref (math-read-expr calc-lu-field-reference)))
1874 (math-logunits-level val ref nil nil))
1876 (defun calcFunc-nppower (val &optional ref)
1878 (setq ref (math-read-expr calc-lu-power-reference)))
1879 (math-logunits-level val ref nil t))
1881 (defun calc-db (arg)
1884 (if (calc-is-hyperbolic)
1885 (if (calc-is-option)
1886 (calc-binary-op "ludb" 'calcFunc-dbfield arg)
1887 (calc-unary-op "ludb" 'calcFunc-dbfield arg))
1888 (if (calc-is-option)
1889 (calc-binary-op "ludb" 'calcFunc-dbpower arg)
1890 (calc-unary-op "ludb" 'calcFunc-dbpower arg)))))
1892 (defun calc-np (arg)
1895 (if (calc-is-hyperbolic)
1896 (if (calc-is-option)
1897 (calc-binary-op "lunp" 'calcFunc-npfield arg)
1898 (calc-unary-op "lunp" 'calcFunc-npfield arg))
1899 (if (calc-is-option)
1900 (calc-binary-op "lunp" 'calcFunc-nppower arg)
1901 (calc-unary-op "lunp" 'calcFunc-nppower arg)))))
1906 (defvar calc-note-threshold)
1908 (defun math-midi-round (num)
1909 "Round NUM to an integer N if NUM is within calc-note-threshold cents of N."
1910 (let* ((n (math-round num))
1913 (if (< (math-compare diff
1914 (math-div (math-read-expr calc-note-threshold) 100)) 0)
1918 (defconst math-notes
1919 '(((var C var-C) . 0)
1920 ((var Csharp var-Csharp) . 1)
1921 ; ((var C♯ var-C♯) . 1)
1922 ((var Dflat var-Dflat) . 1)
1923 ; ((var D♭ var-D♭) . 1)
1925 ((var Dsharp var-Dsharp) . 3)
1926 ; ((var D♯ var-D♯) . 3)
1929 ((var Fsharp var-Fsharp) . 6)
1930 ; ((var F♯ var-F♯) . 6)
1931 ((var Gflat var-Gflat) . 6)
1932 ; ((var G♭ var-G♭) . 6)
1934 ((var Gsharp var-Gsharp) . 8)
1935 ; ((var G♯ var-G♯) . 8)
1937 ((var Asharp var-Asharp) . 10)
1938 ; ((var A♯ var-A♯) . 10)
1939 ((var Bflat var-Bflat) . 10)
1940 ; ((var B♭ var-B♭) . 10)
1941 ((var B var-B) . 11))
1942 "An alist of notes with their number of semitones above C.")
1944 (defun math-freqp (freq)
1945 "Non-nil if FREQ is a positive number times the unit Hz.
1946 If non-nil, return the coefficient of Hz."
1947 (let ((freqcoef (math-simplify-units
1948 (math-div freq '(var Hz var-Hz)))))
1949 (if (Math-posp freqcoef) freqcoef)))
1951 (defun math-midip (num)
1952 "Non-nil if NUM is a possible MIDI note number.
1953 If non-nil, return NUM."
1954 (if (Math-numberp num) num))
1956 (defun math-spnp (spn)
1957 "Non-nil if NUM is a scientific pitch note (note + cents).
1958 If non-nil, return a list consisting of the note and the cents coefficient."
1959 (let (note cents rnote rcents)
1960 (if (eq (car-safe spn) '+)
1961 (setq note (nth 1 spn)
1966 ((and ;; NOTE is a note, CENTS is nil or cents.
1967 (eq (car-safe note) 'calcFunc-subscr)
1968 (assoc (nth 1 note) math-notes)
1969 (integerp (nth 2 note))
1973 (Math-numberp (setq rcents
1975 (math-div cents '(var cents var-cents)))))))
1976 (list rnote rcents))
1977 ((and ;; CENTS is a note, NOTE is cents.
1978 (eq (car-safe cents) 'calcFunc-subscr)
1979 (assoc (nth 1 cents) math-notes)
1980 (integerp (nth 2 cents))
1984 (Math-numberp (setq rcents
1986 (math-div note '(var cents var-cents)))))))
1987 (list rnote rcents)))))
1989 (defun math-freq-to-midi (freq)
1990 "Return the midi note number corresponding to FREQ Hz."
1991 (let ((midi (math-add
1998 (math-midi-round midi)))
2000 (defun math-spn-to-midi (spn)
2001 "Return the MIDI number corresponding to SPN."
2002 (let* ((note (cdr (assoc (nth 1 (car spn)) math-notes)))
2003 (octave (math-add (nth 2 (car spn)) 1))
2006 (math-mul 12 octave)
2009 (math-add midi (math-div cents 100))
2012 (defun math-midi-to-spn (midi)
2013 "Return the scientific pitch notation corresponding to midi number MIDI."
2015 (if (math-integerp midi)
2018 (setq midin (math-floor midi)
2019 cents (math-mul 100 (math-sub midi midin))))
2020 (let* ((nr ;; This should be (math-idivmod midin 12), but with
2021 ;; better behavior for negative midin.
2022 (if (Math-negp midin)
2023 (let ((dm (math-idivmod (math-neg midin) 12)))
2025 (cons (math-neg (car dm)) 0)
2027 (math-sub (math-neg (car dm)) 1)
2028 (math-sub 12 (cdr dm)))))
2029 (math-idivmod midin 12)))
2030 (n (math-sub (car nr) 1))
2031 (note (car (rassoc (cdr nr) math-notes))))
2033 (list '+ (list 'calcFunc-subscr note n)
2034 (list '* cents '(var cents var-cents)))
2035 (list 'calcFunc-subscr note n)))))
2037 (defun math-freq-to-spn (freq)
2038 "Return the scientific pitch notation corresponding to FREQ Hz."
2039 (math-with-extra-prec 3
2040 (math-midi-to-spn (math-freq-to-midi freq))))
2042 (defun math-midi-to-freq (midi)
2043 "Return the frequency of the note with midi number MIDI."
2056 (defun math-spn-to-freq (spn)
2057 "Return the frequency of the note with scientific pitch notation SPN."
2058 (math-midi-to-freq (math-spn-to-midi spn)))
2060 (defun calcFunc-spn (expr)
2061 "Return EXPR written as scientific pitch notation + cents."
2062 ;; Get the coefficient of Hz
2065 ((setq note (math-freqp expr))
2066 (math-freq-to-spn note))
2067 ((setq note (math-midip expr))
2068 (math-midi-to-spn note))
2072 (math-reject-arg expr "*Improper expression")))))
2074 (defun calcFunc-midi (expr)
2075 "Return EXPR written as a MIDI number."
2078 ((setq note (math-freqp expr))
2079 (math-freq-to-midi note))
2080 ((setq note (math-spnp expr))
2081 (math-spn-to-midi note))
2085 (math-reject-arg expr "*Improper expression")))))
2087 (defun calcFunc-freq (expr)
2088 "Return the frequency corresponding to EXPR."
2091 ((setq note (math-midip expr))
2092 (math-midi-to-freq note))
2093 ((setq note (math-spnp expr))
2094 (math-spn-to-freq note))
2098 (math-reject-arg expr "*Improper expression")))))
2100 (defun calc-freq (arg)
2101 "Return the frequency corresponding to the expression on the stack."
2104 (calc-unary-op "freq" 'calcFunc-freq arg)))
2106 (defun calc-midi (arg)
2107 "Return the MIDI number corresponding to the expression on the stack."
2110 (calc-unary-op "midi" 'calcFunc-midi arg)))
2112 (defun calc-spn (arg)
2113 "Return the scientific pitch notation corresponding to the expression on the stack."
2116 (calc-unary-op "spn" 'calcFunc-spn arg)))
2119 (provide 'calc-units)
2125 ;;; calc-units.el ends here