1 ;;; calc-units.el --- unit conversion functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2017 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 ;; CODATA values updated February 2016, using 2014 adjustment
47 ;; http://arxiv.org/pdf/1507.07956.pdf
49 (defvar math-standard-units
52 ( in
"254*10^(-2) cm" "Inch" nil
56 ( mi
"5280 ft" "Mile" )
57 ( au
"149597870691. m" "Astronomical Unit" nil
59 ;; (approx) NASA JPL (http://neo.jpl.nasa.gov/glossary/au.html)
60 ( lyr
"c yr" "Light Year" )
61 ( pc
"3.0856775854*10^16 m" "Parsec (**)" nil
62 "3.0856775854 10^16 m (*)") ;; (approx) ESUWM
63 ( nmi
"1852 m" "Nautical Mile" )
64 ( fath
"6 ft" "Fathom" )
65 ( fur
"660 ft" "Furlong")
66 ( mu
"1 um" "Micron" )
67 ( mil
"(1/1000) in" "Mil" )
68 ( point
"(1/72) in" "Point (PostScript convention)" )
69 ( Ang
"10^(-10) m" "Angstrom" )
70 ( mfi
"mi+ft+in" "Miles + feet + inches" )
72 ( texpt
"(100/7227) in" "Point (TeX convention) (**)" )
73 ( texpc
"12 texpt" "Pica (TeX convention) (**)" )
74 ( texbp
"point" "Big point (TeX convention) (**)" )
75 ( texdd
"(1238/1157) texpt" "Didot point (TeX convention) (**)" )
76 ( texcc
"12 texdd" "Cicero (TeX convention) (**)" )
77 ( texsp
"(1/65536) texpt" "Scaled TeX point (TeX convention) (**)" )
80 ( hect
"10000 m^2" "*Hectare" )
82 ( acre
"(1/640) mi^2" "Acre" )
83 ( b
"10^(-28) m^2" "Barn" )
86 ( L
"10^(-3) m^3" "*Liter" )
88 ( gal
"4 qt" "US Gallon" )
90 ( pt
"2 cup" "Pint (**)" )
91 ( cup
"8 ozfl" "Cup" )
92 ( ozfl
"2 tbsp" "Fluid Ounce" )
93 ( floz
"2 tbsp" "Fluid Ounce" )
94 ( tbsp
"3 tsp" "Tablespoon" )
95 ;; ESUWM defines a US gallon as 231 in^3.
96 ;; That gives the following exact value for tsp.
97 ( tsp
"492892159375*10^(-11) ml" "Teaspoon" nil
99 ( vol
"tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" nil
100 "tsp+tbsp+ozfl+cup+pt+qt+gal")
101 ( galC
"galUK" "Canadian Gallon" )
102 ( galUK
"454609*10^(-5) L" "UK Gallon" nil
108 ( min
"60 s" "Minute" )
109 ( hr
"60 min" "Hour" )
110 ( day
"24 hr" "Day" )
111 ( wk
"7 day" "Week" )
112 ( hms
"wk+day+hr+min+s" "Hours, minutes, seconds" )
113 ( yr
"36525*10^(-2) day" "Year (Julian)" nil
118 ( mph
"mi/hr" "*Miles per hour" )
119 ( kph
"km/hr" "Kilometers per hour" )
120 ( knot
"nmi/hr" "Knot" )
121 ( c
"299792458 m/s" "Speed of light" ) ;;; CODATA
124 ( ga
"980665*10^(-5) m/s^2" "*\"g\" acceleration" nil
125 "9.80665 m / s^2") ;; CODATA
129 ( lb
"16 oz" "Pound (mass)" )
130 ( oz
"28349523125*10^(-9) g" "Ounce (mass)" nil
131 "28.349523125 g") ;; ESUWM
132 ( ton
"2000 lb" "Ton" )
133 ( tpo
"ton+lb+oz" "Tons + pounds + ounces (mass)" )
134 ( t
"1000 kg" "Metric ton" )
135 ( tonUK
"10160469088*10^(-7) kg" "UK ton" nil
136 "1016.0469088 kg") ;; ESUWM
137 ( lbt
"12 ozt" "Troy pound" )
138 ( ozt
"311034768*10^(-7) g" "Troy ounce" nil
139 "31.10347680 g") ;; ESUWM, 1/12 exact value for lbt
140 ( ct
"(2/10) g" "Carat" nil
142 ( u
"1.660539040*10^(-27) kg" "Unified atomic mass" nil
143 "1.660539040 10^-27 kg (*)");;(approx) CODATA
146 ( N
"m kg/s^2" "*Newton" )
147 ( dyn
"10^(-5) N" "Dyne" )
148 ( gf
"ga g" "Gram (force)" )
149 ( lbf
"ga lb" "Pound (force)" )
150 ( kip
"1000 lbf" "Kilopound (force)" )
151 ( pdl
"138254954376*10^(-12) N" "Poundal" nil
152 "0.138254954376 N") ;; ESUWM
156 ( erg
"10^(-7) J" "Erg" )
157 ( cal
"41868*10^(-4) J" "International Table Calorie" nil
159 ( calth
"4184*10^(-3) J" "Thermochemical Calorie" nil
161 ( Cal
"1000 cal" "Large Calorie")
162 ( Btu
"105505585262*10^(-8) J" "International Table Btu" nil
163 "1055.05585262 J") ;; ESUWM
164 ( eV
"ech V" "Electron volt" )
165 ( ev
"eV" "Electron volt" )
166 ( therm
"105506000 J" "EEC therm" )
167 ( invcm
"h c/cm" "Energy in inverse centimeters" )
168 ( Kayser
"invcm" "Kayser (inverse centimeter energy)" )
169 ( men
"100/invcm" "Inverse energy in meters" )
170 ( Hzen
"h Hz" "Energy in Hertz")
171 ( Ken
"k K" "Energy in Kelvins")
172 ( Wh
"W hr" "Watt hour")
173 ( Ws
"W s" "Watt second")
177 ( hp
"550 ft lbf/s" "Horsepower") ;;ESUWM
178 ( hpm
"75 m kgf/s" "Metric Horsepower") ;;ESUWM
181 ( K nil
"*Degree Kelvin" K
)
182 ( dK
"K" "Degree Kelvin" K
)
183 ( degK
"K" "Degree Kelvin" K
)
184 ( dC
"K" "Degree Celsius" C
)
185 ( degC
"K" "Degree Celsius" C
)
186 ( dF
"(5/9) K" "Degree Fahrenheit" F
)
187 ( degF
"(5/9) K" "Degree Fahrenheit" F
)
190 ( Pa
"N/m^2" "*Pascal" )
191 ( bar
"10^5 Pa" "Bar" )
192 ( atm
"101325 Pa" "Standard atmosphere" ) ;; CODATA
193 ( Torr
"(1/760) atm" "Torr")
194 ( mHg
"1000 Torr" "Meter of mercury" )
195 ( inHg
"254*10^(-1) mmHg" "Inch of mercury" nil
197 ( inH2O
"2.490889*10^2 Pa" "Inch of water" nil
198 "2.490889 10^2 Pa (*)") ;;(approx) NIST
199 ( psi
"lbf/in^2" "Pounds per square inch" )
202 ( P
"(1/10) Pa s" "*Poise" )
203 ( St
"10^(-4) m^2/s" "Stokes" )
207 ( C
"A s" "Coulomb" )
208 ( Fdy
"ech Nav" "Faraday" )
209 ( e
"ech" "Elementary charge" )
210 ( ech
"1.6021766208*10^(-19) C" "Elementary charge" nil
211 "1.6021766208 10^-19 C (*)") ;;(approx) CODATA
216 ( S
"A/V" "Siemens" )
219 ( T
"Wb/m^2" "Tesla" )
220 ( Gs
"10^(-4) T" "Gauss" )
223 ;; Luminous intensity
224 ( cd nil
"*Candela" )
225 ( sb
"10000 cd/m^2" "Stilb" )
226 ( lm
"cd sr" "Lumen" )
227 ( lx
"lm/m^2" "Lux" )
228 ( ph
"10000 lx" "Phot" )
229 ( fc
"lm/ft^2" "Footcandle") ;; ESUWM
230 ( lam
"10000 lm/m^2" "Lambert" )
231 ( flam
"(1/pi) cd/ft^2" "Footlambert") ;; ESUWM
234 ( Bq
"1/s" "*Becquerel" )
235 ( Ci
"37*10^9 Bq" "Curie" ) ;; ESUWM
237 ( Sv
"Gy" "Sievert" )
238 ( R
"258*10^(-6) C/kg" "Roentgen" ) ;; NIST
239 ( rd
"(1/100) Gy" "Rad" )
242 ;; Amount of substance
246 ( rad nil
"*Radian" )
247 ( circ
"2 pi rad" "Full circle" )
248 ( rev
"circ" "Full revolution" )
249 ( deg
"circ/360" "Degree" )
250 ( arcmin
"deg/60" "Arc minute" )
251 ( arcsec
"arcmin/60" "Arc second" )
252 ( grad
"circ/400" "Grade" )
253 ( rpm
"rev/min" "Revolutions per minute" )
256 ( sr nil
"*Steradian" )
258 ;; Other physical quantities
259 ;; The values are from CODATA, and are approximate.
260 ( h
"6.626070040*10^(-34) J s" "*Planck's constant" nil
261 "6.626070040 10^-34 J s (*)")
262 ( hbar
"h / (2 pi)" "Planck's constant" ) ;; Exact
263 ( mu0
"4 pi 10^(-7) H/m" "Permeability of vacuum") ;; Exact
264 ( μ
0 "mu0" "Permeability of vacuum") ;; Exact
265 ( eps0
"1 / (mu0 c^2)" "Permittivity of vacuum" )
266 ( ε
0 "eps0" "Permittivity of vacuum" )
267 ( G
"6.67408*10^(-11) m^3/(kg s^2)" "Gravitational constant" nil
268 "6.67408 10^-11 m^3/(kg s^2) (*)")
269 ( Nav
"6.022140857*10^(23) / mol" "Avogadro's constant" nil
270 "6.022140857 10^23 / mol (*)")
271 ( me
"9.10938356*10^(-31) kg" "Electron rest mass" nil
272 "9.10938356 10^-31 kg (*)")
273 ( mp
"1.672621898*10^(-27) kg" "Proton rest mass" nil
274 "1.672621898 10^-27 kg (*)")
275 ( mn
"1.674927471*10^(-27) kg" "Neutron rest mass" nil
276 "1.674927471 10^-27 kg (*)")
277 ( mmu
"1.883531594*10^(-28) kg" "Muon rest mass" nil
278 "1.883531594 10^-28 kg (*)")
279 ( mμ
"mmu" "Muon rest mass" nil
280 "1.883531594 10^-28 kg (*)")
281 ( Ryd
"10973731.568508 /m" "Rydberg's constant" nil
282 "10973731.568508 /m (*)")
283 ( k
"1.38064852*10^(-23) J/K" "Boltzmann's constant" nil
284 "1.38064852 10^-23 J/K (*)")
285 ( sigma
"5.670367*10^(-8) W/(m^2 K^4)" "Stefan-Boltzmann constant" nil
286 "5.670367 10^-8 W/(m^2 K^4) (*)")
287 ( σ
"sigma" "Stefan-Boltzmann constant" nil
288 "5.670367 10^-8 W/(m^2 K^4) (*)")
289 ( alpha
"7.2973525664*10^(-3)" "Fine structure constant" nil
290 "7.2973525664 10^-3 (*)")
291 ( α
"alpha" "Fine structure constant" nil
292 "7.2973525664 10^-3 (*)")
293 ( muB
"927.4009994*10^(-26) J/T" "Bohr magneton" nil
294 "927.4009994 10^-26 J/T (*)")
295 ( muN
"5.050783699*10^(-27) J/T" "Nuclear magneton" nil
296 "5.050783699 10^-27 J/T (*)")
297 ( mue
"-928.4764620*10^(-26) J/T" "Electron magnetic moment" nil
298 "-928.4764620 10^-26 J/T (*)")
299 ( mup
"1.4106067873*10^(-26) J/T" "Proton magnetic moment" nil
300 "1.4106067873 10^-26 J/T (*)")
301 ( R0
"8.3144598 J/(mol K)" "Molar gas constant" nil
302 "8.3144598 J/(mol K) (*)")
303 ( V0
"22.710947*10^(-3) m^3/mol" "Standard volume of ideal gas" nil
304 "22.710947 10^-3 m^3/mol (*)")
307 ( dB
"(ln(10)/20) Np" "decibel")))
310 (defvar math-additional-units nil
311 "Additional units table for user-defined units.
312 Must be formatted like `math-standard-units'.
313 If you change this, be sure to set `math-units-table' to nil to ensure
314 that the combined units table will be rebuilt.")
316 (defvar math-unit-prefixes
317 '( ( ?Y
(^
10 24) "Yotta" )
318 ( ?Z
(^
10 21) "Zetta" )
319 ( ?E
(^
10 18) "Exa" )
320 ( ?P
(^
10 15) "Peta" )
321 ( ?T
(^
10 12) "Tera" )
322 ( ?G
(^
10 9) "Giga" )
323 ( ?M
(^
10 6) "Mega" )
324 ( ?k
(^
10 3) "Kilo" )
325 ( ?K
(^
10 3) "Kilo" )
326 ( ?h
(^
10 2) "Hecto" )
327 ( ?H
(^
10 2) "Hecto" )
328 ( ?D
(^
10 1) "Deka" )
330 ( ?d
(^
10 -
1) "Deci" )
331 ( ?c
(^
10 -
2) "Centi" )
332 ( ?m
(^
10 -
3) "Milli" )
333 ( ?u
(^
10 -
6) "Micro" )
334 ( ?μ
(^
10 -
6) "Micro" )
335 ( ?n
(^
10 -
9) "Nano" )
336 ( ?p
(^
10 -
12) "Pico" )
337 ( ?f
(^
10 -
15) "Femto" )
338 ( ?a
(^
10 -
18) "Atto" )
339 ( ?z
(^
10 -
21) "zepto" )
340 ( ?y
(^
10 -
24) "yocto" )))
342 (defvar math-standard-units-systems
344 ( si
( ( g
'(/ (var kg var-kg
) 1000) ) ) )
345 ( mks
( ( g
'(/ (var kg var-kg
) 1000) ) ) )
346 ( cgs
( ( m
'(* (var cm var-cm
) 100 ) ) ) )))
348 (defvar math-units-table nil
349 "Internal units table.
350 Derived from `math-standard-units' and `math-additional-units'.
351 Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
353 (defvar math-units-table-buffer-valid nil
)
357 (defun calc-base-units ()
360 (let ((calc-autorange-units nil
))
361 (calc-enter-result 1 "bsun" (math-simplify-units
362 (math-to-standard-units (calc-top-n 1)
365 (defvar calc-ensure-consistent-units
)
367 (defun calc-quick-units ()
370 (let* ((num (- last-command-event ?
0))
371 (pos (if (= num
0) 10 num
))
372 (units (calc-var-value 'var-Units
))
373 (expr (calc-top-n 1)))
374 (unless (and (>= num
0) (<= num
9))
375 (error "Bad unit number"))
376 (unless (math-vectorp units
)
377 (error "No \"quick units\" are defined"))
378 (unless (< pos
(length units
))
379 (error "Unit number %d not defined" pos
))
380 (if (math-units-in-expr-p expr nil
)
382 (if calc-ensure-consistent-units
383 (math-check-unit-consistency expr
(nth pos units
)))
384 (calc-enter-result 1 (format "cun%d" num
)
385 (math-convert-units expr
(nth pos units
))))
386 (calc-enter-result 1 (format "*un%d" num
)
388 (math-mul expr
(nth pos units
))))))))
390 (defun math-get-standard-units (expr)
391 "Return the standard units in EXPR."
394 (math-to-standard-units expr nil
))))
396 (defun math-get-units (expr)
397 "Return the units in EXPR."
399 (math-extract-units expr
)))
401 (defun math-make-unit-string (expr)
402 "Return EXPR in string form.
403 If EXPR is nil, return nil."
405 (let ((cexpr (math-compose-expr expr
0)))
406 (replace-regexp-in-string
410 (math-composition-to-string cexpr
))))))
412 (defvar math-default-units-table
413 (make-hash-table :test
'equal
)
414 "A table storing previously converted units.")
416 (defun math-get-default-units (expr)
417 "Get default units to use when converting the units in EXPR."
418 (let* ((units (math-get-units expr
))
419 (standard-units (math-get-standard-units expr
))
420 (default-units (gethash
422 math-default-units-table
)))
423 (if (equal units
(car default-units
))
424 (math-make-unit-string (cadr default-units
))
425 (math-make-unit-string (car default-units
)))))
427 (defun math-put-default-units (expr &optional comp std
)
428 "Put the units in EXPR in the default units table.
429 If COMP or STD is non-nil, put that in the units table instead."
430 (let* ((new-units (or comp std
(math-get-units expr
)))
431 (standard-units (math-get-standard-units
433 (comp (math-simplify-units expr
))
436 (default-units (gethash standard-units math-default-units-table
)))
437 (unless (eq standard-units
1)
440 (puthash standard-units
(list new-units
) math-default-units-table
))
441 ((not (equal new-units
(car default-units
)))
442 (puthash standard-units
443 (list new-units
(car default-units
))
444 math-default-units-table
))))))
446 (defvar calc-allow-units-as-numbers t
)
448 (defun calc-convert-units (&optional old-units new-units
)
451 (let ((expr (calc-top-n 1))
458 (if (or (not (math-units-in-expr-p expr t
))
459 (setq unitscancel
(and
460 (if (get 'calc-allow-units-as-numbers
'saved-value
)
461 (car (get 'calc-allow-units-as-numbers
'saved-value
))
462 calc-allow-units-as-numbers
)
463 (eq (math-get-standard-units expr
) 1))))
464 (let ((uold (or old-units
469 "(The expression is unitless when simplified) Old Units: ")
470 (read-string "Old units: ")))
471 (if (equal uoldname
"")
473 (setq nouold unitscancel
)
476 (if (string-match "\\` */" uoldname
)
477 (setq uoldname
(concat "1" uoldname
)))
478 (math-read-expr uoldname
))))))
479 (unless (math-units-in-expr-p uold t
)
480 (error "No units specified"))
481 (when (eq (car-safe uold
) 'error
)
482 (error "Bad format in units expression: %s" (nth 1 uold
)))
483 (setq expr
(math-mul expr uold
))))
484 (setq defunits
(math-get-default-units expr
))
488 (if (and uoldname
(not nouold
))
489 (concat "Old units: "
500 (string= new-units
"")
502 (setq new-units defunits
)))
503 (when (string-match "\\` */" new-units
)
504 (setq new-units
(concat "1" new-units
)))
505 (setq units
(math-read-expr new-units
))
506 (when (eq (car-safe units
) 'error
)
507 (error "Bad format in units expression: %s" (nth 2 units
)))
508 (if calc-ensure-consistent-units
509 (math-check-unit-consistency expr units
))
510 (let ((unew (math-units-in-expr-p units t
))
511 (std (and (eq (car-safe units
) 'var
)
512 (assq (nth 1 units
) math-standard-units-systems
)))
513 (comp (eq (car-safe units
) '+)))
514 (unless (or unew std
)
515 (error "No units specified"))
516 (let* ((noold (and uoldname
(not (equal uoldname
"1"))))
519 (math-simplify-units (math-to-standard-units expr
(nth 1 std
)))
520 (math-convert-units expr units noold
))))
522 (math-put-default-units (if noold units res
) (if comp units
)))
523 (calc-enter-result 1 "cvun" res
))))))
525 (defun calc-convert-exact-units ()
528 (let* ((expr (calc-top-n 1)))
529 (unless (math-units-in-expr-p expr t
)
530 (error "No units in expression."))
531 (let* ((old-units (math-extract-units expr
))
532 (defunits (math-get-default-units expr
))
535 (read-string (concat "New units"
543 (string= new-units
"")
545 (setq new-units defunits
))
546 (setq units
(math-read-expr new-units
))
547 (when (eq (car-safe units
) 'error
)
548 (error "Bad format in units expression: %s" (nth 2 units
)))
549 (math-check-unit-consistency old-units units
)
551 (list '* (math-mul (math-remove-units expr
)
553 (math-to-standard-units (list '/ old-units units
) nil
)))
555 (calc-enter-result 1 "cvxu" res
))))))
557 (defun calc-autorange-units (arg)
560 (calc-change-mode 'calc-autorange-units arg nil t
)
561 (message (if calc-autorange-units
562 "Adjusting target unit prefix automatically"
563 "Using target units exactly"))))
565 (defun calc-convert-temperature (&optional old-units new-units
)
568 (let ((tempunits (delq nil
571 (if (nth 3 x
) (car x
)))
572 math-standard-units
)))
573 (expr (calc-top-n 1))
578 (setq uold
(or old-units
579 (let ((units (math-single-units-in-expr-p expr
)))
582 (list 'var
(car units
)
583 (intern (concat "var-"
586 (error "Not a pure temperature expression"))
588 (setq uoldname
(completing-read
589 "Old temperature units: "
591 (when (eq (car-safe uold
) 'error
)
592 (error "Bad format in units expression: %s" (nth 2 uold
)))
593 (or (math-units-in-expr-p expr nil
)
594 (setq expr
(math-mul expr uold
)))
595 (setq defunits
(math-get-default-units expr
))
596 (setq unew
(or new-units
600 (concat "Old temperature units: "
603 "New temperature units")
610 (setq unew
(math-read-expr (if (string= unew
"") defunits unew
)))
611 (when (eq (car-safe unew
) 'error
)
612 (error "Bad format in units expression: %s" (nth 2 unew
)))
613 (math-put-default-units unew
)
614 (let ((ntemp (calc-normalize
616 (math-convert-temperature expr uold unew
618 (if (Math-zerop ntemp
)
619 (setq ntemp
(list '* ntemp unew
)))
620 (let ((calc-simplify-mode 'none
))
621 (calc-enter-result 1 "cvtm" ntemp
))))))
623 (defun calc-remove-units ()
626 (calc-enter-result 1 "rmun" (math-simplify-units
627 (math-remove-units (calc-top-n 1))))))
629 (defun calc-extract-units ()
632 (calc-enter-result 1 "exun" (math-simplify-units
633 (math-extract-units (calc-top-n 1))))))
635 ;; The variables calc-num-units and calc-den-units are local to
636 ;; calc-explain-units, but are used by calc-explain-units-rec,
637 ;; which is called by calc-explain-units.
638 (defvar calc-num-units
)
639 (defvar calc-den-units
)
641 (defun calc-explain-units ()
644 (let ((calc-num-units nil
)
645 (calc-den-units nil
))
646 (calc-explain-units-rec (calc-top-n 1) 1)
647 (and calc-den-units
(string-match "^[^(].* .*[^)]$" calc-den-units
)
648 (setq calc-den-units
(concat "(" calc-den-units
")")))
651 (message "%s per %s" calc-num-units calc-den-units
)
652 (message "%s" calc-num-units
))
654 (message "1 per %s" calc-den-units
)
655 (message "No units in expression"))))))
657 (defun calc-explain-units-rec (expr pow
)
658 (let ((u (math-check-unit-name expr
))
660 (if (and u
(not (math-zerop pow
)))
661 (let ((name (or (nth 2 u
) (symbol-name (car u
)))))
662 (if (eq (aref name
0) ?\
*)
663 (setq name
(substring name
1)))
664 (if (string-match "[^a-zA-Zα-ωΑ-Ω0-9']" name
)
665 (if (string-match "^[a-zA-Zα-ωΑ-Ω0-9' ()]*$" name
)
666 (while (setq pos
(string-match "[ ()]" name
))
667 (setq name
(concat (substring name
0 pos
)
668 (if (eq (aref name pos
) 32) "-" "")
669 (substring name
(1+ pos
)))))
670 (setq name
(concat "(" name
")"))))
671 (or (eq (nth 1 expr
) (car u
))
672 (setq name
(concat (nth 2 (assq (aref (symbol-name
675 (if (and (string-match "[^a-zA-Zα-ωΑ-Ω0-9']" name
)
676 (not (memq (car u
) '(mHg gf
))))
679 (cond ((or (math-equal-int pow
1)
680 (math-equal-int pow -
1)))
681 ((or (math-equal-int pow
2)
682 (math-equal-int pow -
2))
683 (if (equal (nth 4 u
) '((m .
1)))
684 (setq name
(concat "Square-" name
))
685 (setq name
(concat name
"-squared"))))
686 ((or (math-equal-int pow
3)
687 (math-equal-int pow -
3))
688 (if (equal (nth 4 u
) '((m .
1)))
689 (setq name
(concat "Cubic-" name
))
690 (setq name
(concat name
"-cubed"))))
692 (setq name
(concat name
"^"
693 (math-format-number (math-abs pow
))))))
695 (setq calc-num-units
(if calc-num-units
696 (concat calc-num-units
" " name
)
698 (setq calc-den-units
(if calc-den-units
699 (concat calc-den-units
" " name
)
701 (cond ((eq (car-safe expr
) '*)
702 (calc-explain-units-rec (nth 1 expr
) pow
)
703 (calc-explain-units-rec (nth 2 expr
) pow
))
704 ((eq (car-safe expr
) '/)
705 (calc-explain-units-rec (nth 1 expr
) pow
)
706 (calc-explain-units-rec (nth 2 expr
) (- pow
)))
707 ((memq (car-safe expr
) '(neg + -
))
708 (calc-explain-units-rec (nth 1 expr
) pow
))
709 ((and (eq (car-safe expr
) '^
)
710 (math-realp (nth 2 expr
)))
711 (calc-explain-units-rec (nth 1 expr
)
712 (math-mul pow
(nth 2 expr
))))))))
714 (defun calc-simplify-units ()
717 (calc-with-default-simplification
718 (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1))))))
720 (defun calc-view-units-table (n)
722 (and n
(setq math-units-table-buffer-valid nil
))
723 (let ((win (get-buffer-window "*Units Table*")))
726 math-units-table-buffer-valid
)
728 (bury-buffer (window-buffer win
))
729 (let ((curwin (selected-window)))
731 (switch-to-buffer nil
)
732 (select-window curwin
)))
733 (math-build-units-table-buffer nil
))))
735 (defun calc-enter-units-table (n)
737 (and n
(setq math-units-table-buffer-valid nil
))
738 (math-build-units-table-buffer t
)
739 (message "%s" (substitute-command-keys "Type \\[calc] to return to the Calculator")))
741 (defun calc-define-unit (uname desc
&optional disp
)
742 (interactive "SDefine unit name: \nsDescription: \nP")
743 (if disp
(setq disp
(read-string "Display definition: ")))
745 (let ((form (calc-top-n 1))
746 (unit (assq uname math-additional-units
)))
748 (setq math-additional-units
749 (cons (setq unit
(list uname nil nil nil nil
))
750 math-additional-units
)
751 math-units-table nil
))
752 (setcar (cdr unit
) (and (not (and (eq (car-safe form
) 'var
)
753 (eq (nth 1 form
) uname
)))
754 (not (math-equal-int form
1))
755 (math-format-flat-expr form
0)))
756 (setcar (cdr (cdr unit
)) (and (not (equal desc
""))
759 (setcar (cdr (cdr (cdr (cdr unit
)))) disp
))))
760 (calc-invalidate-units-table))
762 (defun calc-undefine-unit (uname)
763 (interactive "SUndefine unit name: ")
765 (let ((unit (assq uname math-additional-units
)))
767 (if (assq uname math-standard-units
)
768 (error "\"%s\" is a predefined unit name" uname
)
769 (error "Unit name \"%s\" not found" uname
)))
770 (setq math-additional-units
(delq unit math-additional-units
)
771 math-units-table nil
)))
772 (calc-invalidate-units-table))
774 (defun calc-invalidate-units-table ()
775 (setq math-units-table nil
)
776 (let ((buf (get-buffer "*Units Table*")))
778 (with-current-buffer buf
780 (goto-char (point-min))
781 (if (looking-at "Calculator Units Table")
782 (let ((inhibit-read-only t
))
783 (insert "(Obsolete) "))))))))
785 (defun calc-get-unit-definition (uname)
786 (interactive "SGet definition for unit: ")
788 (math-build-units-table)
789 (let ((unit (assq uname math-units-table
)))
791 (error "Unit name \"%s\" not found" uname
))
792 (let ((msg (nth 2 unit
)))
794 (if (string-match "^\\*" msg
)
795 (setq msg
(substring msg
1)))
796 (setq msg
(symbol-name uname
)))
799 (calc-enter-result 0 "ugdf" (nth 1 unit
))
800 (message "Derived unit: %s" msg
))
801 (calc-enter-result 0 "ugdf" (list 'var uname
804 (symbol-name uname
)))))
805 (message "Base unit: %s" msg
))))))
807 (defun calc-permanent-units ()
811 (set-buffer (find-file-noselect (substitute-in-file-name
812 calc-settings-file
)))
813 (goto-char (point-min))
814 (if (and (search-forward ";;; Custom units stored by Calc" nil t
)
818 (search-forward "\n;;; End of custom units" nil t
)))
822 (delete-region pos
(point)))
823 (goto-char (point-max))
826 (insert ";;; Custom units stored by Calc on " (current-time-string) "\n")
827 (if math-additional-units
829 (insert "(setq math-additional-units '(\n")
830 (let ((list math-additional-units
))
832 (insert " (" (symbol-name (car (car list
))) " "
833 (if (nth 1 (car list
))
834 (if (stringp (nth 1 (car list
)))
835 (prin1-to-string (nth 1 (car list
)))
836 (prin1-to-string (math-format-flat-expr
837 (nth 1 (car list
)) 0)))
840 (prin1-to-string (nth 2 (car list
)))
842 (setq list
(cdr list
))))
844 (insert ";;; (no custom units defined)\n"))
845 (insert ";;; End of custom units\n")
849 ;; The variable math-cu-unit-list is local to math-build-units-table,
850 ;; but is used by math-compare-unit-names, which is called (indirectly)
851 ;; by math-build-units-table.
852 ;; math-cu-unit-list is also local to math-convert-units, but is used
853 ;; by math-convert-units-rec, which is called by math-convert-units.
854 (defvar math-cu-unit-list
)
856 (defun math-build-units-table ()
858 (let* ((combined-units (append math-additional-units
859 math-standard-units
))
860 (math-cu-unit-list (mapcar 'car combined-units
))
862 (message "Building units table...")
863 (setq math-units-table-buffer-valid nil
)
864 (setq tab
(mapcar (function
868 (if (stringp (nth 1 x
))
869 (let ((exp (math-read-plain-expr
871 (if (eq (car-safe exp
) 'error
)
872 (error "Format error in definition of %s in units table: %s"
879 (list (cons (car x
) 1)))
882 (let ((math-units-table tab
))
883 (mapc 'math-find-base-units tab
))
884 (message "Building units table...done")
885 (setq math-units-table tab
))))
887 ;; The variables math-fbu-base and math-fbu-entry are local to
888 ;; math-find-base-units, but are used by math-find-base-units-rec,
889 ;; which is called by math-find-base-units.
890 (defvar math-fbu-base
)
891 (defvar math-fbu-entry
)
893 (defun math-find-base-units (math-fbu-entry)
894 (if (eq (nth 4 math-fbu-entry
) 'boom
)
895 (error "Circular definition involving unit %s" (car math-fbu-entry
)))
896 (or (nth 4 math-fbu-entry
)
898 (setcar (nthcdr 4 math-fbu-entry
) 'boom
)
899 (math-find-base-units-rec (nth 1 math-fbu-entry
) 1)
901 (error "Dimensionless definition for unit %s" (car math-fbu-entry
)))
902 (while (eq (cdr (car math-fbu-base
)) 0)
903 (setq math-fbu-base
(cdr math-fbu-base
)))
904 (let ((b math-fbu-base
))
906 (if (eq (cdr (car (cdr b
))) 0)
907 (setcdr b
(cdr (cdr b
)))
909 (setq math-fbu-base
(sort math-fbu-base
'math-compare-unit-names
))
910 (setcar (nthcdr 4 math-fbu-entry
) math-fbu-base
)
913 (defun math-compare-unit-names (a b
)
914 (memq (car b
) (cdr (memq (car a
) math-cu-unit-list
))))
916 (defun math-find-base-units-rec (expr pow
)
917 (let ((u (math-check-unit-name expr
)))
919 (let ((ulist (math-find-base-units u
)))
921 (let ((p (* (cdr (car ulist
)) pow
))
922 (old (assq (car (car ulist
)) math-fbu-base
)))
924 (setcdr old
(+ (cdr old
) p
))
926 (cons (cons (car (car ulist
)) p
) math-fbu-base
))))
927 (setq ulist
(cdr ulist
)))))
928 ((math-scalarp expr
))
929 ((and (eq (car expr
) '^
)
930 (integerp (nth 2 expr
)))
931 (math-find-base-units-rec (nth 1 expr
) (* pow
(nth 2 expr
))))
933 (math-find-base-units-rec (nth 1 expr
) pow
)
934 (math-find-base-units-rec (nth 2 expr
) pow
))
936 (math-find-base-units-rec (nth 1 expr
) pow
)
937 (math-find-base-units-rec (nth 2 expr
) (- pow
)))
938 ((eq (car expr
) 'neg
)
939 (math-find-base-units-rec (nth 1 expr
) pow
))
941 (math-find-base-units-rec (nth 1 expr
) pow
))
942 ((eq (car expr
) 'var
)
943 (or (eq (nth 1 expr
) 'pi
)
944 (error "Unknown name %s in defining expression for unit %s"
945 (nth 1 expr
) (car math-fbu-entry
))))
946 ((equal expr
'(calcFunc-ln 10)))
947 (t (error "Malformed defining expression for unit %s" (car math-fbu-entry
))))))
950 (defun math-units-in-expr-p (expr sub-exprs
)
952 (if (eq (car expr
) 'var
)
953 (math-check-unit-name expr
)
954 (if (eq (car expr
) 'neg
)
955 (math-units-in-expr-p (nth 1 expr
) sub-exprs
)
957 (memq (car expr
) '(* / ^
)))
958 (or (math-units-in-expr-p (nth 1 expr
) sub-exprs
)
959 (math-units-in-expr-p (nth 2 expr
) sub-exprs
)))))))
961 (defun math-only-units-in-expr-p (expr)
963 (if (eq (car expr
) 'var
)
964 (math-check-unit-name expr
)
965 (if (memq (car expr
) '(* /))
966 (and (math-only-units-in-expr-p (nth 1 expr
))
967 (math-only-units-in-expr-p (nth 2 expr
)))
968 (and (eq (car expr
) '^
)
969 (and (math-only-units-in-expr-p (nth 1 expr
))
970 (math-realp (nth 2 expr
))))))))
972 (defun math-single-units-in-expr-p (expr)
973 (cond ((math-scalarp expr
) nil
)
974 ((eq (car expr
) 'var
)
975 (math-check-unit-name expr
))
976 ((eq (car expr
) 'neg
)
977 (math-single-units-in-expr-p (nth 1 expr
)))
979 (let ((u1 (math-single-units-in-expr-p (nth 1 expr
)))
980 (u2 (math-single-units-in-expr-p (nth 2 expr
))))
981 (or (and u1 u2
'wrong
)
985 (if (math-units-in-expr-p (nth 2 expr
) nil
)
987 (math-single-units-in-expr-p (nth 1 expr
))))
990 (defun math-consistent-units-p (expr newunits
)
991 "Non-nil if EXPR and NEWUNITS have consistent units."
993 (and (eq (car-safe newunits
) 'var
)
994 (assq (nth 1 newunits
) math-standard-units-systems
))
995 (math-numberp (math-get-units (math-to-standard-units (list '/ expr newunits
) nil
)))))
997 (defun math-check-unit-consistency (expr units
)
998 "Give an error if EXPR and UNITS do not have consistent units."
999 (unless (math-consistent-units-p expr units
)
1000 (error "New units (%s) are inconsistent with current units (%s)"
1001 (math-format-value units
)
1002 (math-format-value (math-get-units expr
)))))
1004 (defun math-check-unit-name (v)
1005 (and (eq (car-safe v
) 'var
)
1006 (or (assq (nth 1 v
) (or math-units-table
(math-build-units-table)))
1007 (let ((name (symbol-name (nth 1 v
))))
1008 (and (> (length name
) 1)
1009 (assq (aref name
0) math-unit-prefixes
)
1010 (or (assq (intern (substring name
1)) math-units-table
)
1011 (and (eq (aref name
0) ?M
)
1013 (eq (aref name
1) ?e
)
1014 (eq (aref name
2) ?g
)
1015 (assq (intern (substring name
3))
1016 math-units-table
))))))))
1018 ;; The variable math-which-standard is local to math-to-standard-units,
1019 ;; but is used by math-to-standard-rec, which is called by
1020 ;; math-to-standard-units.
1021 (defvar math-which-standard
)
1023 (defun math-to-standard-units (expr math-which-standard
)
1024 (math-to-standard-rec expr
))
1026 (defun math-to-standard-rec (expr)
1027 (if (eq (car-safe expr
) 'var
)
1028 (let ((u (math-check-unit-name expr
))
1029 (base (nth 1 expr
)))
1033 (setq expr
(math-to-standard-rec (nth 1 u
)))
1034 (let ((st (assq (car u
) math-which-standard
)))
1036 (setq expr
(nth 1 st
))
1037 (setq expr
(list 'var
(car u
)
1038 (intern (concat "var-"
1044 (nth 1 (assq (aref (symbol-name base
) 0)
1045 math-unit-prefixes
))
1053 (and (eq (car-safe expr
) 'calcFunc-subscr
)
1054 (eq (car-safe (nth 1 expr
)) 'var
)))
1057 (mapcar 'math-to-standard-rec
(cdr expr
))))))
1059 (defun math-apply-units (expr units ulist
&optional pure
)
1060 (setq expr
(math-simplify-units expr
))
1064 (or (math-numberp expr
)
1065 (error "Incompatible units"))
1067 (setq value
(math-div expr
(nth 1 (car ulist
)))
1068 value
(math-floor (let ((calc-internal-prec
1069 (1- calc-internal-prec
)))
1070 (math-normalize value
)))
1071 new
(math-add new
(math-mul value
(car (car ulist
))))
1072 expr
(math-sub expr
(math-mul value
(nth 1 (car ulist
))))
1074 (math-add new
(math-mul (math-div expr
(nth 1 (car ulist
)))
1075 (car (car ulist
)))))
1078 (math-simplify-units (list '* expr units
)))))
1080 (defvar math-decompose-units-cache nil
)
1081 (defun math-decompose-units (units)
1082 (let ((u (math-check-unit-name units
)))
1083 (and u
(eq (car-safe (nth 1 u
)) '+)
1084 (setq units
(nth 1 u
))))
1085 (setq units
(calcFunc-expand units
))
1086 (and (eq (car-safe units
) '+)
1087 (let ((entry (list units calc-internal-prec calc-prefer-frac
)))
1088 (or (equal entry
(car math-decompose-units-cache
))
1092 (while (eq (car-safe utemp
) '+)
1093 (setq ulist
(cons (math-decompose-unit-part (nth 2 utemp
))
1095 utemp
(nth 1 utemp
)))
1096 (setq ulist
(cons (math-decompose-unit-part utemp
) ulist
)
1098 (while (setq utemp
(cdr utemp
))
1099 (unless (equal (nth 2 (car utemp
)) (nth 2 (car ulist
)))
1100 (error "Inconsistent units in sum")))
1101 (setq math-decompose-units-cache
1106 (not (Math-lessp (nth 1 x
)
1108 (cdr math-decompose-units-cache
))))
1110 (defun math-decompose-unit-part (unit)
1112 (math-is-multiple (math-simplify-units (math-to-standard-units
1116 ;; The variable math-fcu-u is local to math-find-compatible-unit,
1117 ;; but is used by math-find-compatible-rec which is called by
1118 ;; math-find-compatible-unit.
1121 (defun math-find-compatible-unit (expr unit
)
1122 (let ((math-fcu-u (math-check-unit-name unit
)))
1124 (math-find-compatible-unit-rec expr
1))))
1126 (defun math-find-compatible-unit-rec (expr pow
)
1127 (cond ((eq (car-safe expr
) '*)
1128 (or (math-find-compatible-unit-rec (nth 1 expr
) pow
)
1129 (math-find-compatible-unit-rec (nth 2 expr
) pow
)))
1130 ((eq (car-safe expr
) '/)
1131 (or (math-find-compatible-unit-rec (nth 1 expr
) pow
)
1132 (math-find-compatible-unit-rec (nth 2 expr
) (- pow
))))
1133 ((eq (car-safe expr
) 'neg
)
1134 (math-find-compatible-unit-rec (nth 1 expr
) pow
))
1135 ((and (eq (car-safe expr
) '^
)
1136 (integerp (nth 2 expr
)))
1137 (math-find-compatible-unit-rec (nth 1 expr
) (* pow
(nth 2 expr
))))
1139 (let ((u2 (math-check-unit-name expr
)))
1140 (if (equal (nth 4 math-fcu-u
) (nth 4 u2
))
1141 (cons expr pow
))))))
1143 ;; The variables math-cu-new-units and math-cu-pure are local to
1144 ;; math-convert-units, but are used by math-convert-units-rec,
1145 ;; which is called by math-convert-units.
1146 (defvar math-cu-new-units
)
1147 (defvar math-cu-pure
)
1149 (defun math-convert-units (expr math-cu-new-units
&optional math-cu-pure
)
1150 (if (eq (car-safe math-cu-new-units
) 'var
)
1151 (let ((unew (assq (nth 1 math-cu-new-units
)
1152 (math-build-units-table))))
1153 (if (eq (car-safe (nth 1 unew
)) '+)
1154 (setq math-cu-new-units
(nth 1 unew
)))))
1155 (math-with-extra-prec 2
1156 (let ((compat (and (not math-cu-pure
)
1157 (math-find-compatible-unit expr math-cu-new-units
)))
1158 (math-cu-unit-list nil
)
1159 (math-combining-units nil
))
1161 (math-simplify-units
1162 (math-mul (math-mul (math-simplify-units
1163 (math-div expr
(math-pow (car compat
)
1165 (math-pow math-cu-new-units
(cdr compat
)))
1166 (math-simplify-units
1167 (math-to-standard-units
1168 (math-pow (math-div (car compat
) math-cu-new-units
)
1171 (when (setq math-cu-unit-list
(math-decompose-units math-cu-new-units
))
1172 (setq math-cu-new-units
(nth 2 (car math-cu-unit-list
))))
1173 (when (eq (car-safe expr
) '+)
1174 (setq expr
(math-simplify-units expr
)))
1175 (if (math-units-in-expr-p expr t
)
1176 (math-convert-units-rec expr
)
1177 (math-apply-units (math-to-standard-units
1178 (list '/ expr math-cu-new-units
) nil
)
1179 math-cu-new-units math-cu-unit-list math-cu-pure
))))))
1181 (defun math-convert-units-rec (expr)
1182 (if (math-units-in-expr-p expr nil
)
1183 (math-apply-units (math-to-standard-units
1184 (list '/ expr math-cu-new-units
) nil
)
1185 math-cu-new-units math-cu-unit-list math-cu-pure
)
1186 (if (Math-primp expr
)
1189 (mapcar 'math-convert-units-rec
(cdr expr
))))))
1191 (defun math-convert-temperature (expr old new
&optional pure
)
1192 (let* ((units (math-single-units-in-expr-p expr
))
1194 (if (or (null units
)
1195 (equal (nth 1 old
) (car units
)))
1196 (math-check-unit-name old
)
1197 (error "Inconsistent temperature units"))
1199 (unew (math-check-unit-name new
)))
1200 (unless (and (consp unew
) (nth 3 unew
))
1201 (error "Not a valid temperature unit"))
1202 (unless (and (consp uold
) (nth 3 uold
))
1203 (error "Not a pure temperature expression"))
1204 (let ((v (car uold
)))
1205 (setq expr
(list '/ expr
(list 'var v
1206 (intern (concat "var-"
1207 (symbol-name v
)))))))
1208 (or (eq (nth 3 uold
) (nth 3 unew
))
1209 (cond ((eq (nth 3 uold
) 'K
)
1210 (setq expr
(list '- expr
'(/ 27315 100)))
1211 (if (eq (nth 3 unew
) 'F
)
1212 (setq expr
(list '+ (list '* expr
'(/ 9 5)) 32))))
1213 ((eq (nth 3 uold
) 'C
)
1214 (if (eq (nth 3 unew
) 'F
)
1215 (setq expr
(list '+ (list '* expr
'(/ 9 5)) 32))
1216 (setq expr
(list '+ expr
'(/ 27315 100)))))
1218 (setq expr
(list '* (list '- expr
32) '(/ 5 9)))
1219 (if (eq (nth 3 unew
) 'K
)
1220 (setq expr
(list '+ expr
'(/ 27315 100)))))))
1223 (list '* expr new
))))
1227 (defun math-simplify-units (a)
1228 (let ((math-simplifying-units t
)
1229 (calc-matrix-mode 'scalar
))
1231 (defalias 'calcFunc-usimplify
'math-simplify-units
)
1233 ;; The function created by math-defsimplify uses the variable
1234 ;; math-simplify-expr, and so is used by functions in math-defsimplify
1235 (defvar math-simplify-expr
)
1237 (math-defsimplify (+ -
)
1238 (and math-simplifying-units
1239 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1240 (let* ((units (math-extract-units (nth 1 math-simplify-expr
)))
1241 (ratio (math-simplify (math-to-standard-units
1242 (list '/ (nth 2 math-simplify-expr
) units
) nil
))))
1243 (if (math-units-in-expr-p ratio nil
)
1245 (calc-record-why "*Inconsistent units" math-simplify-expr
)
1247 (list '* (math-add (math-remove-units (nth 1 math-simplify-expr
))
1248 (if (eq (car math-simplify-expr
) '-
)
1249 (math-neg ratio
) ratio
))
1253 (math-simplify-units-prod))
1255 (defun math-simplify-units-prod ()
1256 (and math-simplifying-units
1257 calc-autorange-units
1258 (Math-realp (nth 1 math-simplify-expr
))
1259 (let* ((num (math-float (nth 1 math-simplify-expr
)))
1260 (xpon (calcFunc-xpon num
))
1261 (unitp (cdr (cdr math-simplify-expr
)))
1263 (pow (if (eq (car math-simplify-expr
) '*) 1 -
1))
1265 (and (eq (car-safe unit
) '*)
1266 (setq unitp
(cdr unit
)
1268 (and (eq (car-safe unit
) '^
)
1269 (integerp (nth 2 unit
))
1270 (setq pow
(* pow
(nth 2 unit
))
1273 (and (setq u
(math-check-unit-name unit
))
1276 (>= xpon
(if (eq (car u
) 'm
) 1 3)))
1279 (reverse math-unit-prefixes
)
1280 math-unit-prefixes
))
1283 (or (eq (car u
) (nth 1 unit
))
1288 math-unit-prefixes
))))))
1289 (setq xpon
(+ xpon uxpon
))
1291 (or (memq (car (car p
)) '(?d ?D ?h ?H
))
1292 (and (eq (car (car p
)) ?c
)
1293 (not (eq (car u
) 'm
)))
1294 (< xpon
(setq pxpon
(* (nth 2 (nth 1 (car p
)))
1297 (setq pname
(math-build-var-name
1298 (if (eq (car (car p
)) 0)
1300 (concat (char-to-string
1304 (and (/= (car (car p
)) 0)
1306 math-units-table
)))))
1310 (or (not (eq p pref
))
1311 (< xpon
(+ pxpon
(* (math-abs pow
) 3))))
1313 (setcar (cdr math-simplify-expr
)
1314 (let ((calc-prefer-frac nil
))
1315 (calcFunc-scf (nth 1 math-simplify-expr
)
1317 (setcar unitp pname
)
1318 math-simplify-expr
)))))))
1320 (defvar math-try-cancel-units
)
1323 (and math-simplifying-units
1324 (let ((np (cdr math-simplify-expr
))
1325 (math-try-cancel-units 0)
1327 (setq n
(if (eq (car-safe (nth 2 math-simplify-expr
)) '*)
1328 (cdr (nth 2 math-simplify-expr
))
1329 (nthcdr 2 math-simplify-expr
)))
1330 (if (math-realp (car n
))
1332 (setcar (cdr math-simplify-expr
) (math-mul (nth 1 math-simplify-expr
)
1333 (let ((calc-prefer-frac nil
))
1334 (math-div 1 (car n
)))))
1336 (while (eq (car-safe (setq n
(car np
))) '*)
1337 (math-simplify-units-divisor (cdr n
) (cdr (cdr math-simplify-expr
)))
1338 (setq np
(cdr (cdr n
))))
1339 (math-simplify-units-divisor np
(cdr (cdr math-simplify-expr
)))
1340 (if (eq math-try-cancel-units
0)
1341 (let* ((math-simplifying-units nil
)
1342 (base (math-simplify
1343 (math-to-standard-units math-simplify-expr nil
))))
1344 (if (Math-numberp base
)
1345 (setq math-simplify-expr base
))))
1346 (if (eq (car-safe math-simplify-expr
) '/)
1347 (math-simplify-units-prod))
1348 math-simplify-expr
)))
1350 (defun math-simplify-units-divisor (np dp
)
1353 (while (eq (car-safe (setq d
(car dp
))) '*)
1354 (when (setq temp
(math-simplify-units-quotient n
(nth 1 d
)))
1355 (setcar np
(setq n temp
))
1357 (setq dp
(cdr (cdr d
))))
1358 (when (setq temp
(math-simplify-units-quotient n d
))
1359 (setcar np
(setq n temp
))
1362 ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.
1363 (defun math-simplify-units-quotient (n d
)
1366 (when (and (eq (car-safe n
) '^
)
1367 (integerp (nth 2 n
)))
1368 (setq pow1
(nth 2 n
) n
(nth 1 n
)))
1369 (when (and (eq (car-safe d
) '^
)
1370 (integerp (nth 2 d
)))
1371 (setq pow2
(nth 2 d
) d
(nth 1 d
)))
1372 (let ((un (math-check-unit-name n
))
1373 (ud (math-check-unit-name d
)))
1375 (if (and (equal (nth 4 un
) (nth 4 ud
))
1378 (math-to-standard-units (list '/ n d
) nil
)
1379 (list '^
(math-to-standard-units (list '/ n d
) nil
) pow1
))
1386 (and (eq (car (car un
)) (car (car ud1
)))
1387 (setq math-try-cancel-units
1388 (+ math-try-cancel-units
1389 (- (* (cdr (car un
)) pow1
)
1390 (* (cdr (car ud
)) pow2
)))))
1391 (setq ud1
(cdr ud1
)))
1396 (and math-simplifying-units
1397 (math-realp (nth 2 math-simplify-expr
))
1398 (if (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1399 (list (car (nth 1 math-simplify-expr
))
1400 (list '^
(nth 1 (nth 1 math-simplify-expr
))
1401 (nth 2 math-simplify-expr
))
1402 (list '^
(nth 2 (nth 1 math-simplify-expr
))
1403 (nth 2 math-simplify-expr
)))
1404 (math-simplify-units-pow (nth 1 math-simplify-expr
)
1405 (nth 2 math-simplify-expr
)))))
1407 (math-defsimplify calcFunc-sqrt
1408 (and math-simplifying-units
1409 (if (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1410 (list (car (nth 1 math-simplify-expr
))
1411 (list 'calcFunc-sqrt
(nth 1 (nth 1 math-simplify-expr
)))
1412 (list 'calcFunc-sqrt
(nth 2 (nth 1 math-simplify-expr
))))
1413 (math-simplify-units-pow (nth 1 math-simplify-expr
) '(frac 1 2)))))
1415 (math-defsimplify (calcFunc-floor
1425 (and math-simplifying-units
1426 (= (length math-simplify-expr
) 2)
1427 (if (math-only-units-in-expr-p (nth 1 math-simplify-expr
))
1428 (nth 1 math-simplify-expr
)
1429 (if (and (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1430 (or (math-only-units-in-expr-p
1431 (nth 1 (nth 1 math-simplify-expr
)))
1432 (math-only-units-in-expr-p
1433 (nth 2 (nth 1 math-simplify-expr
)))))
1434 (list (car (nth 1 math-simplify-expr
))
1435 (cons (car math-simplify-expr
)
1436 (cons (nth 1 (nth 1 math-simplify-expr
))
1437 (cdr (cdr math-simplify-expr
))))
1438 (cons (car math-simplify-expr
)
1439 (cons (nth 2 (nth 1 math-simplify-expr
))
1440 (cdr (cdr math-simplify-expr
)))))))))
1442 (defun math-simplify-units-pow (a pow
)
1443 (if (and (eq (car-safe a
) '^
)
1444 (math-check-unit-name (nth 1 a
))
1445 (math-realp (nth 2 a
)))
1446 (list '^
(nth 1 a
) (math-mul pow
(nth 2 a
)))
1447 (let* ((u (math-check-unit-name a
))
1448 (pf (math-to-simple-fraction pow
))
1449 (d (and (eq (car-safe pf
) 'frac
) (nth 2 pf
))))
1451 (math-units-are-multiple u d
)
1452 (list '^
(math-to-standard-units a nil
) pow
)))))
1455 (defun math-units-are-multiple (u n
)
1457 (while (and u
(= (%
(cdr (car u
)) n
) 0))
1461 (math-defsimplify calcFunc-sin
1462 (and math-simplifying-units
1463 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1464 (let ((rad (math-simplify-units
1466 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1467 (calc-angle-mode 'rad
))
1468 (and (eq (car-safe rad
) '*)
1469 (math-realp (nth 1 rad
))
1470 (eq (car-safe (nth 2 rad
)) 'var
)
1471 (eq (nth 1 (nth 2 rad
)) 'rad
)
1472 (list 'calcFunc-sin
(nth 1 rad
))))))
1474 (math-defsimplify calcFunc-cos
1475 (and math-simplifying-units
1476 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1477 (let ((rad (math-simplify-units
1479 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1480 (calc-angle-mode 'rad
))
1481 (and (eq (car-safe rad
) '*)
1482 (math-realp (nth 1 rad
))
1483 (eq (car-safe (nth 2 rad
)) 'var
)
1484 (eq (nth 1 (nth 2 rad
)) 'rad
)
1485 (list 'calcFunc-cos
(nth 1 rad
))))))
1487 (math-defsimplify calcFunc-tan
1488 (and math-simplifying-units
1489 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1490 (let ((rad (math-simplify-units
1492 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1493 (calc-angle-mode 'rad
))
1494 (and (eq (car-safe rad
) '*)
1495 (math-realp (nth 1 rad
))
1496 (eq (car-safe (nth 2 rad
)) 'var
)
1497 (eq (nth 1 (nth 2 rad
)) 'rad
)
1498 (list 'calcFunc-tan
(nth 1 rad
))))))
1500 (math-defsimplify calcFunc-sec
1501 (and math-simplifying-units
1502 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1503 (let ((rad (math-simplify-units
1505 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1506 (calc-angle-mode 'rad
))
1507 (and (eq (car-safe rad
) '*)
1508 (math-realp (nth 1 rad
))
1509 (eq (car-safe (nth 2 rad
)) 'var
)
1510 (eq (nth 1 (nth 2 rad
)) 'rad
)
1511 (list 'calcFunc-sec
(nth 1 rad
))))))
1513 (math-defsimplify calcFunc-csc
1514 (and math-simplifying-units
1515 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1516 (let ((rad (math-simplify-units
1518 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1519 (calc-angle-mode 'rad
))
1520 (and (eq (car-safe rad
) '*)
1521 (math-realp (nth 1 rad
))
1522 (eq (car-safe (nth 2 rad
)) 'var
)
1523 (eq (nth 1 (nth 2 rad
)) 'rad
)
1524 (list 'calcFunc-csc
(nth 1 rad
))))))
1526 (math-defsimplify calcFunc-cot
1527 (and math-simplifying-units
1528 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1529 (let ((rad (math-simplify-units
1531 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1532 (calc-angle-mode 'rad
))
1533 (and (eq (car-safe rad
) '*)
1534 (math-realp (nth 1 rad
))
1535 (eq (car-safe (nth 2 rad
)) 'var
)
1536 (eq (nth 1 (nth 2 rad
)) 'rad
)
1537 (list 'calcFunc-cot
(nth 1 rad
))))))
1540 (defun math-remove-units (expr)
1541 (if (math-check-unit-name expr
)
1543 (if (Math-primp expr
)
1546 (mapcar 'math-remove-units
(cdr expr
))))))
1548 (defun math-extract-units (expr)
1550 ((memq (car-safe expr
) '(* /))
1552 (mapcar 'math-extract-units
(cdr expr
))))
1553 ((eq (car-safe expr
) 'neg
)
1554 (math-extract-units (nth 1 expr
)))
1555 ((eq (car-safe expr
) '^
)
1556 (list '^
(math-extract-units (nth 1 expr
)) (nth 2 expr
)))
1557 ((math-check-unit-name expr
) expr
)
1560 (defun math-build-units-table-buffer (enter-buffer)
1561 (if (not (and math-units-table math-units-table-buffer-valid
1562 (get-buffer "*Units Table*")))
1563 (let ((buf (get-buffer-create "*Units Table*"))
1564 (uptr (math-build-units-table))
1565 (calc-language (if (eq calc-language
'big
) nil calc-language
))
1566 (calc-float-format '(float 0))
1567 (calc-group-digits nil
)
1568 (calc-number-radix 10)
1569 (calc-twos-complement-mode nil
)
1570 (calc-point-char ".")
1574 (message "Formatting units table...")
1576 (let ((inhibit-read-only t
))
1578 (insert "Calculator Units Table:\n\n")
1579 (insert "(All definitions are exact unless marked with an asterisk (*).)\n\n")
1580 (insert "Unit Type Definition Description\n\n")
1584 (when (eq (car u
) 'm
)
1586 (setq shadowed
(and std
(assq (car u
) math-additional-units
)))
1589 (eq (aref name
0) ?\
*))
1590 (unless (eq uptr math-units-table
)
1592 (setq name
(substring name
1)))
1594 (and shadowed
(insert "("))
1595 (insert (symbol-name (car u
)))
1596 (and shadowed
(insert ")"))
1600 (insert (symbol-name (nth 3 u
))))
1606 (and shadowed
(insert "("))
1610 (insert (math-format-value (nth 1 u
) 80))
1611 (insert (symbol-name (car u
)))))
1612 (and shadowed
(insert ")"))
1618 (insert " (redefined above)")
1620 (insert " (base unit)")))
1622 (setq uptr
(cdr uptr
)))
1623 (insert "\n\nUnit Prefix Table:\n\n")
1624 (setq uptr math-unit-prefixes
)
1627 (insert " " (char-to-string (car u
)))
1628 (if (equal (nth 1 u
) (nth 1 (nth 1 uptr
)))
1629 (insert " " (char-to-string (car (car (setq uptr
(cdr uptr
)))))
1632 (insert "10^" (int-to-string (nth 2 (nth 1 u
))))
1634 (insert " " (nth 2 u
) "\n")
1635 (while (eq (car (car (setq uptr
(cdr uptr
)))) 0)))
1640 "(**) When in TeX or LaTeX display mode, the TeX specific unit\n"
1641 "names will not use the `tex' prefix; the unit name for a\n"
1642 "TeX point will be `pt' instead of `texpt', for example.\n"
1643 "To avoid conflicts, the unit names for pint and parsec will\n"
1644 "be `pint' and `parsec' instead of `pt' and `pc'."))))
1646 (message "Formatting units table...done"))
1647 (setq math-units-table-buffer-valid t
)
1648 (let ((oldbuf (current-buffer)))
1650 (goto-char (point-min))
1651 (set-buffer oldbuf
))
1654 (display-buffer buf
)))
1656 (pop-to-buffer (get-buffer "*Units Table*"))
1657 (display-buffer (get-buffer "*Units Table*")))))
1659 ;;; Logarithmic units functions
1661 (defvar math-logunits
'((var dB var-dB
)
1664 (defun math-conditional-apply (fn &rest args
)
1665 "Evaluate f(args) unless in symbolic mode.
1666 In symbolic mode, return the list (fn args)."
1667 (if calc-symbolic-mode
1671 (defun math-conditional-pow (a b
)
1672 "Evaluate a^b unless in symbolic mode.
1673 In symbolic mode, return the list (^ a b)."
1674 (if calc-symbolic-mode
1678 (defun math-extract-logunits (expr)
1679 (if (memq (car-safe expr
) '(* /))
1681 (mapcar 'math-extract-logunits
(cdr expr
)))
1682 (if (memq (car-safe expr
) '(^
))
1683 (list '^
(math-extract-logunits (nth 1 expr
)) (nth 2 expr
))
1684 (if (member expr math-logunits
) expr
1))))
1686 (defun math-logunits-add (a b neg power
)
1687 (let ((aunit (math-simplify (math-extract-logunits a
))))
1688 (if (not (eq (car-safe aunit
) 'var
))
1689 (calc-record-why "*Improper logarithmic unit" aunit
)
1690 (let* ((units (math-extract-units a
))
1691 (acoeff (math-simplify (math-remove-units a
)))
1692 (bcoeff (math-simplify (math-to-standard-units
1693 (list '/ b units
) nil
))))
1694 (if (math-units-in-expr-p bcoeff nil
)
1695 (calc-record-why "*Inconsistent units" nil
)
1697 (or (math-lessp acoeff bcoeff
)
1698 (math-equal acoeff bcoeff
)))
1699 (calc-record-why "*Improper coefficients" nil
)
1701 (if (equal aunit
'(var dB var-dB
))
1702 (let ((coef (if power
10 20)))
1704 (math-conditional-apply 'calcFunc-log10
1707 (math-conditional-pow 10 (math-div acoeff coef
))
1708 (math-conditional-pow 10 (math-div bcoeff coef
)))
1710 (math-conditional-pow 10 (math-div acoeff coef
))
1711 (math-conditional-pow 10 (math-div bcoeff coef
)))))))
1712 (let ((coef (if power
2 1)))
1714 (math-conditional-apply 'calcFunc-ln
1717 (math-conditional-apply 'calcFunc-exp
(math-mul coef acoeff
))
1718 (math-conditional-apply 'calcFunc-exp
(math-mul coef bcoeff
)))
1720 (math-conditional-apply 'calcFunc-exp
(math-mul coef acoeff
))
1721 (math-conditional-apply 'calcFunc-exp
(math-mul coef bcoeff
)))))
1725 (defun calcFunc-lufadd (a b
)
1726 (math-logunits-add a b nil nil
))
1728 (defun calcFunc-lupadd (a b
)
1729 (math-logunits-add a b nil t
))
1731 (defun calcFunc-lufsub (a b
)
1732 (math-logunits-add a b t nil
))
1734 (defun calcFunc-lupsub (a b
)
1735 (math-logunits-add a b t t
))
1737 (defun calc-lu-plus (arg)
1740 (if (calc-is-inverse)
1741 (if (calc-is-hyperbolic)
1742 (calc-binary-op "lu-" 'calcFunc-lufsub arg
)
1743 (calc-binary-op "lu-" 'calcFunc-lupsub arg
))
1744 (if (calc-is-hyperbolic)
1745 (calc-binary-op "lu+" 'calcFunc-lufadd arg
)
1746 (calc-binary-op "lu+" 'calcFunc-lupadd arg
)))))
1748 (defun calc-lu-minus (arg)
1751 (if (calc-is-inverse)
1752 (if (calc-is-hyperbolic)
1753 (calc-binary-op "lu+" 'calcFunc-lufadd arg
)
1754 (calc-binary-op "lu+" 'calcFunc-lupadd arg
))
1755 (if (calc-is-hyperbolic)
1756 (calc-binary-op "lu-" 'calcFunc-lufsub arg
)
1757 (calc-binary-op "lu-" 'calcFunc-lupsub arg
)))))
1759 (defun math-logunits-mul (a b power
)
1760 (let (logunit coef units number
)
1763 (setq logunit
(math-simplify (math-extract-logunits a
)))
1764 (eq (car-safe logunit
) 'var
)
1765 (eq (math-simplify (math-extract-units b
)) 1))
1766 (setq coef
(math-simplify (math-remove-units a
))
1767 units
(math-extract-units a
)
1770 (setq logunit
(math-simplify (math-extract-logunits b
)))
1771 (eq (car-safe logunit
) 'var
)
1772 (eq (math-simplify (math-extract-units a
)) 1))
1773 (setq coef
(math-simplify (math-remove-units b
))
1774 units
(math-extract-units b
)
1776 (t (setq logunit nil
)))
1779 ((equal logunit
'(var dB var-dB
))
1784 (math-mul (if power
10 20)
1785 (math-conditional-apply 'calcFunc-log10 number
)))
1792 (math-div (math-conditional-apply 'calcFunc-ln number
) (if power
2 1)))
1794 (calc-record-why "*Improper units" nil
))))
1796 (defun math-logunits-divide (a b power
)
1797 (let ((logunit (math-simplify (math-extract-logunits a
))))
1798 (if (not (eq (car-safe logunit
) 'var
))
1799 (calc-record-why "*Improper logarithmic unit" logunit
)
1800 (if (math-units-in-expr-p b nil
)
1801 (calc-record-why "*Improper units quantity" b
)
1802 (let* ((units (math-extract-units a
))
1803 (coef (math-simplify (math-remove-units a
))))
1805 ((equal logunit
'(var dB var-dB
))
1810 (math-mul (if power
10 20)
1811 (math-conditional-apply 'calcFunc-log10 b
)))
1818 (math-div (math-conditional-apply 'calcFunc-ln b
) (if power
2 1)))
1821 (defun calcFunc-lufmul (a b
)
1822 (math-logunits-mul a b nil
))
1824 (defun calcFunc-lupmul (a b
)
1825 (math-logunits-mul a b t
))
1827 (defun calc-lu-times (arg)
1830 (if (calc-is-inverse)
1831 (if (calc-is-hyperbolic)
1832 (calc-binary-op "lu/" 'calcFunc-lufdiv arg
)
1833 (calc-binary-op "lu/" 'calcFunc-lupdiv arg
))
1834 (if (calc-is-hyperbolic)
1835 (calc-binary-op "lu*" 'calcFunc-lufmul arg
)
1836 (calc-binary-op "lu*" 'calcFunc-lupmul arg
)))))
1838 (defun calcFunc-lufdiv (a b
)
1839 (math-logunits-divide a b nil
))
1841 (defun calcFunc-lupdiv (a b
)
1842 (math-logunits-divide a b t
))
1844 (defun calc-lu-divide (arg)
1847 (if (calc-is-inverse)
1848 (if (calc-is-hyperbolic)
1849 (calc-binary-op "lu*" 'calcFunc-lufmul arg
)
1850 (calc-binary-op "lu*" 'calcFunc-lupmul arg
))
1851 (if (calc-is-hyperbolic)
1852 (calc-binary-op "lu/" 'calcFunc-lufdiv arg
)
1853 (calc-binary-op "lu/" 'calcFunc-lupdiv arg
)))))
1855 (defun math-logunits-quant (val ref power
)
1856 (let* ((units (math-simplify (math-extract-units val
)))
1857 (lunit (math-simplify (math-extract-logunits units
))))
1858 (if (not (eq (car-safe lunit
) 'var
))
1859 (calc-record-why "*Improper logarithmic unit" lunit
)
1860 (let ((runits (math-simplify (math-div units lunit
)))
1861 (coeff (math-simplify (math-div val units
))))
1863 (if (equal lunit
'(var dB var-dB
))
1866 (math-conditional-pow
1873 (math-conditional-apply 'calcFunc-exp
1879 (defvar calc-lu-field-reference
)
1880 (defvar calc-lu-power-reference
)
1882 (defun calcFunc-lufquant (val &optional ref
)
1884 (setq ref
(math-read-expr calc-lu-field-reference
)))
1885 (math-logunits-quant val ref nil
))
1887 (defun calcFunc-lupquant (val &optional ref
)
1889 (setq ref
(math-read-expr calc-lu-power-reference
)))
1890 (math-logunits-quant val ref t
))
1892 (defun calc-lu-quant (arg)
1895 (if (calc-is-hyperbolic)
1896 (if (calc-is-option)
1897 (calc-binary-op "lupq" 'calcFunc-lufquant arg
)
1898 (calc-unary-op "lupq" 'calcFunc-lufquant arg
))
1899 (if (calc-is-option)
1900 (calc-binary-op "lufq" 'calcFunc-lupquant arg
)
1901 (calc-unary-op "lufq" 'calcFunc-lupquant arg
)))))
1903 (defun math-logunits-level (val ref db power
)
1904 "Compute the value of VAL in decibels or nepers."
1905 (let* ((ratio (math-simplify-units (math-div val ref
)))
1906 (ratiou (math-simplify-units (math-remove-units ratio
)))
1907 (units (math-simplify (math-extract-units ratio
))))
1911 (math-mul (if power
10 20)
1912 (math-conditional-apply 'calcFunc-log10 ratiou
))
1915 (math-div (math-conditional-apply 'calcFunc-ln ratiou
) (if power
2 1))
1919 (defun calcFunc-dbfield (val &optional ref
)
1921 (setq ref
(math-read-expr calc-lu-field-reference
)))
1922 (math-logunits-level val ref t nil
))
1924 (defun calcFunc-dbpower (val &optional ref
)
1926 (setq ref
(math-read-expr calc-lu-power-reference
)))
1927 (math-logunits-level val ref t t
))
1929 (defun calcFunc-npfield (val &optional ref
)
1931 (setq ref
(math-read-expr calc-lu-field-reference
)))
1932 (math-logunits-level val ref nil nil
))
1934 (defun calcFunc-nppower (val &optional ref
)
1936 (setq ref
(math-read-expr calc-lu-power-reference
)))
1937 (math-logunits-level val ref nil t
))
1939 (defun calc-db (arg)
1942 (if (calc-is-hyperbolic)
1943 (if (calc-is-option)
1944 (calc-binary-op "ludb" 'calcFunc-dbfield arg
)
1945 (calc-unary-op "ludb" 'calcFunc-dbfield arg
))
1946 (if (calc-is-option)
1947 (calc-binary-op "ludb" 'calcFunc-dbpower arg
)
1948 (calc-unary-op "ludb" 'calcFunc-dbpower arg
)))))
1950 (defun calc-np (arg)
1953 (if (calc-is-hyperbolic)
1954 (if (calc-is-option)
1955 (calc-binary-op "lunp" 'calcFunc-npfield arg
)
1956 (calc-unary-op "lunp" 'calcFunc-npfield arg
))
1957 (if (calc-is-option)
1958 (calc-binary-op "lunp" 'calcFunc-nppower arg
)
1959 (calc-unary-op "lunp" 'calcFunc-nppower arg
)))))
1964 (defvar calc-note-threshold
)
1966 (defun math-midi-round (num)
1967 "Round NUM to an integer N if NUM is within calc-note-threshold cents of N."
1968 (let* ((n (math-round num
))
1971 (if (< (math-compare diff
1972 (math-div (math-read-expr calc-note-threshold
) 100)) 0)
1976 (defconst math-notes
1977 '(((var C var-C
) .
0)
1978 ((var Csharp var-Csharp
) .
1)
1979 ; ((var C♯ var-C♯) . 1)
1980 ((var Dflat var-Dflat
) .
1)
1981 ; ((var D♭ var-D♭) . 1)
1983 ((var Dsharp var-Dsharp
) .
3)
1984 ; ((var D♯ var-D♯) . 3)
1987 ((var Fsharp var-Fsharp
) .
6)
1988 ; ((var F♯ var-F♯) . 6)
1989 ((var Gflat var-Gflat
) .
6)
1990 ; ((var G♭ var-G♭) . 6)
1992 ((var Gsharp var-Gsharp
) .
8)
1993 ; ((var G♯ var-G♯) . 8)
1995 ((var Asharp var-Asharp
) .
10)
1996 ; ((var A♯ var-A♯) . 10)
1997 ((var Bflat var-Bflat
) .
10)
1998 ; ((var B♭ var-B♭) . 10)
1999 ((var B var-B
) .
11))
2000 "An alist of notes with their number of semitones above C.")
2002 (defun math-freqp (freq)
2003 "Non-nil if FREQ is a positive number times the unit Hz.
2004 If non-nil, return the coefficient of Hz."
2005 (let ((freqcoef (math-simplify-units
2006 (math-div freq
'(var Hz var-Hz
)))))
2007 (if (Math-posp freqcoef
) freqcoef
)))
2009 (defun math-midip (num)
2010 "Non-nil if NUM is a possible MIDI note number.
2011 If non-nil, return NUM."
2012 (if (Math-numberp num
) num
))
2014 (defun math-spnp (spn)
2015 "Non-nil if NUM is a scientific pitch note (note + cents).
2016 If non-nil, return a list consisting of the note and the cents coefficient."
2017 (let (note cents rnote rcents
)
2018 (if (eq (car-safe spn
) '+)
2019 (setq note
(nth 1 spn
)
2024 ((and ;; NOTE is a note, CENTS is nil or cents.
2025 (eq (car-safe note
) 'calcFunc-subscr
)
2026 (assoc (nth 1 note
) math-notes
)
2027 (integerp (nth 2 note
))
2031 (Math-numberp (setq rcents
2033 (math-div cents
'(var cents var-cents
)))))))
2034 (list rnote rcents
))
2035 ((and ;; CENTS is a note, NOTE is cents.
2036 (eq (car-safe cents
) 'calcFunc-subscr
)
2037 (assoc (nth 1 cents
) math-notes
)
2038 (integerp (nth 2 cents
))
2042 (Math-numberp (setq rcents
2044 (math-div note
'(var cents var-cents
)))))))
2045 (list rnote rcents
)))))
2047 (defun math-freq-to-midi (freq)
2048 "Return the midi note number corresponding to FREQ Hz."
2049 (let ((midi (math-add
2056 (math-midi-round midi
)))
2058 (defun math-spn-to-midi (spn)
2059 "Return the MIDI number corresponding to SPN."
2060 (let* ((note (cdr (assoc (nth 1 (car spn
)) math-notes
)))
2061 (octave (math-add (nth 2 (car spn
)) 1))
2064 (math-mul 12 octave
)
2067 (math-add midi
(math-div cents
100))
2070 (defun math-midi-to-spn (midi)
2071 "Return the scientific pitch notation corresponding to midi number MIDI."
2073 (if (math-integerp midi
)
2076 (setq midin
(math-floor midi
)
2077 cents
(math-mul 100 (math-sub midi midin
))))
2078 (let* ((nr ;; This should be (math-idivmod midin 12), but with
2079 ;; better behavior for negative midin.
2080 (if (Math-negp midin
)
2081 (let ((dm (math-idivmod (math-neg midin
) 12)))
2083 (cons (math-neg (car dm
)) 0)
2085 (math-sub (math-neg (car dm
)) 1)
2086 (math-sub 12 (cdr dm
)))))
2087 (math-idivmod midin
12)))
2088 (n (math-sub (car nr
) 1))
2089 (note (car (rassoc (cdr nr
) math-notes
))))
2091 (list '+ (list 'calcFunc-subscr note n
)
2092 (list '* cents
'(var cents var-cents
)))
2093 (list 'calcFunc-subscr note n
)))))
2095 (defun math-freq-to-spn (freq)
2096 "Return the scientific pitch notation corresponding to FREQ Hz."
2097 (math-with-extra-prec 3
2098 (math-midi-to-spn (math-freq-to-midi freq
))))
2100 (defun math-midi-to-freq (midi)
2101 "Return the frequency of the note with midi number MIDI."
2114 (defun math-spn-to-freq (spn)
2115 "Return the frequency of the note with scientific pitch notation SPN."
2116 (math-midi-to-freq (math-spn-to-midi spn
)))
2118 (defun calcFunc-spn (expr)
2119 "Return EXPR written as scientific pitch notation + cents."
2120 ;; Get the coefficient of Hz
2123 ((setq note
(math-freqp expr
))
2124 (math-freq-to-spn note
))
2125 ((setq note
(math-midip expr
))
2126 (math-midi-to-spn note
))
2130 (math-reject-arg expr
"*Improper expression")))))
2132 (defun calcFunc-midi (expr)
2133 "Return EXPR written as a MIDI number."
2136 ((setq note
(math-freqp expr
))
2137 (math-freq-to-midi note
))
2138 ((setq note
(math-spnp expr
))
2139 (math-spn-to-midi note
))
2143 (math-reject-arg expr
"*Improper expression")))))
2145 (defun calcFunc-freq (expr)
2146 "Return the frequency corresponding to EXPR."
2149 ((setq note
(math-midip expr
))
2150 (math-midi-to-freq note
))
2151 ((setq note
(math-spnp expr
))
2152 (math-spn-to-freq note
))
2156 (math-reject-arg expr
"*Improper expression")))))
2158 (defun calc-freq (arg)
2159 "Return the frequency corresponding to the expression on the stack."
2162 (calc-unary-op "freq" 'calcFunc-freq arg
)))
2164 (defun calc-midi (arg)
2165 "Return the MIDI number corresponding to the expression on the stack."
2168 (calc-unary-op "midi" 'calcFunc-midi arg
)))
2170 (defun calc-spn (arg)
2171 "Return the scientific pitch notation corresponding to the expression on the stack."
2174 (calc-unary-op "spn" 'calcFunc-spn arg
)))
2177 (provide 'calc-units
)
2179 ;;; calc-units.el ends here