1 ;;; calc-units.el --- unit conversion functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
35 ;;; Units table last updated 9-Jan-91 by Ulrich Mueller (ulm@vsnhd1.cern.ch)
36 ;;; with some additions by Przemek Klosowski (przemek@rrdstrad.nist.gov)
37 ;;; Updated April 2002 by Jochen Küpper
39 ;;; Updated August 2007, using
40 ;;; CODATA (http://physics.nist.gov/cuu/Constants/index.html)
41 ;;; NIST (http://physics.nist.gov/Pubs/SP811/appenB9.html)
42 ;;; ESUWM (Encyclopaedia of Scientific Units, Weights and
43 ;;; Measures, by François Cardarelli)
44 ;;; All conversions are exact unless otherwise noted.
46 (defvar math-standard-units
49 ( in "254*10^(-2) cm" "Inch" nil
53 ( mi "5280 ft" "Mile" )
54 ( au "149597870691. m" "Astronomical Unit" nil
56 ;; (approx) NASA JPL (http://neo.jpl.nasa.gov/glossary/au.html)
57 ( lyr "c yr" "Light Year" )
58 ( pc "3.0856775854*10^16 m" "Parsec (**)" nil
59 "3.0856775854 10^16 m (*)") ;; (approx) ESUWM
60 ( nmi "1852 m" "Nautical Mile" )
61 ( fath "6 ft" "Fathom" )
62 ( fur "660 ft" "Furlong")
63 ( mu "1 um" "Micron" )
64 ( mil "(1/1000) in" "Mil" )
65 ( point "(1/72) in" "Point (PostScript convention)" )
66 ( Ang "10^(-10) m" "Angstrom" )
67 ( mfi "mi+ft+in" "Miles + feet + inches" )
69 ( texpt "(100/7227) in" "Point (TeX convention) (**)" )
70 ( texpc "12 texpt" "Pica (TeX convention) (**)" )
71 ( texbp "point" "Big point (TeX convention) (**)" )
72 ( texdd "(1238/1157) texpt" "Didot point (TeX convention) (**)" )
73 ( texcc "12 texdd" "Cicero (TeX convention) (**)" )
74 ( texsp "(1/65536) texpt" "Scaled TeX point (TeX convention) (**)" )
77 ( hect "10000 m^2" "*Hectare" )
79 ( acre "(1/640) mi^2" "Acre" )
80 ( b "10^(-28) m^2" "Barn" )
83 ( L "10^(-3) m^3" "*Liter" )
85 ( gal "4 qt" "US Gallon" )
87 ( pt "2 cup" "Pint (**)" )
88 ( cup "8 ozfl" "Cup" )
89 ( ozfl "2 tbsp" "Fluid Ounce" )
90 ( floz "2 tbsp" "Fluid Ounce" )
91 ( tbsp "3 tsp" "Tablespoon" )
92 ;; ESUWM defines a US gallon as 231 in^3.
93 ;; That gives the following exact value for tsp.
94 ( tsp "492892159375*10^(-11) ml" "Teaspoon" nil
96 ( vol "tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" nil
97 "tsp+tbsp+ozfl+cup+pt+qt+gal")
98 ( galC "galUK" "Canadian Gallon" )
99 ( galUK "454609*10^(-5) L" "UK Gallon" nil
105 ( min "60 s" "Minute" )
106 ( hr "60 min" "Hour" )
107 ( day "24 hr" "Day" )
108 ( wk "7 day" "Week" )
109 ( hms "wk+day+hr+min+s" "Hours, minutes, seconds" )
110 ( yr "36525*10^(-2) day" "Year (Julian)" nil
115 ( mph "mi/hr" "*Miles per hour" )
116 ( kph "km/hr" "Kilometers per hour" )
117 ( knot "nmi/hr" "Knot" )
118 ( c "299792458 m/s" "Speed of light" ) ;;; CODATA
121 ( ga "980665*10^(-5) m/s^2" "*\"g\" acceleration" nil
122 "9.80665 m / s^2") ;; CODATA
126 ( lb "16 oz" "Pound (mass)" )
127 ( oz "28349523125*10^(-9) g" "Ounce (mass)" nil
128 "28.349523125 g") ;; ESUWM
129 ( ton "2000 lb" "Ton" )
130 ( tpo "ton+lb+oz" "Tons + pounds + ounces (mass)" )
131 ( t "1000 kg" "Metric ton" )
132 ( tonUK "10160469088*10^(-7) kg" "UK ton" nil
133 "1016.0469088 kg") ;; ESUWM
134 ( lbt "12 ozt" "Troy pound" )
135 ( ozt "311034768*10^(-7) g" "Troy ounce" nil
136 "31.10347680 g") ;; ESUWM, 1/12 exact value for lbt
137 ( ct "(2/10) g" "Carat" nil
139 ( u "1.660538782*10^(-27) kg" "Unified atomic mass" nil
140 "1.660538782 10^-27 kg (*)");;(approx) CODATA
143 ( N "m kg/s^2" "*Newton" )
144 ( dyn "10^(-5) N" "Dyne" )
145 ( gf "ga g" "Gram (force)" )
146 ( lbf "ga lb" "Pound (force)" )
147 ( kip "1000 lbf" "Kilopound (force)" )
148 ( pdl "138254954376*10^(-12) N" "Poundal" nil
149 "0.138254954376 N") ;; ESUWM
153 ( erg "10^(-7) J" "Erg" )
154 ( cal "41868*10^(-4) J" "International Table Calorie" nil
156 ( calth "4184*10^(-3) J" "Thermochemical Calorie" nil
158 ( Cal "1000 cal" "Large Calorie")
159 ( Btu "105505585262*10^(-8) J" "International Table Btu" nil
160 "1055.05585262 J") ;; ESUWM
161 ( eV "ech V" "Electron volt" )
162 ( ev "eV" "Electron volt" )
163 ( therm "105506000 J" "EEC therm" )
164 ( invcm "h c/cm" "Energy in inverse centimeters" )
165 ( Kayser "invcm" "Kayser (inverse centimeter energy)" )
166 ( men "100/invcm" "Inverse energy in meters" )
167 ( Hzen "h Hz" "Energy in Hertz")
168 ( Ken "k K" "Energy in Kelvins")
169 ( Wh "W hr" "Watt hour")
170 ( Ws "W s" "Watt second")
174 ( hp "550 ft lbf/s" "Horsepower") ;;ESUWM
175 ( hpm "75 m kgf/s" "Metric Horsepower") ;;ESUWM
178 ( K nil "*Degree Kelvin" K )
179 ( dK "K" "Degree Kelvin" K )
180 ( degK "K" "Degree Kelvin" K )
181 ( dC "K" "Degree Celsius" C )
182 ( degC "K" "Degree Celsius" C )
183 ( dF "(5/9) K" "Degree Fahrenheit" F )
184 ( degF "(5/9) K" "Degree Fahrenheit" F )
187 ( Pa "N/m^2" "*Pascal" )
188 ( bar "10^5 Pa" "Bar" )
189 ( atm "101325 Pa" "Standard atmosphere" ) ;; CODATA
190 ( Torr "(1/760) atm" "Torr")
191 ( mHg "1000 Torr" "Meter of mercury" )
192 ( inHg "254*10^(-1) mmHg" "Inch of mercury" nil
194 ( inH2O "2.490889*10^2 Pa" "Inch of water" nil
195 "2.490889 10^2 Pa (*)") ;;(approx) NIST
196 ( psi "lbf/in^2" "Pounds per square inch" )
199 ( P "(1/10) Pa s" "*Poise" )
200 ( St "10^(-4) m^2/s" "Stokes" )
204 ( C "A s" "Coulomb" )
205 ( Fdy "ech Nav" "Faraday" )
206 ( e "ech" "Elementary charge" )
207 ( ech "1.602176487*10^(-19) C" "Elementary charge" nil
208 "1.602176487 10^-19 C (*)") ;;(approx) CODATA
213 ( S "A/V" "Siemens" )
216 ( T "Wb/m^2" "Tesla" )
217 ( Gs "10^(-4) T" "Gauss" )
220 ;; Luminous intensity
221 ( cd nil "*Candela" )
222 ( sb "10000 cd/m^2" "Stilb" )
223 ( lm "cd sr" "Lumen" )
224 ( lx "lm/m^2" "Lux" )
225 ( ph "10000 lx" "Phot" )
226 ( fc "lm/ft^2" "Footcandle") ;; ESUWM
227 ( lam "10000 lm/m^2" "Lambert" )
228 ( flam "(1/pi) cd/ft^2" "Footlambert") ;; ESUWM
231 ( Bq "1/s" "*Becquerel" )
232 ( Ci "37*10^9 Bq" "Curie" ) ;; ESUWM
234 ( Sv "Gy" "Sievert" )
235 ( R "258*10^(-6) C/kg" "Roentgen" ) ;; NIST
236 ( rd "(1/100) Gy" "Rad" )
239 ;; Amount of substance
243 ( rad nil "*Radian" )
244 ( circ "2 pi rad" "Full circle" )
245 ( rev "circ" "Full revolution" )
246 ( deg "circ/360" "Degree" )
247 ( arcmin "deg/60" "Arc minute" )
248 ( arcsec "arcmin/60" "Arc second" )
249 ( grad "circ/400" "Grade" )
250 ( rpm "rev/min" "Revolutions per minute" )
253 ( sr nil "*Steradian" )
255 ;; Other physical quantities
256 ;; The values are from CODATA, and are approximate.
257 ( h "6.62606896*10^(-34) J s" "*Planck's constant" nil
258 "6.62606896 10^-34 J s (*)")
259 ( hbar "h / (2 pi)" "Planck's constant" ) ;; Exact
260 ( mu0 "4 pi 10^(-7) H/m" "Permeability of vacuum") ;; Exact
261 ; ( μ0 "mu0" "Permeability of vacuum") ;; Exact
262 ( eps0 "1 / (mu0 c^2)" "Permittivity of vacuum" )
263 ; ( ε0 "eps0" "Permittivity of vacuum" )
264 ( G "6.67428*10^(-11) m^3/(kg s^2)" "Gravitational constant" nil
265 "6.67428 10^-11 m^3/(kg s^2) (*)")
266 ( Nav "6.02214179*10^(23) / mol" "Avogadro's constant" nil
267 "6.02214179 10^23 / mol (*)")
268 ( me "9.10938215*10^(-31) kg" "Electron rest mass" nil
269 "9.10938215 10^-31 kg (*)")
270 ( mp "1.672621637*10^(-27) kg" "Proton rest mass" nil
271 "1.672621637 10^-27 kg (*)")
272 ( mn "1.674927211*10^(-27) kg" "Neutron rest mass" nil
273 "1.674927211 10^-27 kg (*)")
274 ( mmu "1.88353130*10^(-28) kg" "Muon rest mass" nil
275 "1.88353130 10^-28 kg (*)")
276 ; ( mμ "mmu" "Muon rest mass" nil
277 ; "1.88353130 10^-28 kg (*)")
278 ( Ryd "10973731.568527 /m" "Rydberg's constant" nil
279 "10973731.568527 /m (*)")
280 ( k "1.3806504*10^(-23) J/K" "Boltzmann's constant" nil
281 "1.3806504 10^-23 J/K (*)")
282 ( alpha "7.2973525376*10^(-3)" "Fine structure constant" nil
283 "7.2973525376 10^-3 (*)")
284 ; ( α "alpha" "Fine structure constant" nil
285 ; "7.2973525376 10^-3 (*)")
286 ( muB "927.400915*10^(-26) J/T" "Bohr magneton" nil
287 "927.400915 10^-26 J/T (*)")
288 ( muN "5.05078324*10^(-27) J/T" "Nuclear magneton" nil
289 "5.05078324 10^-27 J/T (*)")
290 ( mue "-928.476377*10^(-26) J/T" "Electron magnetic moment" nil
291 "-928.476377 10^-26 J/T (*)")
292 ( mup "1.410606662*10^(-26) J/T" "Proton magnetic moment" nil
293 "1.410606662 10^-26 J/T (*)")
294 ( R0 "8.314472 J/(mol K)" "Molar gas constant" nil
295 "8.314472 J/(mol K) (*)")
296 ( V0 "22.710981*10^(-3) m^3/mol" "Standard volume of ideal gas" nil
297 "22.710981 10^-3 m^3/mol (*)")
300 ( dB "(ln(10)/20) Np" "decibel")))
303 (defvar math-additional-units nil
304 "Additional units table for user-defined units.
305 Must be formatted like `math-standard-units'.
306 If you change this, be sure to set `math-units-table' to nil to ensure
307 that the combined units table will be rebuilt.")
309 (defvar math-unit-prefixes
310 '( ( ?Y (^ 10 24) "Yotta" )
311 ( ?Z (^ 10 21) "Zetta" )
312 ( ?E (^ 10 18) "Exa" )
313 ( ?P (^ 10 15) "Peta" )
314 ( ?T (^ 10 12) "Tera" )
315 ( ?G (^ 10 9) "Giga" )
316 ( ?M (^ 10 6) "Mega" )
317 ( ?k (^ 10 3) "Kilo" )
318 ( ?K (^ 10 3) "Kilo" )
319 ( ?h (^ 10 2) "Hecto" )
320 ( ?H (^ 10 2) "Hecto" )
321 ( ?D (^ 10 1) "Deka" )
323 ( ?d (^ 10 -1) "Deci" )
324 ( ?c (^ 10 -2) "Centi" )
325 ( ?m (^ 10 -3) "Milli" )
326 ( ?u (^ 10 -6) "Micro" )
327 ; ( ?μ (^ 10 -6) "Micro" )
328 ( ?n (^ 10 -9) "Nano" )
329 ( ?p (^ 10 -12) "Pico" )
330 ( ?f (^ 10 -15) "Femto" )
331 ( ?a (^ 10 -18) "Atto" )
332 ( ?z (^ 10 -21) "zepto" )
333 ( ?y (^ 10 -24) "yocto" )))
335 (defvar math-standard-units-systems
337 ( si ( ( g '(/ (var kg var-kg) 1000) ) ) )
338 ( mks ( ( g '(/ (var kg var-kg) 1000) ) ) )
339 ( cgs ( ( m '(* (var cm var-cm) 100 ) ) ) )))
341 (defvar math-units-table nil
342 "Internal units table.
343 Derived from `math-standard-units' and `math-additional-units'.
344 Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
346 (defvar math-units-table-buffer-valid nil)
350 (defun calc-base-units ()
353 (let ((calc-autorange-units nil))
354 (calc-enter-result 1 "bsun" (math-simplify-units
355 (math-to-standard-units (calc-top-n 1)
358 (defvar calc-ensure-consistent-units)
360 (defun calc-quick-units ()
363 (let* ((num (- last-command-char ?0))
364 (pos (if (= num 0) 10 num))
365 (units (calc-var-value 'var-Units))
366 (expr (calc-top-n 1)))
367 (unless (and (>= num 0) (<= num 9))
368 (error "Bad unit number"))
369 (unless (math-vectorp units)
370 (error "No \"quick units\" are defined"))
371 (unless (< pos (length units))
372 (error "Unit number %d not defined" pos))
373 (if (math-units-in-expr-p expr nil)
375 (if calc-ensure-consistent-units
376 (math-check-unit-consistency expr (nth pos units)))
377 (calc-enter-result 1 (format "cun%d" num)
378 (math-convert-units expr (nth pos units))))
379 (calc-enter-result 1 (format "*un%d" num)
381 (math-mul expr (nth pos units))))))))
383 (defun math-get-standard-units (expr)
384 "Return the standard units in EXPR."
387 (math-to-standard-units expr nil))))
389 (defun math-get-units (expr)
390 "Return the units in EXPR."
392 (math-extract-units expr)))
394 (defun math-make-unit-string (expr)
395 "Return EXPR in string form.
396 If EXPR is nil, return nil."
398 (let ((cexpr (math-compose-expr expr 0)))
399 (replace-regexp-in-string
403 (math-composition-to-string cexpr))))))
405 (defvar math-default-units-table
406 (make-hash-table :test 'equal)
407 "A table storing previously converted units.")
409 (defun math-get-default-units (expr)
410 "Get default units to use when converting the units in EXPR."
411 (let* ((units (math-get-units expr))
412 (standard-units (math-get-standard-units expr))
413 (default-units (gethash
415 math-default-units-table)))
416 (if (equal units (car default-units))
417 (math-make-unit-string (cadr default-units))
418 (math-make-unit-string (car default-units)))))
420 (defun math-put-default-units (expr &optional comp std)
421 "Put the units in EXPR in the default units table.
422 If COMP or STD is non-nil, put that in the units table instead."
423 (let* ((new-units (or comp std (math-get-units expr)))
424 (standard-units (math-get-standard-units
426 (comp (math-simplify-units expr))
429 (default-units (gethash standard-units math-default-units-table)))
430 (unless (eq standard-units 1)
433 (puthash standard-units (list new-units) math-default-units-table))
434 ((not (equal new-units (car default-units)))
435 (puthash standard-units
436 (list new-units (car default-units))
437 math-default-units-table))))))
439 (defvar calc-allow-units-as-numbers t)
441 (defun calc-convert-units (&optional old-units new-units)
444 (let ((expr (calc-top-n 1))
448 ;; unew -- unused? --SY.
451 (if (or (not (math-units-in-expr-p expr t))
452 (setq unitscancel (and
453 (if (get 'calc-allow-units-as-numbers 'saved-value)
454 (car (get 'calc-allow-units-as-numbers 'saved-value))
455 calc-allow-units-as-numbers)
456 (eq (math-get-standard-units expr) 1))))
464 "(The expression is unitless when simplified) Old Units: ")
465 (read-string "Old units: ")))
466 (if (equal uoldname "")
468 (setq nouold unitscancel)
471 (if (string-match "\\` */" uoldname)
472 (setq uoldname (concat "1" uoldname)))
473 (math-read-expr uoldname))))))
474 (unless (math-units-in-expr-p uold t)
475 (error "No units specified"))
476 (when (eq (car-safe uold) 'error)
477 (error "Bad format in units expression: %s" (nth 1 uold)))
478 (setq expr (math-mul expr uold))))
479 (setq defunits (math-get-default-units expr))
483 (if (and uoldname (not nouold))
484 (concat "Old units: "
495 (string= new-units "")
497 (setq new-units defunits)))
498 (when (string-match "\\` */" new-units)
499 (setq new-units (concat "1" new-units)))
500 (setq units (math-read-expr new-units))
501 (when (eq (car-safe units) 'error)
502 (error "Bad format in units expression: %s" (nth 2 units)))
503 (if calc-ensure-consistent-units
504 (math-check-unit-consistency expr units))
505 (let ((unew (math-units-in-expr-p units t))
506 (std (and (eq (car-safe units) 'var)
507 (assq (nth 1 units) math-standard-units-systems)))
508 (comp (eq (car-safe units) '+)))
509 (unless (or unew std)
510 (error "No units specified"))
511 (let* ((noold (and uoldname (not (equal uoldname "1"))))
514 (math-simplify-units (math-to-standard-units expr (nth 1 std)))
515 (math-convert-units expr units noold))))
517 (math-put-default-units (if noold units res) (if comp units)))
518 (calc-enter-result 1 "cvun" res))))))
520 (defun calc-convert-exact-units ()
523 (let* ((expr (calc-top-n 1)))
524 (unless (math-units-in-expr-p expr t)
525 (error "No units in expression."))
526 (let* ((old-units (math-extract-units expr))
527 (defunits (math-get-default-units expr))
530 (read-string (concat "New units"
538 (string= new-units "")
540 (setq new-units defunits))
541 (setq units (math-read-expr new-units))
542 (when (eq (car-safe units) 'error)
543 (error "Bad format in units expression: %s" (nth 2 units)))
544 (math-check-unit-consistency old-units units)
547 (math-mul (math-remove-units expr)
549 (math-to-standard-units (list '/ old-units units) nil)))
551 (calc-enter-result 1 "cvxu" res))))))
553 (defun calc-autorange-units (arg)
556 (calc-change-mode 'calc-autorange-units arg nil t)
557 (message (if calc-autorange-units
558 "Adjusting target unit prefix automatically"
559 "Using target units exactly"))))
561 (defun calc-convert-temperature (&optional old-units new-units)
564 (let ((expr (calc-top-n 1))
569 (setq uold (or old-units
570 (let ((units (math-single-units-in-expr-p expr)))
573 (list 'var (car units)
574 (intern (concat "var-"
577 (error "Not a pure temperature expression"))
579 (setq uoldname (read-string
580 "Old temperature units: ")))))))
581 (when (eq (car-safe uold) 'error)
582 (error "Bad format in units expression: %s" (nth 2 uold)))
583 (or (math-units-in-expr-p expr nil)
584 (setq expr (math-mul expr uold)))
585 (setq defunits (math-get-default-units expr))
586 (setq unew (or new-units
590 (concat "Old temperature units: "
593 "New temperature units")
599 (setq unew (math-read-expr (if (string= unew "") defunits unew)))
600 (when (eq (car-safe unew) 'error)
601 (error "Bad format in units expression: %s" (nth 2 unew)))
602 (math-put-default-units unew)
603 (let ((ntemp (calc-normalize
605 (math-convert-temperature expr uold unew
607 (if (Math-zerop ntemp)
608 (setq ntemp (list '* ntemp unew)))
609 (let ((calc-simplify-mode 'none))
610 (calc-enter-result 1 "cvtm" ntemp))))))
612 (defun calc-remove-units ()
615 (calc-enter-result 1 "rmun" (math-simplify-units
616 (math-remove-units (calc-top-n 1))))))
618 (defun calc-extract-units ()
621 (calc-enter-result 1 "exun" (math-simplify-units
622 (math-extract-units (calc-top-n 1))))))
624 ;; The variables calc-num-units and calc-den-units are local to
625 ;; calc-explain-units, but are used by calc-explain-units-rec,
626 ;; which is called by calc-explain-units.
627 (defvar calc-num-units)
628 (defvar calc-den-units)
630 (defun calc-explain-units ()
633 (let ((calc-num-units nil)
634 (calc-den-units nil))
635 (calc-explain-units-rec (calc-top-n 1) 1)
636 (and calc-den-units (string-match "^[^(].* .*[^)]$" calc-den-units)
637 (setq calc-den-units (concat "(" calc-den-units ")")))
640 (message "%s per %s" calc-num-units calc-den-units)
641 (message "%s" calc-num-units))
643 (message "1 per %s" calc-den-units)
644 (message "No units in expression"))))))
646 (defun calc-explain-units-rec (expr pow)
647 (let ((u (math-check-unit-name expr))
649 (if (and u (not (math-zerop pow)))
650 (let ((name (or (nth 2 u) (symbol-name (car u)))))
651 (if (eq (aref name 0) ?\*)
652 (setq name (substring name 1)))
653 (if (string-match "[^a-zA-Z0-9']" name)
654 (if (string-match "^[a-zA-Z0-9' ()]*$" name)
655 ; (if (string-match "[^a-zA-Zα-ωΑ-Ω0-9']" name)
656 ; (if (string-match "^[a-zA-Zα-ωΑ-Ω0-9' ()]*$" name)
657 (while (setq pos (string-match "[ ()]" name))
658 (setq name (concat (substring name 0 pos)
659 (if (eq (aref name pos) 32) "-" "")
660 (substring name (1+ pos)))))
661 (setq name (concat "(" name ")"))))
662 (or (eq (nth 1 expr) (car u))
663 (setq name (concat (nth 2 (assq (aref (symbol-name
666 (if (and (string-match "[^a-zA-Z0-9']" name)
667 ; (if (and (string-match "[^a-zA-Zα-ωΑ-Ω0-9']" name)
668 (not (memq (car u) '(mHg gf))))
671 (cond ((or (math-equal-int pow 1)
672 (math-equal-int pow -1)))
673 ((or (math-equal-int pow 2)
674 (math-equal-int pow -2))
675 (if (equal (nth 4 u) '((m . 1)))
676 (setq name (concat "Square-" name))
677 (setq name (concat name "-squared"))))
678 ((or (math-equal-int pow 3)
679 (math-equal-int pow -3))
680 (if (equal (nth 4 u) '((m . 1)))
681 (setq name (concat "Cubic-" name))
682 (setq name (concat name "-cubed"))))
684 (setq name (concat name "^"
685 (math-format-number (math-abs pow))))))
687 (setq calc-num-units (if calc-num-units
688 (concat calc-num-units " " name)
690 (setq calc-den-units (if calc-den-units
691 (concat calc-den-units " " name)
693 (cond ((eq (car-safe expr) '*)
694 (calc-explain-units-rec (nth 1 expr) pow)
695 (calc-explain-units-rec (nth 2 expr) pow))
696 ((eq (car-safe expr) '/)
697 (calc-explain-units-rec (nth 1 expr) pow)
698 (calc-explain-units-rec (nth 2 expr) (- pow)))
699 ((memq (car-safe expr) '(neg + -))
700 (calc-explain-units-rec (nth 1 expr) pow))
701 ((and (eq (car-safe expr) '^)
702 (math-realp (nth 2 expr)))
703 (calc-explain-units-rec (nth 1 expr)
704 (math-mul pow (nth 2 expr))))))))
706 (defun calc-simplify-units ()
709 (calc-with-default-simplification
710 (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1))))))
712 (defun calc-view-units-table (n)
714 (and n (setq math-units-table-buffer-valid nil))
715 (let ((win (get-buffer-window "*Units Table*")))
718 math-units-table-buffer-valid)
720 (bury-buffer (window-buffer win))
721 (let ((curwin (selected-window)))
723 (switch-to-buffer nil)
724 (select-window curwin)))
725 (math-build-units-table-buffer nil))))
727 (defun calc-enter-units-table (n)
729 (and n (setq math-units-table-buffer-valid nil))
730 (math-build-units-table-buffer t)
731 (message "%s" (substitute-command-keys "Type \\[calc] to return to the Calculator")))
733 (defun calc-define-unit (uname desc &optional disp)
734 (interactive "SDefine unit name: \nsDescription: \nP")
735 (if disp (setq disp (read-string "Display definition: ")))
737 (let ((form (calc-top-n 1))
738 (unit (assq uname math-additional-units)))
740 (setq math-additional-units
741 (cons (setq unit (list uname nil nil nil nil))
742 math-additional-units)
743 math-units-table nil))
744 (setcar (cdr unit) (and (not (and (eq (car-safe form) 'var)
745 (eq (nth 1 form) uname)))
746 (not (math-equal-int form 1))
747 (math-format-flat-expr form 0)))
748 (setcar (cdr (cdr unit)) (and (not (equal desc ""))
751 (setcar (cdr (cdr (cdr (cdr unit)))) disp))))
752 (calc-invalidate-units-table))
754 (defun calc-undefine-unit (uname)
755 (interactive "SUndefine unit name: ")
757 (let ((unit (assq uname math-additional-units)))
759 (if (assq uname math-standard-units)
760 (error "\"%s\" is a predefined unit name" uname)
761 (error "Unit name \"%s\" not found" uname)))
762 (setq math-additional-units (delq unit math-additional-units)
763 math-units-table nil)))
764 (calc-invalidate-units-table))
766 (defun calc-invalidate-units-table ()
767 (setq math-units-table nil)
768 (let ((buf (get-buffer "*Units Table*")))
770 (with-current-buffer buf
772 (goto-char (point-min))
773 (if (looking-at "Calculator Units Table")
774 (let ((inhibit-read-only t))
775 (insert "(Obsolete) "))))))))
777 (defun calc-get-unit-definition (uname)
778 (interactive "SGet definition for unit: ")
780 (math-build-units-table)
781 (let ((unit (assq uname math-units-table)))
783 (error "Unit name \"%s\" not found" uname))
784 (let ((msg (nth 2 unit)))
786 (if (string-match "^\\*" msg)
787 (setq msg (substring msg 1)))
788 (setq msg (symbol-name uname)))
791 (calc-enter-result 0 "ugdf" (nth 1 unit))
792 (message "Derived unit: %s" msg))
793 (calc-enter-result 0 "ugdf" (list 'var uname
796 (symbol-name uname)))))
797 (message "Base unit: %s" msg))))))
799 (defun calc-permanent-units ()
803 (set-buffer (find-file-noselect (substitute-in-file-name
804 calc-settings-file)))
805 (goto-char (point-min))
806 (if (and (search-forward ";;; Custom units stored by Calc" nil t)
810 (search-forward "\n;;; End of custom units" nil t)))
814 (delete-region pos (point)))
815 (goto-char (point-max))
818 (insert ";;; Custom units stored by Calc on " (current-time-string) "\n")
819 (if math-additional-units
821 (insert "(setq math-additional-units '(\n")
822 (let ((list math-additional-units))
824 (insert " (" (symbol-name (car (car list))) " "
825 (if (nth 1 (car list))
826 (if (stringp (nth 1 (car list)))
827 (prin1-to-string (nth 1 (car list)))
828 (prin1-to-string (math-format-flat-expr
829 (nth 1 (car list)) 0)))
832 (prin1-to-string (nth 2 (car list)))
834 (setq list (cdr list))))
836 (insert ";;; (no custom units defined)\n"))
837 (insert ";;; End of custom units\n")
841 ;; The variable math-cu-unit-list is local to math-build-units-table,
842 ;; but is used by math-compare-unit-names, which is called (indirectly)
843 ;; by math-build-units-table.
844 ;; math-cu-unit-list is also local to math-convert-units, but is used
845 ;; by math-convert-units-rec, which is called by math-convert-units.
846 (defvar math-cu-unit-list)
848 (defun math-build-units-table ()
850 (let* ((combined-units (append math-additional-units
851 math-standard-units))
852 (math-cu-unit-list (mapcar 'car combined-units))
854 (message "Building units table...")
855 (setq math-units-table-buffer-valid nil)
856 (setq tab (mapcar (function
860 (if (stringp (nth 1 x))
861 (let ((exp (math-read-plain-expr
863 (if (eq (car-safe exp) 'error)
864 (error "Format error in definition of %s in units table: %s"
871 (list (cons (car x) 1)))
874 (let ((math-units-table tab))
875 (mapc 'math-find-base-units tab))
876 (message "Building units table...done")
877 (setq math-units-table tab))))
879 ;; The variables math-fbu-base and math-fbu-entry are local to
880 ;; math-find-base-units, but are used by math-find-base-units-rec,
881 ;; which is called by math-find-base-units.
882 (defvar math-fbu-base)
883 (defvar math-fbu-entry)
885 (defun math-find-base-units (math-fbu-entry)
886 (if (eq (nth 4 math-fbu-entry) 'boom)
887 (error "Circular definition involving unit %s" (car math-fbu-entry)))
888 (or (nth 4 math-fbu-entry)
890 (setcar (nthcdr 4 math-fbu-entry) 'boom)
891 (math-find-base-units-rec (nth 1 math-fbu-entry) 1)
893 (error "Dimensionless definition for unit %s" (car math-fbu-entry)))
894 (while (eq (cdr (car math-fbu-base)) 0)
895 (setq math-fbu-base (cdr math-fbu-base)))
896 (let ((b math-fbu-base))
898 (if (eq (cdr (car (cdr b))) 0)
899 (setcdr b (cdr (cdr b)))
901 (setq math-fbu-base (sort math-fbu-base 'math-compare-unit-names))
902 (setcar (nthcdr 4 math-fbu-entry) math-fbu-base)
905 (defun math-compare-unit-names (a b)
906 (memq (car b) (cdr (memq (car a) math-cu-unit-list))))
908 (defun math-find-base-units-rec (expr pow)
909 (let ((u (math-check-unit-name expr)))
911 (let ((ulist (math-find-base-units u)))
913 (let ((p (* (cdr (car ulist)) pow))
914 (old (assq (car (car ulist)) math-fbu-base)))
916 (setcdr old (+ (cdr old) p))
918 (cons (cons (car (car ulist)) p) math-fbu-base))))
919 (setq ulist (cdr ulist)))))
920 ((math-scalarp expr))
921 ((and (eq (car expr) '^)
922 (integerp (nth 2 expr)))
923 (math-find-base-units-rec (nth 1 expr) (* pow (nth 2 expr))))
925 (math-find-base-units-rec (nth 1 expr) pow)
926 (math-find-base-units-rec (nth 2 expr) pow))
928 (math-find-base-units-rec (nth 1 expr) pow)
929 (math-find-base-units-rec (nth 2 expr) (- pow)))
930 ((eq (car expr) 'neg)
931 (math-find-base-units-rec (nth 1 expr) pow))
933 (math-find-base-units-rec (nth 1 expr) pow))
934 ((eq (car expr) 'var)
935 (or (eq (nth 1 expr) 'pi)
936 (error "Unknown name %s in defining expression for unit %s"
937 (nth 1 expr) (car math-fbu-entry))))
938 ((equal expr '(calcFunc-ln 10)))
939 (t (error "Malformed defining expression for unit %s"
940 (car math-fbu-entry))))))
943 (defun math-units-in-expr-p (expr sub-exprs)
945 (if (eq (car expr) 'var)
946 (math-check-unit-name expr)
947 (if (eq (car expr) 'neg)
948 (math-units-in-expr-p (nth 1 expr) sub-exprs)
950 (memq (car expr) '(* / ^)))
951 (or (math-units-in-expr-p (nth 1 expr) sub-exprs)
952 (math-units-in-expr-p (nth 2 expr) sub-exprs)))))))
954 (defun math-only-units-in-expr-p (expr)
956 (if (eq (car expr) 'var)
957 (math-check-unit-name expr)
958 (if (memq (car expr) '(* /))
959 (and (math-only-units-in-expr-p (nth 1 expr))
960 (math-only-units-in-expr-p (nth 2 expr)))
961 (and (eq (car expr) '^)
962 (and (math-only-units-in-expr-p (nth 1 expr))
963 (math-realp (nth 2 expr))))))))
965 (defun math-single-units-in-expr-p (expr)
966 (cond ((math-scalarp expr) nil)
967 ((eq (car expr) 'var)
968 (math-check-unit-name expr))
969 ((eq (car expr) 'neg)
970 (math-single-units-in-expr-p (nth 1 expr)))
972 (let ((u1 (math-single-units-in-expr-p (nth 1 expr)))
973 (u2 (math-single-units-in-expr-p (nth 2 expr))))
974 (or (and u1 u2 'wrong)
978 (if (math-units-in-expr-p (nth 2 expr) nil)
980 (math-single-units-in-expr-p (nth 1 expr))))
983 (defun math-consistent-units-p (expr newunits)
984 "Non-nil if EXPR and NEWUNITS have consistent units."
986 (and (eq (car-safe newunits) 'var)
987 (assq (nth 1 newunits) math-standard-units-systems))
989 (math-get-units (math-to-standard-units (list '/ expr newunits) nil)))))
991 (defun math-check-unit-consistency (expr units)
992 "Give an error if EXPR and UNITS do not have consistent units."
993 (unless (math-consistent-units-p expr units)
994 (error "New units (%s) are inconsistent with current units (%s)"
995 (math-format-value units)
996 (math-format-value (math-get-units expr)))))
998 (defun math-check-unit-name (v)
999 (and (eq (car-safe v) 'var)
1000 (or (assq (nth 1 v) (or math-units-table (math-build-units-table)))
1001 (let ((name (symbol-name (nth 1 v))))
1002 (and (> (length name) 1)
1003 (assq (aref name 0) math-unit-prefixes)
1004 (or (assq (intern (substring name 1)) math-units-table)
1005 (and (eq (aref name 0) ?M)
1007 (eq (aref name 1) ?e)
1008 (eq (aref name 2) ?g)
1009 (assq (intern (substring name 3))
1010 math-units-table))))))))
1012 ;; The variable math-which-standard is local to math-to-standard-units,
1013 ;; but is used by math-to-standard-rec, which is called by
1014 ;; math-to-standard-units.
1015 (defvar math-which-standard)
1017 (defun math-to-standard-units (expr math-which-standard)
1018 (math-to-standard-rec expr))
1020 (defun math-to-standard-rec (expr)
1021 (if (eq (car-safe expr) 'var)
1022 (let ((u (math-check-unit-name expr))
1023 (base (nth 1 expr)))
1027 (setq expr (math-to-standard-rec (nth 1 u)))
1028 (let ((st (assq (car u) math-which-standard)))
1030 (setq expr (nth 1 st))
1031 (setq expr (list 'var (car u)
1032 (intern (concat "var-"
1038 (nth 1 (assq (aref (symbol-name base) 0)
1039 math-unit-prefixes))
1047 (and (eq (car-safe expr) 'calcFunc-subscr)
1048 (eq (car-safe (nth 1 expr)) 'var)))
1051 (mapcar 'math-to-standard-rec (cdr expr))))))
1053 (defun math-apply-units (expr units ulist &optional pure)
1054 (setq expr (math-simplify-units expr))
1058 (or (math-numberp expr)
1059 (error "Incompatible units"))
1061 (setq value (math-div expr (nth 1 (car ulist)))
1062 value (math-floor (let ((calc-internal-prec
1063 (1- calc-internal-prec)))
1064 (math-normalize value)))
1065 new (math-add new (math-mul value (car (car ulist))))
1066 expr (math-sub expr (math-mul value (nth 1 (car ulist))))
1068 (math-add new (math-mul (math-div expr (nth 1 (car ulist)))
1069 (car (car ulist)))))
1072 (math-simplify-units (list '* expr units)))))
1074 (defvar math-decompose-units-cache nil)
1075 (defun math-decompose-units (units)
1076 (let ((u (math-check-unit-name units)))
1077 (and u (eq (car-safe (nth 1 u)) '+)
1078 (setq units (nth 1 u))))
1079 (setq units (calcFunc-expand units))
1080 (and (eq (car-safe units) '+)
1081 (let ((entry (list units calc-internal-prec calc-prefer-frac)))
1082 (or (equal entry (car math-decompose-units-cache))
1085 (while (eq (car-safe utemp) '+)
1086 (setq ulist (cons (math-decompose-unit-part (nth 2 utemp))
1088 utemp (nth 1 utemp)))
1089 (setq ulist (cons (math-decompose-unit-part utemp) ulist)
1091 (while (setq utemp (cdr utemp))
1092 (unless (equal (nth 2 (car utemp)) (nth 2 (car ulist)))
1093 (error "Inconsistent units in sum")))
1094 (setq math-decompose-units-cache
1099 (not (Math-lessp (nth 1 x)
1101 (cdr math-decompose-units-cache))))
1103 (defun math-decompose-unit-part (unit)
1105 (math-is-multiple (math-simplify-units (math-to-standard-units
1109 ;; The variable math-fcu-u is local to math-find-compatible-unit,
1110 ;; but is used by math-find-compatible-rec which is called by
1111 ;; math-find-compatible-unit.
1114 (defun math-find-compatible-unit (expr unit)
1115 (let ((math-fcu-u (math-check-unit-name unit)))
1117 (math-find-compatible-unit-rec expr 1))))
1119 (defun math-find-compatible-unit-rec (expr pow)
1120 (cond ((eq (car-safe expr) '*)
1121 (or (math-find-compatible-unit-rec (nth 1 expr) pow)
1122 (math-find-compatible-unit-rec (nth 2 expr) pow)))
1123 ((eq (car-safe expr) '/)
1124 (or (math-find-compatible-unit-rec (nth 1 expr) pow)
1125 (math-find-compatible-unit-rec (nth 2 expr) (- pow))))
1126 ((eq (car-safe expr) 'neg)
1127 (math-find-compatible-unit-rec (nth 1 expr) pow))
1128 ((and (eq (car-safe expr) '^)
1129 (integerp (nth 2 expr)))
1130 (math-find-compatible-unit-rec (nth 1 expr) (* pow (nth 2 expr))))
1132 (let ((u2 (math-check-unit-name expr)))
1133 (if (equal (nth 4 math-fcu-u) (nth 4 u2))
1134 (cons expr pow))))))
1136 ;; The variables math-cu-new-units and math-cu-pure are local to
1137 ;; math-convert-units, but are used by math-convert-units-rec,
1138 ;; which is called by math-convert-units.
1139 (defvar math-cu-new-units)
1140 (defvar math-cu-pure)
1142 (defun math-convert-units (expr math-cu-new-units &optional math-cu-pure)
1143 (if (eq (car-safe math-cu-new-units) 'var)
1144 (let ((unew (assq (nth 1 math-cu-new-units)
1145 (math-build-units-table))))
1146 (if (eq (car-safe (nth 1 unew)) '+)
1147 (setq math-cu-new-units (nth 1 unew)))))
1148 (math-with-extra-prec 2
1149 (let ((compat (and (not math-cu-pure)
1150 (math-find-compatible-unit expr math-cu-new-units)))
1151 (math-cu-unit-list nil)
1152 (math-combining-units nil))
1154 (math-simplify-units
1155 (math-mul (math-mul (math-simplify-units
1156 (math-div expr (math-pow (car compat)
1158 (math-pow math-cu-new-units (cdr compat)))
1159 (math-simplify-units
1160 (math-to-standard-units
1161 (math-pow (math-div (car compat) math-cu-new-units)
1164 (when (setq math-cu-unit-list (math-decompose-units math-cu-new-units))
1165 (setq math-cu-new-units (nth 2 (car math-cu-unit-list))))
1166 (when (eq (car-safe expr) '+)
1167 (setq expr (math-simplify-units expr)))
1168 (if (math-units-in-expr-p expr t)
1169 (math-convert-units-rec expr)
1170 (math-apply-units (math-to-standard-units
1171 (list '/ expr math-cu-new-units) nil)
1172 math-cu-new-units math-cu-unit-list math-cu-pure))))))
1174 (defun math-convert-units-rec (expr)
1175 (if (math-units-in-expr-p expr nil)
1176 (math-apply-units (math-to-standard-units
1177 (list '/ expr math-cu-new-units) nil)
1178 math-cu-new-units math-cu-unit-list math-cu-pure)
1179 (if (Math-primp expr)
1182 (mapcar 'math-convert-units-rec (cdr expr))))))
1184 (defun math-convert-temperature (expr old new &optional pure)
1185 (let* ((units (math-single-units-in-expr-p expr))
1187 (if (or (null units)
1188 (equal (nth 1 old) (car units)))
1189 (math-check-unit-name old)
1190 (error "Inconsistent temperature units"))
1192 (unew (math-check-unit-name new)))
1193 (unless (and (consp unew) (nth 3 unew))
1194 (error "Not a valid temperature unit"))
1195 (unless (and (consp uold) (nth 3 uold))
1196 (error "Not a pure temperature expression"))
1197 (let ((v (car uold)))
1198 (setq expr (list '/ expr (list 'var v
1199 (intern (concat "var-"
1200 (symbol-name v)))))))
1201 (or (eq (nth 3 uold) (nth 3 unew))
1202 (cond ((eq (nth 3 uold) 'K)
1203 (setq expr (list '- expr '(/ 27315 100)))
1204 (if (eq (nth 3 unew) 'F)
1205 (setq expr (list '+ (list '* expr '(/ 9 5)) 32))))
1206 ((eq (nth 3 uold) 'C)
1207 (if (eq (nth 3 unew) 'F)
1208 (setq expr (list '+ (list '* expr '(/ 9 5)) 32))
1209 (setq expr (list '+ expr '(/ 27315 100)))))
1211 (setq expr (list '* (list '- expr 32) '(/ 5 9)))
1212 (if (eq (nth 3 unew) 'K)
1213 (setq expr (list '+ expr '(/ 27315 100)))))))
1216 (list '* expr new))))
1220 (defun math-simplify-units (a)
1221 (let ((math-simplifying-units t)
1222 (calc-matrix-mode 'scalar))
1224 (defalias 'calcFunc-usimplify 'math-simplify-units)
1226 ;; The function created by math-defsimplify uses the variable
1227 ;; math-simplify-expr, and so is used by functions in math-defsimplify
1228 (defvar math-simplify-expr)
1230 (math-defsimplify (+ -)
1231 (and math-simplifying-units
1232 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1233 (let* ((units (math-extract-units (nth 1 math-simplify-expr)))
1234 (ratio (math-simplify (math-to-standard-units
1235 (list '/ (nth 2 math-simplify-expr) units) nil))))
1236 (if (math-units-in-expr-p ratio nil)
1238 (calc-record-why "*Inconsistent units" math-simplify-expr)
1240 (list '* (math-add (math-remove-units (nth 1 math-simplify-expr))
1241 (if (eq (car math-simplify-expr) '-)
1242 (math-neg ratio) ratio))
1246 (math-simplify-units-prod))
1248 (defun math-simplify-units-prod ()
1249 (and math-simplifying-units
1250 calc-autorange-units
1251 (Math-realp (nth 1 math-simplify-expr))
1252 (let* ((num (math-float (nth 1 math-simplify-expr)))
1253 (xpon (calcFunc-xpon num))
1254 (unitp (cdr (cdr math-simplify-expr)))
1256 (pow (if (eq (car math-simplify-expr) '*) 1 -1))
1258 (and (eq (car-safe unit) '*)
1259 (setq unitp (cdr unit)
1261 (and (eq (car-safe unit) '^)
1262 (integerp (nth 2 unit))
1263 (setq pow (* pow (nth 2 unit))
1266 (and (setq u (math-check-unit-name unit))
1269 (>= xpon (if (eq (car u) 'm) 1 3)))
1272 (reverse math-unit-prefixes)
1273 math-unit-prefixes))
1276 (or (eq (car u) (nth 1 unit))
1281 math-unit-prefixes))))))
1282 (setq xpon (+ xpon uxpon))
1284 (or (memq (car (car p)) '(?d ?D ?h ?H))
1285 (and (eq (car (car p)) ?c)
1286 (not (eq (car u) 'm)))
1287 (< xpon (setq pxpon (* (nth 2 (nth 1 (car p)))
1290 (setq pname (math-build-var-name
1291 (if (eq (car (car p)) 0)
1293 (concat (char-to-string
1297 (and (/= (car (car p)) 0)
1299 math-units-table)))))
1303 (or (not (eq p pref))
1304 (< xpon (+ pxpon (* (math-abs pow) 3))))
1306 (setcar (cdr math-simplify-expr)
1307 (let ((calc-prefer-frac nil))
1308 (calcFunc-scf (nth 1 math-simplify-expr)
1310 (setcar unitp pname)
1311 math-simplify-expr)))))))
1313 (defvar math-try-cancel-units)
1316 (and math-simplifying-units
1317 (let ((np (cdr math-simplify-expr))
1318 (math-try-cancel-units 0)
1320 (setq n (if (eq (car-safe (nth 2 math-simplify-expr)) '*)
1321 (cdr (nth 2 math-simplify-expr))
1322 (nthcdr 2 math-simplify-expr)))
1323 (if (math-realp (car n))
1325 (setcar (cdr math-simplify-expr) (math-mul (nth 1 math-simplify-expr)
1326 (let ((calc-prefer-frac nil))
1327 (math-div 1 (car n)))))
1329 (while (eq (car-safe (setq n (car np))) '*)
1330 (math-simplify-units-divisor (cdr n) (cdr (cdr math-simplify-expr)))
1331 (setq np (cdr (cdr n))))
1332 (math-simplify-units-divisor np (cdr (cdr math-simplify-expr)))
1333 (if (eq math-try-cancel-units 0)
1334 (let* ((math-simplifying-units nil)
1335 (base (math-simplify
1336 (math-to-standard-units math-simplify-expr nil))))
1337 (if (Math-numberp base)
1338 (setq math-simplify-expr base))))
1339 (if (eq (car-safe math-simplify-expr) '/)
1340 (math-simplify-units-prod))
1341 math-simplify-expr)))
1343 (defun math-simplify-units-divisor (np dp)
1346 (while (eq (car-safe (setq d (car dp))) '*)
1347 (when (setq temp (math-simplify-units-quotient n (nth 1 d)))
1348 (setcar np (setq n temp))
1350 (setq dp (cdr (cdr d))))
1351 (when (setq temp (math-simplify-units-quotient n d))
1352 (setcar np (setq n temp))
1355 ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.
1356 (defun math-simplify-units-quotient (n d)
1359 (when (and (eq (car-safe n) '^)
1360 (integerp (nth 2 n)))
1361 (setq pow1 (nth 2 n) n (nth 1 n)))
1362 (when (and (eq (car-safe d) '^)
1363 (integerp (nth 2 d)))
1364 (setq pow2 (nth 2 d) d (nth 1 d)))
1365 (let ((un (math-check-unit-name n))
1366 (ud (math-check-unit-name d)))
1368 (if (and (equal (nth 4 un) (nth 4 ud))
1371 (math-to-standard-units (list '/ n d) nil)
1372 (list '^ (math-to-standard-units (list '/ n d) nil) pow1))
1379 (and (eq (car (car un)) (car (car ud1)))
1380 (setq math-try-cancel-units
1381 (+ math-try-cancel-units
1382 (- (* (cdr (car un)) pow1)
1383 (* (cdr (car ud)) pow2)))))
1384 (setq ud1 (cdr ud1)))
1389 (and math-simplifying-units
1390 (math-realp (nth 2 math-simplify-expr))
1391 (if (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
1392 (list (car (nth 1 math-simplify-expr))
1393 (list '^ (nth 1 (nth 1 math-simplify-expr))
1394 (nth 2 math-simplify-expr))
1395 (list '^ (nth 2 (nth 1 math-simplify-expr))
1396 (nth 2 math-simplify-expr)))
1397 (math-simplify-units-pow (nth 1 math-simplify-expr)
1398 (nth 2 math-simplify-expr)))))
1400 (math-defsimplify calcFunc-sqrt
1401 (and math-simplifying-units
1402 (if (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
1403 (list (car (nth 1 math-simplify-expr))
1404 (list 'calcFunc-sqrt (nth 1 (nth 1 math-simplify-expr)))
1405 (list 'calcFunc-sqrt (nth 2 (nth 1 math-simplify-expr))))
1406 (math-simplify-units-pow (nth 1 math-simplify-expr) '(frac 1 2)))))
1408 (math-defsimplify (calcFunc-floor
1418 (and math-simplifying-units
1419 (= (length math-simplify-expr) 2)
1420 (if (math-only-units-in-expr-p (nth 1 math-simplify-expr))
1421 (nth 1 math-simplify-expr)
1422 (if (and (memq (car-safe (nth 1 math-simplify-expr)) '(* /))
1423 (or (math-only-units-in-expr-p
1424 (nth 1 (nth 1 math-simplify-expr)))
1425 (math-only-units-in-expr-p
1426 (nth 2 (nth 1 math-simplify-expr)))))
1427 (list (car (nth 1 math-simplify-expr))
1428 (cons (car math-simplify-expr)
1429 (cons (nth 1 (nth 1 math-simplify-expr))
1430 (cdr (cdr math-simplify-expr))))
1431 (cons (car math-simplify-expr)
1432 (cons (nth 2 (nth 1 math-simplify-expr))
1433 (cdr (cdr math-simplify-expr)))))))))
1435 (defun math-simplify-units-pow (a pow)
1436 (if (and (eq (car-safe a) '^)
1437 (math-check-unit-name (nth 1 a))
1438 (math-realp (nth 2 a)))
1439 (list '^ (nth 1 a) (math-mul pow (nth 2 a)))
1440 (let* ((u (math-check-unit-name a))
1441 (pf (math-to-simple-fraction pow))
1442 (d (and (eq (car-safe pf) 'frac) (nth 2 pf))))
1444 (math-units-are-multiple u d)
1445 (list '^ (math-to-standard-units a nil) pow)))))
1448 (defun math-units-are-multiple (u n)
1450 (while (and u (= (% (cdr (car u)) n) 0))
1454 (math-defsimplify calcFunc-sin
1455 (and math-simplifying-units
1456 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1457 (let ((rad (math-simplify-units
1459 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1460 (calc-angle-mode 'rad))
1461 (and (eq (car-safe rad) '*)
1462 (math-realp (nth 1 rad))
1463 (eq (car-safe (nth 2 rad)) 'var)
1464 (eq (nth 1 (nth 2 rad)) 'rad)
1465 (list 'calcFunc-sin (nth 1 rad))))))
1467 (math-defsimplify calcFunc-cos
1468 (and math-simplifying-units
1469 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1470 (let ((rad (math-simplify-units
1472 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1473 (calc-angle-mode 'rad))
1474 (and (eq (car-safe rad) '*)
1475 (math-realp (nth 1 rad))
1476 (eq (car-safe (nth 2 rad)) 'var)
1477 (eq (nth 1 (nth 2 rad)) 'rad)
1478 (list 'calcFunc-cos (nth 1 rad))))))
1480 (math-defsimplify calcFunc-tan
1481 (and math-simplifying-units
1482 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1483 (let ((rad (math-simplify-units
1485 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1486 (calc-angle-mode 'rad))
1487 (and (eq (car-safe rad) '*)
1488 (math-realp (nth 1 rad))
1489 (eq (car-safe (nth 2 rad)) 'var)
1490 (eq (nth 1 (nth 2 rad)) 'rad)
1491 (list 'calcFunc-tan (nth 1 rad))))))
1493 (math-defsimplify calcFunc-sec
1494 (and math-simplifying-units
1495 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1496 (let ((rad (math-simplify-units
1498 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1499 (calc-angle-mode 'rad))
1500 (and (eq (car-safe rad) '*)
1501 (math-realp (nth 1 rad))
1502 (eq (car-safe (nth 2 rad)) 'var)
1503 (eq (nth 1 (nth 2 rad)) 'rad)
1504 (list 'calcFunc-sec (nth 1 rad))))))
1506 (math-defsimplify calcFunc-csc
1507 (and math-simplifying-units
1508 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1509 (let ((rad (math-simplify-units
1511 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1512 (calc-angle-mode 'rad))
1513 (and (eq (car-safe rad) '*)
1514 (math-realp (nth 1 rad))
1515 (eq (car-safe (nth 2 rad)) 'var)
1516 (eq (nth 1 (nth 2 rad)) 'rad)
1517 (list 'calcFunc-csc (nth 1 rad))))))
1519 (math-defsimplify calcFunc-cot
1520 (and math-simplifying-units
1521 (math-units-in-expr-p (nth 1 math-simplify-expr) nil)
1522 (let ((rad (math-simplify-units
1524 (math-to-standard-units (nth 1 math-simplify-expr) nil))))
1525 (calc-angle-mode 'rad))
1526 (and (eq (car-safe rad) '*)
1527 (math-realp (nth 1 rad))
1528 (eq (car-safe (nth 2 rad)) 'var)
1529 (eq (nth 1 (nth 2 rad)) 'rad)
1530 (list 'calcFunc-cot (nth 1 rad))))))
1533 (defun math-remove-units (expr)
1534 (if (math-check-unit-name expr)
1536 (if (Math-primp expr)
1539 (mapcar 'math-remove-units (cdr expr))))))
1541 (defun math-extract-units (expr)
1543 ((memq (car-safe expr) '(* /))
1545 (mapcar 'math-extract-units (cdr expr))))
1546 ((eq (car-safe expr) 'neg)
1547 (math-extract-units (nth 1 expr)))
1548 ((eq (car-safe expr) '^)
1549 (list '^ (math-extract-units (nth 1 expr)) (nth 2 expr)))
1550 ((math-check-unit-name expr) expr)
1553 (defun math-build-units-table-buffer (enter-buffer)
1554 (if (not (and math-units-table math-units-table-buffer-valid
1555 (get-buffer "*Units Table*")))
1556 (let ((buf (get-buffer-create "*Units Table*"))
1557 (uptr (math-build-units-table))
1558 (calc-language (if (eq calc-language 'big) nil calc-language))
1559 (calc-float-format '(float 0))
1560 (calc-group-digits nil)
1561 (calc-number-radix 10)
1562 (calc-twos-complement-mode nil)
1563 (calc-point-char ".")
1567 (message "Formatting units table...")
1569 (let ((inhibit-read-only t))
1571 (insert "Calculator Units Table:\n\n")
1573 "(All definitions are exact unless marked with an asterisk (*).)\n\n")
1574 (insert "Unit Type Definition Description\n\n")
1578 (when (eq (car u) 'm)
1580 (setq shadowed (and std (assq (car u) math-additional-units)))
1583 (eq (aref name 0) ?\*))
1584 (unless (eq uptr math-units-table)
1586 (setq name (substring name 1)))
1588 (and shadowed (insert "("))
1589 (insert (symbol-name (car u)))
1590 (and shadowed (insert ")"))
1594 (insert (symbol-name (nth 3 u))))
1600 (and shadowed (insert "("))
1604 (insert (math-format-value (nth 1 u) 80))
1605 (insert (symbol-name (car u)))))
1606 (and shadowed (insert ")"))
1612 (insert " (redefined above)")
1614 (insert " (base unit)")))
1616 (setq uptr (cdr uptr)))
1617 (insert "\n\nUnit Prefix Table:\n\n")
1618 (setq uptr math-unit-prefixes)
1621 (insert " " (char-to-string (car u)))
1622 (if (equal (nth 1 u) (nth 1 (nth 1 uptr)))
1623 (insert " " (char-to-string (car (car (setq uptr (cdr uptr)))))
1626 (insert "10^" (int-to-string (nth 2 (nth 1 u))))
1628 (insert " " (nth 2 u) "\n")
1629 (while (eq (car (car (setq uptr (cdr uptr)))) 0)))
1634 "(**) When in TeX or LaTeX display mode, the TeX specific unit\n"
1635 "names will not use the `tex' prefix; the unit name for a\n"
1636 "TeX point will be `pt' instead of `texpt', for example.\n"
1637 "To avoid conflicts, the unit names for pint and parsec will\n"
1638 "be `pint' and `parsec' instead of `pt' and `pc'."))))
1640 (message "Formatting units table...done"))
1641 (setq math-units-table-buffer-valid t)
1642 (let ((oldbuf (current-buffer)))
1644 (goto-char (point-min))
1645 (set-buffer oldbuf))
1648 (display-buffer buf)))
1650 (pop-to-buffer (get-buffer "*Units Table*"))
1651 (display-buffer (get-buffer "*Units Table*")))))
1653 ;;; Logarithmic units functions
1655 (defvar math-logunits '((var dB var-dB)
1658 (defun math-conditional-apply (fn &rest args)
1659 "Evaluate f(args) unless in symbolic mode.
1660 In symbolic mode, return the list (fn args)."
1661 (if calc-symbolic-mode
1665 (defun math-conditional-pow (a b)
1666 "Evaluate a^b unless in symbolic mode.
1667 In symbolic mode, return the list (^ a b)."
1668 (if calc-symbolic-mode
1672 (defun math-extract-logunits (expr)
1673 (if (memq (car-safe expr) '(* /))
1675 (mapcar 'math-extract-logunits (cdr expr)))
1676 (if (memq (car-safe expr) '(^))
1677 (list '^ (math-extract-logunits (nth 1 expr)) (nth 2 expr))
1678 (if (member expr math-logunits) expr 1))))
1680 (defun math-logunits-add (a b neg power)
1681 (let ((aunit (math-simplify (math-extract-logunits a))))
1682 (if (not (eq (car-safe aunit) 'var))
1683 (calc-record-why "*Improper logarithmic unit" aunit)
1684 (let* ((units (math-extract-units a))
1685 (acoeff (math-simplify (math-remove-units a)))
1686 (bcoeff (math-simplify (math-to-standard-units
1687 (list '/ b units) nil))))
1688 (if (math-units-in-expr-p bcoeff nil)
1689 (calc-record-why "*Inconsistent units" nil)
1691 (or (math-lessp acoeff bcoeff)
1692 (math-equal acoeff bcoeff)))
1693 (calc-record-why "*Improper coefficients" nil)
1695 (if (equal aunit '(var dB var-dB))
1696 (let ((coef (if power 10 20)))
1698 (math-conditional-apply 'calcFunc-log10
1701 (math-conditional-pow 10 (math-div acoeff coef))
1702 (math-conditional-pow 10 (math-div bcoeff coef)))
1704 (math-conditional-pow 10 (math-div acoeff coef))
1705 (math-conditional-pow 10 (math-div bcoeff coef)))))))
1706 (let ((coef (if power 2 1)))
1708 (math-conditional-apply 'calcFunc-ln
1711 (math-conditional-apply 'calcFunc-exp (math-mul coef acoeff))
1712 (math-conditional-apply 'calcFunc-exp (math-mul coef bcoeff)))
1714 (math-conditional-apply 'calcFunc-exp (math-mul coef acoeff))
1715 (math-conditional-apply 'calcFunc-exp (math-mul coef bcoeff)))))
1719 (defun calcFunc-lufadd (a b)
1720 (math-logunits-add a b nil nil))
1722 (defun calcFunc-lupadd (a b)
1723 (math-logunits-add a b nil t))
1725 (defun calcFunc-lufsub (a b)
1726 (math-logunits-add a b t nil))
1728 (defun calcFunc-lupsub (a b)
1729 (math-logunits-add a b t t))
1731 (defun calc-lu-plus (arg)
1734 (if (calc-is-inverse)
1735 (if (calc-is-hyperbolic)
1736 (calc-binary-op "lu-" 'calcFunc-lufsub arg)
1737 (calc-binary-op "lu-" 'calcFunc-lupsub arg))
1738 (if (calc-is-hyperbolic)
1739 (calc-binary-op "lu+" 'calcFunc-lufadd arg)
1740 (calc-binary-op "lu+" 'calcFunc-lupadd arg)))))
1742 (defun calc-lu-minus (arg)
1745 (if (calc-is-inverse)
1746 (if (calc-is-hyperbolic)
1747 (calc-binary-op "lu+" 'calcFunc-lufadd arg)
1748 (calc-binary-op "lu+" 'calcFunc-lupadd arg))
1749 (if (calc-is-hyperbolic)
1750 (calc-binary-op "lu-" 'calcFunc-lufsub arg)
1751 (calc-binary-op "lu-" 'calcFunc-lupsub arg)))))
1753 (defun math-logunits-mul (a b power)
1754 (let (logunit coef units number)
1757 (setq logunit (math-simplify (math-extract-logunits a)))
1758 (eq (car-safe logunit) 'var)
1759 (eq (math-simplify (math-extract-units b)) 1))
1760 (setq coef (math-simplify (math-remove-units a))
1761 units (math-extract-units a)
1764 (setq logunit (math-simplify (math-extract-logunits b)))
1765 (eq (car-safe logunit) 'var)
1766 (eq (math-simplify (math-extract-units a)) 1))
1767 (setq coef (math-simplify (math-remove-units b))
1768 units (math-extract-units b)
1770 (t (setq logunit nil)))
1773 ((equal logunit '(var dB var-dB))
1778 (math-mul (if power 10 20)
1779 (math-conditional-apply 'calcFunc-log10 number)))
1786 (math-div (math-conditional-apply 'calcFunc-ln number) (if power 2 1)))
1788 (calc-record-why "*Improper units" nil))))
1790 (defun math-logunits-divide (a b power)
1791 (let ((logunit (math-simplify (math-extract-logunits a))))
1792 (if (not (eq (car-safe logunit) 'var))
1793 (calc-record-why "*Improper logarithmic unit" logunit)
1794 (if (math-units-in-expr-p b nil)
1795 (calc-record-why "*Improper units quantity" b)
1796 (let* ((units (math-extract-units a))
1797 (coef (math-simplify (math-remove-units a))))
1799 ((equal logunit '(var dB var-dB))
1804 (math-mul (if power 10 20)
1805 (math-conditional-apply 'calcFunc-log10 b)))
1812 (math-div (math-conditional-apply 'calcFunc-ln b) (if power 2 1)))
1815 (defun calcFunc-lufmul (a b)
1816 (math-logunits-mul a b nil))
1818 (defun calcFunc-lupmul (a b)
1819 (math-logunits-mul a b t))
1821 (defun calc-lu-times (arg)
1824 (if (calc-is-inverse)
1825 (if (calc-is-hyperbolic)
1826 (calc-binary-op "lu/" 'calcFunc-lufdiv arg)
1827 (calc-binary-op "lu/" 'calcFunc-lupdiv arg))
1828 (if (calc-is-hyperbolic)
1829 (calc-binary-op "lu*" 'calcFunc-lufmul arg)
1830 (calc-binary-op "lu*" 'calcFunc-lupmul arg)))))
1832 (defun calcFunc-lufdiv (a b)
1833 (math-logunits-divide a b nil))
1835 (defun calcFunc-lupdiv (a b)
1836 (math-logunits-divide a b t))
1838 (defun calc-lu-divide (arg)
1841 (if (calc-is-inverse)
1842 (if (calc-is-hyperbolic)
1843 (calc-binary-op "lu*" 'calcFunc-lufmul arg)
1844 (calc-binary-op "lu*" 'calcFunc-lupmul arg))
1845 (if (calc-is-hyperbolic)
1846 (calc-binary-op "lu/" 'calcFunc-lufdiv arg)
1847 (calc-binary-op "lu/" 'calcFunc-lupdiv arg)))))
1849 (defun math-logunits-quant (val ref power)
1850 (let* ((units (math-simplify (math-extract-units val)))
1851 (lunit (math-simplify (math-extract-logunits units))))
1852 (if (not (eq (car-safe lunit) 'var))
1853 (calc-record-why "*Improper logarithmic unit" lunit)
1854 (let ((runits (math-simplify (math-div units lunit)))
1855 (coeff (math-simplify (math-div val units))))
1857 (if (equal lunit '(var dB var-dB))
1860 (math-conditional-pow
1867 (math-conditional-apply 'calcFunc-exp
1873 (defvar calc-lu-field-reference)
1874 (defvar calc-lu-power-reference)
1876 (defun calcFunc-lufquant (val &optional ref)
1878 (setq ref (math-read-expr calc-lu-field-reference)))
1879 (math-logunits-quant val ref nil))
1881 (defun calcFunc-lupquant (val &optional ref)
1883 (setq ref (math-read-expr calc-lu-power-reference)))
1884 (math-logunits-quant val ref t))
1886 (defun calc-lu-quant (arg)
1889 (if (calc-is-hyperbolic)
1890 (if (calc-is-option)
1891 (calc-binary-op "lupq" 'calcFunc-lufquant arg)
1892 (calc-unary-op "lupq" 'calcFunc-lufquant arg))
1893 (if (calc-is-option)
1894 (calc-binary-op "lufq" 'calcFunc-lupquant arg)
1895 (calc-unary-op "lufq" 'calcFunc-lupquant arg)))))
1897 (defun math-logunits-level (val ref db power)
1898 "Compute the value of VAL in decibels or nepers."
1899 (let* ((ratio (math-simplify-units (math-div val ref)))
1900 (ratiou (math-simplify-units (math-remove-units ratio)))
1901 (units (math-simplify (math-extract-units ratio))))
1905 (math-mul (if power 10 20)
1906 (math-conditional-apply 'calcFunc-log10 ratiou))
1909 (math-div (math-conditional-apply 'calcFunc-ln ratiou) (if power 2 1))
1913 (defun calcFunc-dbfield (val &optional ref)
1915 (setq ref (math-read-expr calc-lu-field-reference)))
1916 (math-logunits-level val ref t nil))
1918 (defun calcFunc-dbpower (val &optional ref)
1920 (setq ref (math-read-expr calc-lu-power-reference)))
1921 (math-logunits-level val ref t t))
1923 (defun calcFunc-npfield (val &optional ref)
1925 (setq ref (math-read-expr calc-lu-field-reference)))
1926 (math-logunits-level val ref nil nil))
1928 (defun calcFunc-nppower (val &optional ref)
1930 (setq ref (math-read-expr calc-lu-power-reference)))
1931 (math-logunits-level val ref nil t))
1933 (defun calc-db (arg)
1936 (if (calc-is-hyperbolic)
1937 (if (calc-is-option)
1938 (calc-binary-op "ludb" 'calcFunc-dbfield arg)
1939 (calc-unary-op "ludb" 'calcFunc-dbfield arg))
1940 (if (calc-is-option)
1941 (calc-binary-op "ludb" 'calcFunc-dbpower arg)
1942 (calc-unary-op "ludb" 'calcFunc-dbpower arg)))))
1944 (defun calc-np (arg)
1947 (if (calc-is-hyperbolic)
1948 (if (calc-is-option)
1949 (calc-binary-op "lunp" 'calcFunc-npfield arg)
1950 (calc-unary-op "lunp" 'calcFunc-npfield arg))
1951 (if (calc-is-option)
1952 (calc-binary-op "lunp" 'calcFunc-nppower arg)
1953 (calc-unary-op "lunp" 'calcFunc-nppower arg)))))
1958 (defvar calc-note-threshold)
1960 (defun math-midi-round (num)
1961 "Round NUM to an integer N if NUM is within calc-note-threshold cents of N."
1962 (let* ((n (math-round num))
1965 (if (< (math-compare diff
1966 (math-div (math-read-expr calc-note-threshold) 100)) 0)
1970 (defconst math-notes
1971 '(((var C var-C) . 0)
1972 ((var Csharp var-Csharp) . 1)
1973 ; ((var C♯ var-C♯) . 1)
1974 ((var Dflat var-Dflat) . 1)
1975 ; ((var D♭ var-D♭) . 1)
1977 ((var Dsharp var-Dsharp) . 3)
1978 ; ((var D♯ var-D♯) . 3)
1981 ((var Fsharp var-Fsharp) . 6)
1982 ; ((var F♯ var-F♯) . 6)
1983 ((var Gflat var-Gflat) . 6)
1984 ; ((var G♭ var-G♭) . 6)
1986 ((var Gsharp var-Gsharp) . 8)
1987 ; ((var G♯ var-G♯) . 8)
1989 ((var Asharp var-Asharp) . 10)
1990 ; ((var A♯ var-A♯) . 10)
1991 ((var Bflat var-Bflat) . 10)
1992 ; ((var B♭ var-B♭) . 10)
1993 ((var B var-B) . 11))
1994 "An alist of notes with their number of semitones above C.")
1996 (defun math-freqp (freq)
1997 "Non-nil if FREQ is a positive number times the unit Hz.
1998 If non-nil, return the coefficient of Hz."
1999 (let ((freqcoef (math-simplify-units
2000 (math-div freq '(var Hz var-Hz)))))
2001 (if (Math-posp freqcoef) freqcoef)))
2003 (defun math-midip (num)
2004 "Non-nil if NUM is a possible MIDI note number.
2005 If non-nil, return NUM."
2006 (if (Math-numberp num) num))
2008 (defun math-spnp (spn)
2009 "Non-nil if NUM is a scientific pitch note (note + cents).
2010 If non-nil, return a list consisting of the note and the cents coefficient."
2011 (let (note cents rnote rcents)
2012 (if (eq (car-safe spn) '+)
2013 (setq note (nth 1 spn)
2018 ((and ;; NOTE is a note, CENTS is nil or cents.
2019 (eq (car-safe note) 'calcFunc-subscr)
2020 (assoc (nth 1 note) math-notes)
2021 (integerp (nth 2 note))
2025 (Math-numberp (setq rcents
2027 (math-div cents '(var cents var-cents)))))))
2028 (list rnote rcents))
2029 ((and ;; CENTS is a note, NOTE is cents.
2030 (eq (car-safe cents) 'calcFunc-subscr)
2031 (assoc (nth 1 cents) math-notes)
2032 (integerp (nth 2 cents))
2036 (Math-numberp (setq rcents
2038 (math-div note '(var cents var-cents)))))))
2039 (list rnote rcents)))))
2041 (defun math-freq-to-midi (freq)
2042 "Return the midi note number corresponding to FREQ Hz."
2043 (let ((midi (math-add
2050 (math-midi-round midi)))
2052 (defun math-spn-to-midi (spn)
2053 "Return the MIDI number corresponding to SPN."
2054 (let* ((note (cdr (assoc (nth 1 (car spn)) math-notes)))
2055 (octave (math-add (nth 2 (car spn)) 1))
2058 (math-mul 12 octave)
2061 (math-add midi (math-div cents 100))
2064 (defun math-midi-to-spn (midi)
2065 "Return the scientific pitch notation corresponding to midi number MIDI."
2067 (if (math-integerp midi)
2070 (setq midin (math-floor midi)
2071 cents (math-mul 100 (math-sub midi midin))))
2072 (let* ((nr ;; This should be (math-idivmod midin 12), but with
2073 ;; better behavior for negative midin.
2074 (if (Math-negp midin)
2075 (let ((dm (math-idivmod (math-neg midin) 12)))
2077 (cons (math-neg (car dm)) 0)
2079 (math-sub (math-neg (car dm)) 1)
2080 (math-sub 12 (cdr dm)))))
2081 (math-idivmod midin 12)))
2082 (n (math-sub (car nr) 1))
2083 (note (car (rassoc (cdr nr) math-notes))))
2085 (list '+ (list 'calcFunc-subscr note n)
2086 (list '* cents '(var cents var-cents)))
2087 (list 'calcFunc-subscr note n)))))
2089 (defun math-freq-to-spn (freq)
2090 "Return the scientific pitch notation corresponding to FREQ Hz."
2091 (math-with-extra-prec 3
2092 (math-midi-to-spn (math-freq-to-midi freq))))
2094 (defun math-midi-to-freq (midi)
2095 "Return the frequency of the note with midi number MIDI."
2108 (defun math-spn-to-freq (spn)
2109 "Return the frequency of the note with scientific pitch notation SPN."
2110 (math-midi-to-freq (math-spn-to-midi spn)))
2112 (defun calcFunc-spn (expr)
2113 "Return EXPR written as scientific pitch notation + cents."
2114 ;; Get the coefficient of Hz
2117 ((setq note (math-freqp expr))
2118 (math-freq-to-spn note))
2119 ((setq note (math-midip expr))
2120 (math-midi-to-spn note))
2124 (math-reject-arg expr "*Improper expression")))))
2126 (defun calcFunc-midi (expr)
2127 "Return EXPR written as a MIDI number."
2130 ((setq note (math-freqp expr))
2131 (math-freq-to-midi note))
2132 ((setq note (math-spnp expr))
2133 (math-spn-to-midi note))
2137 (math-reject-arg expr "*Improper expression")))))
2139 (defun calcFunc-freq (expr)
2140 "Return the frequency corresponding to EXPR."
2143 ((setq note (math-midip expr))
2144 (math-midi-to-freq note))
2145 ((setq note (math-spnp expr))
2146 (math-spn-to-freq note))
2150 (math-reject-arg expr "*Improper expression")))))
2152 (defun calc-freq (arg)
2153 "Return the frequency corresponding to the expression on the stack."
2156 (calc-unary-op "freq" 'calcFunc-freq arg)))
2158 (defun calc-midi (arg)
2159 "Return the MIDI number corresponding to the expression on the stack."
2162 (calc-unary-op "midi" 'calcFunc-midi arg)))
2164 (defun calc-spn (arg)
2165 "Return the scientific pitch notation corresponding to the expression on the stack."
2168 (calc-unary-op "spn" 'calcFunc-spn arg)))
2171 (provide 'calc-units)
2173 ;;; calc-units.el ends here