1 ;;; calc-units.el --- unit conversion functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file is autoloaded from calc-ext.el.
37 ;;; Units table last updated 9-Jan-91 by Ulrich Mueller (ulm@vsnhd1.cern.ch)
38 ;;; with some additions by Przemek Klosowski (przemek@rrdstrad.nist.gov)
39 ;;; Updated April 2002 by Jochen Küpper
41 ;;; Updated August 2007, using
42 ;;; CODATA (http://physics.nist.gov/cuu/Constants/index.html)
43 ;;; NIST (http://physics.nist.gov/Pubs/SP811/appenB9.html)
44 ;;; ESUWM (Encyclopaedia of Scientific Units, Weights and
45 ;;; Measures, by François Cardarelli)
46 ;;; All conversions are exact unless otherwise noted.
48 (defvar math-standard-units
51 ( in
"254*10^(-2) cm" "Inch" )
54 ( mi
"5280 ft" "Mile" )
55 ( au
"149597870691. m" "Astronomical Unit" )
56 ;; (approx) NASA JPL (http://neo.jpl.nasa.gov/glossary/au.html)
57 ( lyr
"c yr" "Light Year" )
58 ( pc
"3.0856775854e16 m" "Parsec" ) ;; (approx) ESUWM
59 ( nmi
"1852 m" "Nautical Mile" )
60 ( fath
"6 ft" "Fathom" )
61 ( mu
"1 um" "Micron" )
62 ( mil
"in/1000" "Mil" )
63 ( point
"in/72" "Point (1/72 inch)" )
64 ( Ang
"10^(-10) m" "Angstrom" )
65 ( mfi
"mi+ft+in" "Miles + feet + inches" )
67 ( texpt
"(100/7227) in" "Point (TeX conventions)" )
68 ( texpc
"12 texpt" "Pica" )
69 ( texbp
"point" "Big point (TeX conventions)" )
70 ( texdd
"(1238/1157) texpt" "Didot point" )
71 ( texcc
"12 texdd" "Cicero" )
72 ( texsp
"(1/65536) texpt" "Scaled TeX point" )
75 ( hect
"10000 m^2" "*Hectare" )
77 ( acre
"mi^2 / 640" "Acre" )
78 ( b
"10^(-28) m^2" "Barn" )
81 ( L
"10^(-3) m^3" "*Liter" )
83 ( gal
"4 qt" "US Gallon" )
86 ( cup
"8 ozfl" "Cup" )
87 ( ozfl
"2 tbsp" "Fluid Ounce" )
88 ( floz
"2 tbsp" "Fluid Ounce" )
89 ( tbsp
"3 tsp" "Tablespoon" )
90 ;; ESUWM defines a US gallon as 231 in^3.
91 ;; That gives the following exact value for tsp.
92 ( tsp
"492892159375*10^(-11) ml" "Teaspoon" )
93 ( vol
"tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" )
94 ( galC
"galUK" "Canadian Gallon" )
95 ( galUK
"454609*10^(-5) L" "UK Gallon" ) ;; NIST
100 ( min
"60 s" "Minute" )
101 ( hr
"60 min" "Hour" )
102 ( day
"24 hr" "Day" )
103 ( wk
"7 day" "Week" )
104 ( hms
"wk+day+hr+min+s" "Hours, minutes, seconds" )
105 ( yr
"365.25 day" "Year" ) ;; (approx, but keep)
109 ( mph
"mi/hr" "*Miles per hour" )
110 ( kph
"km/hr" "Kilometers per hour" )
111 ( knot
"nmi/hr" "Knot" )
112 ( c
"299792458 m/s" "Speed of light" ) ;;; CODATA
115 ( ga
"980665*10^(-5) m/s^2" "*\"g\" acceleration" ) ;; CODATA
119 ( lb
"16 oz" "Pound (mass)" )
120 ( oz
"28349523125*10^(-9) g" "Ounce (mass)" ) ;; ESUWM
121 ( ton
"2000 lb" "Ton" )
122 ( tpo
"ton+lb+oz" "Tons + pounds + ounces (mass)" )
123 ( t
"1000 kg" "Metric ton" )
124 ( tonUK
"10160469088*10^(-7) kg" "UK ton" ) ;; ESUWM
125 ( lbt
"12 ozt" "Troy pound" )
126 ( ozt
"31.10347680 g" "Troy ounce" ) ;; (approx) ESUWM
127 ( ct
"(2/10) g" "Carat" ) ;; ESUWM
128 ( u
"1.660538782e-27 kg" "Unified atomic mass" );;(approx) CODATA
131 ( N
"m kg/s^2" "*Newton" )
132 ( dyn
"10^(-5) N" "Dyne" )
133 ( gf
"ga g" "Gram (force)" )
134 ( lbf
"ga lb" "Pound (force)" )
135 ( kip
"1000 lbf" "Kilopound (force)" )
136 ( pdl
"138254954376*10^(-12) N" "Poundal" ) ;; ESUWM
140 ( erg
"10^(-7) J" "Erg" )
141 ( cal
"4.18674 J" "International Table Calorie" );;(approx) ESUWM
142 ( Btu
"105505585262*10^(-8) J" "International Table Btu" ) ;; ESUWM
143 ( eV
"ech V" "Electron volt" )
144 ( ev
"eV" "Electron volt" )
145 ( therm
"105506000 J" "EEC therm" )
146 ( invcm
"h c/cm" "Energy in inverse centimeters" )
147 ( Kayser
"invcm" "Kayser (inverse centimeter energy)" )
148 ( men
"100/invcm" "Inverse energy in meters" )
149 ( Hzen
"h Hz" "Energy in Hertz")
150 ( Ken
"k K" "Energy in Kelvins")
151 ( Wh
"W hr" "Watt hour")
152 ( Ws
"W s" "Watt second")
156 ( hp
"745.699871581 W" "Horsepower" ) ;;(approx) ESUWM
159 ( K nil
"*Degree Kelvin" K
)
160 ( dK
"K" "Degree Kelvin" K
)
161 ( degK
"K" "Degree Kelvin" K
)
162 ( dC
"K" "Degree Celsius" C
)
163 ( degC
"K" "Degree Celsius" C
)
164 ( dF
"(5/9) K" "Degree Fahrenheit" F
)
165 ( degF
"(5/9) K" "Degree Fahrenheit" F
)
168 ( Pa
"N/m^2" "*Pascal" )
169 ( bar
"10^5 Pa" "Bar" )
170 ( atm
"101325 Pa" "Standard atmosphere" ) ;; CODATA
171 ( Torr
"1.333224e2 Pa" "Torr" ) ;;(approx) NIST
172 ( mHg
"1000 Torr" "Meter of mercury" )
173 ( inHg
"254*10^(-1) mmHg" "Inch of mercury" )
174 ( inH2O
"2.490889e2 Pa" "Inch of water" ) ;;(approx) NIST
175 ( psi
"lbf/in^2" "Pounds per square inch" )
178 ( P
"(1/10) Pa s" "*Poise" )
179 ( St
"10^(-4) m^2/s" "Stokes" )
183 ( C
"A s" "Coulomb" )
184 ( Fdy
"ech Nav" "Faraday" )
185 ( e
"ech" "Elementary charge" )
186 ( ech
"1.602176487e-19 C" "Elementary charge" ) ;;(approx) CODATA
190 ( S
"A/V" "Siemens" )
193 ( T
"Wb/m^2" "Tesla" )
194 ( Gs
"10^(-4) T" "Gauss" )
197 ;; Luminous intensity
198 ( cd nil
"*Candela" )
199 ( sb
"10000 cd/m^2" "Stilb" )
200 ( lm
"cd sr" "Lumen" )
201 ( lx
"lm/m^2" "Lux" )
202 ( ph
"10000 lx" "Phot" )
203 ( fc
"10.76391 lx" "Footcandle" ) ;;(approx) NIST
204 ( lam
"10000 lm/m^2" "Lambert" )
205 ( flam
"3.426259 cd/m^2" "Footlambert" ) ;;(approx) NIST
208 ( Bq
"1/s" "*Becquerel" )
209 ( Ci
"37*10^9 Bq" "Curie" ) ;; ESUWM
211 ( Sv
"Gy" "Sievert" )
212 ( R
"258*10^(-6) C/kg" "Roentgen" ) ;; NIST
213 ( rd
"(1/100) Gy" "Rad" )
216 ;; Amount of substance
220 ( rad nil
"*Radian" )
221 ( circ
"2 pi rad" "Full circle" )
222 ( rev
"circ" "Full revolution" )
223 ( deg
"circ/360" "Degree" )
224 ( arcmin
"deg/60" "Arc minute" )
225 ( arcsec
"arcmin/60" "Arc second" )
226 ( grad
"circ/400" "Grade" )
227 ( rpm
"rev/min" "Revolutions per minute" )
230 ( sr nil
"*Steradian" )
232 ;; Other physical quantities
233 ;; The values are from CODATA, and are approximate.
234 ( h
"6.62606896e-34 J s" "*Planck's constant" )
235 ( hbar
"h / (2 pi)" "Planck's constant" )
236 ( mu0
"4 pi 10^(-7) H/m" "Permeability of vacuum" )
237 ( G
"6.67428e-11 m^3/(kg s^2)" "Gravitational constant" )
238 ( Nav
"6.02214179e23 / mol" "Avagadro's constant" )
239 ( me
"9.10938215e-31 kg" "Electron rest mass" )
240 ( mp
"1.672621637e-27 kg" "Proton rest mass" )
241 ( mn
"1.674927211e-27 kg" "Neutron rest mass" )
242 ( mmu
"1.88353130e-28 kg" "Muon rest mass" )
243 ( Ryd
"10973731.568527 /m" "Rydberg's constant" )
244 ( k
"1.3806504e-23 J/K" "Boltzmann's constant" )
245 ( alpha
"7.2973525376e-3" "Fine structure constant" )
246 ( muB
"927.400915e-26 J/T" "Bohr magneton" )
247 ( muN
"5.05078324e-27 J/T" "Nuclear magneton" )
248 ( mue
"-928.476377e-26 J/T" "Electron magnetic moment" )
249 ( mup
"1.410606662e-26 J/T" "Proton magnetic moment" )
250 ( R0
"8.314472 J/(mol K)" "Molar gas constant" )
251 ( V0
"22.710981e-3 m^3/mol" "Standard volume of ideal gas" )))
254 (defvar math-additional-units nil
255 "*Additional units table for user-defined units.
256 Must be formatted like math-standard-units.
257 If this is changed, be sure to set math-units-table to nil to ensure
258 that the combined units table will be rebuilt.")
260 (defvar math-unit-prefixes
261 '( ( ?Y
(^
10 24) "Yotta" )
262 ( ?Z
(^
10 21) "Zetta" )
263 ( ?E
(^
10 18) "Exa" )
264 ( ?P
(^
10 15) "Peta" )
265 ( ?T
(^
10 12) "Tera" )
266 ( ?G
(^
10 9) "Giga" )
267 ( ?M
(^
10 6) "Mega" )
268 ( ?k
(^
10 3) "Kilo" )
269 ( ?K
(^
10 3) "Kilo" )
270 ( ?h
(^
10 2) "Hecto" )
271 ( ?H
(^
10 2) "Hecto" )
272 ( ?D
(^
10 1) "Deka" )
274 ( ?d
(^
10 -
1) "Deci" )
275 ( ?c
(^
10 -
2) "Centi" )
276 ( ?m
(^
10 -
3) "Milli" )
277 ( ?u
(^
10 -
6) "Micro" )
278 ( ?n
(^
10 -
9) "Nano" )
279 ( ?p
(^
10 -
12) "Pico" )
280 ( ?f
(^
10 -
15) "Femto" )
281 ( ?a
(^
10 -
18) "Atto" )
282 ( ?z
(^
10 -
21) "zepto" )
283 ( ?y
(^
10 -
24) "yocto" )))
285 (defvar math-standard-units-systems
287 ( si
( ( g
'(/ (var kg var-kg
) 1000) ) ) )
288 ( mks
( ( g
'(/ (var kg var-kg
) 1000) ) ) )
289 ( cgs
( ( m
'(* (var cm var-cm
) 100 ) ) ) )))
291 (defvar math-units-table nil
292 "Internal units table derived from math-defined-units.
293 Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
295 (defvar math-units-table-buffer-valid nil
)
299 (defun calc-base-units ()
302 (let ((calc-autorange-units nil
))
303 (calc-enter-result 1 "bsun" (math-simplify-units
304 (math-to-standard-units (calc-top-n 1)
307 (defun calc-quick-units ()
310 (let* ((num (- last-command-char ?
0))
311 (pos (if (= num
0) 10 num
))
312 (units (calc-var-value 'var-Units
))
313 (expr (calc-top-n 1)))
314 (unless (and (>= num
0) (<= num
9))
315 (error "Bad unit number"))
316 (unless (math-vectorp units
)
317 (error "No \"quick units\" are defined"))
318 (unless (< pos
(length units
))
319 (error "Unit number %d not defined" pos
))
320 (if (math-units-in-expr-p expr nil
)
321 (calc-enter-result 1 (format "cun%d" num
)
322 (math-convert-units expr
(nth pos units
)))
323 (calc-enter-result 1 (format "*un%d" num
)
325 (math-mul expr
(nth pos units
))))))))
327 (defun math-get-standard-units (expr)
328 "Return the standard units in EXPR."
331 (math-to-standard-units expr nil
))))
333 (defun math-get-units (expr)
334 "Return the units in EXPR."
336 (math-extract-units expr
)))
338 (defun math-make-unit-string (expr)
339 "Return EXPR in string form.
340 If EXPR is nil, return nil."
342 (let ((cexpr (math-compose-expr expr
0)))
343 (replace-regexp-in-string
347 (math-composition-to-string cexpr
))))))
349 (defvar math-default-units-table
350 (make-hash-table :test
'equal
)
351 "A table storing previously converted units.")
353 (defun math-get-default-units (expr)
354 "Get default units to use when converting the units in EXPR."
355 (let* ((units (math-get-units expr
))
356 (standard-units (math-get-standard-units expr
))
357 (default-units (gethash
359 math-default-units-table
)))
360 (if (equal units
(car default-units
))
361 (math-make-unit-string (cadr default-units
))
362 (math-make-unit-string (car default-units
)))))
364 (defun math-put-default-units (expr)
365 "Put the units in EXPR in the default units table."
366 (let* ((units (math-get-units expr
))
367 (standard-units (math-get-standard-units expr
))
368 (default-units (gethash
370 math-default-units-table
)))
373 (puthash standard-units
(list units
) math-default-units-table
))
374 ((not (equal units
(car default-units
)))
375 (puthash standard-units
376 (list units
(car default-units
))
377 math-default-units-table
)))))
380 (defun calc-convert-units (&optional old-units new-units
)
383 (let ((expr (calc-top-n 1))
388 (unless (math-units-in-expr-p expr t
)
389 (let ((uold (or old-units
391 (setq uoldname
(read-string "Old units: "))
392 (if (equal uoldname
"")
396 (if (string-match "\\` */" uoldname
)
397 (setq uoldname
(concat "1" uoldname
)))
398 (math-read-expr uoldname
))))))
399 (when (eq (car-safe uold
) 'error
)
400 (error "Bad format in units expression: %s" (nth 1 uold
)))
401 (setq expr
(math-mul expr uold
))))
403 (setq defunits
(math-get-default-units expr
))
407 (concat "Old units: "
419 (string= new-units
"")
421 (setq new-units defunits
)))
422 (when (string-match "\\` */" new-units
)
423 (setq new-units
(concat "1" new-units
)))
424 (setq units
(math-read-expr new-units
))
425 (when (eq (car-safe units
) 'error
)
426 (error "Bad format in units expression: %s" (nth 2 units
)))
427 (math-put-default-units units
)
428 (let ((unew (math-units-in-expr-p units t
))
429 (std (and (eq (car-safe units
) 'var
)
430 (assq (nth 1 units
) math-standard-units-systems
))))
432 (calc-enter-result 1 "cvun" (math-simplify-units
433 (math-to-standard-units expr
436 (error "No units specified"))
437 (calc-enter-result 1 "cvun"
440 (and uoldname
(not (equal uoldname
"1"))))))))))
442 (defun calc-autorange-units (arg)
445 (calc-change-mode 'calc-autorange-units arg nil t
)
446 (message (if calc-autorange-units
447 "Adjusting target unit prefix automatically"
448 "Using target units exactly"))))
450 (defun calc-convert-temperature (&optional old-units new-units
)
453 (let ((expr (calc-top-n 1))
458 (setq uold
(or old-units
459 (let ((units (math-single-units-in-expr-p expr
)))
462 (list 'var
(car units
)
463 (intern (concat "var-"
466 (error "Not a pure temperature expression"))
468 (setq uoldname
(read-string
469 "Old temperature units: ")))))))
470 (when (eq (car-safe uold
) 'error
)
471 (error "Bad format in units expression: %s" (nth 2 uold
)))
472 (or (math-units-in-expr-p expr nil
)
473 (setq expr
(math-mul expr uold
)))
474 (setq defunits
(math-get-default-units expr
))
475 (setq unew
(or new-units
480 (concat "Old temperature units: "
483 "New temperature units")
489 (when (eq (car-safe unew
) 'error
)
490 (error "Bad format in units expression: %s" (nth 2 unew
)))
491 (math-put-default-units unew
)
492 (let ((ntemp (calc-normalize
494 (math-convert-temperature expr uold unew
496 (if (Math-zerop ntemp
)
497 (setq ntemp
(list '* ntemp unew
)))
498 (let ((calc-simplify-mode 'none
))
499 (calc-enter-result 1 "cvtm" ntemp
))))))
501 (defun calc-remove-units ()
504 (calc-enter-result 1 "rmun" (math-simplify-units
505 (math-remove-units (calc-top-n 1))))))
507 (defun calc-extract-units ()
510 (calc-enter-result 1 "rmun" (math-simplify-units
511 (math-extract-units (calc-top-n 1))))))
513 ;; The variables calc-num-units and calc-den-units are local to
514 ;; calc-explain-units, but are used by calc-explain-units-rec,
515 ;; which is called by calc-explain-units.
516 (defvar calc-num-units
)
517 (defvar calc-den-units
)
519 (defun calc-explain-units ()
522 (let ((calc-num-units nil
)
523 (calc-den-units nil
))
524 (calc-explain-units-rec (calc-top-n 1) 1)
525 (and calc-den-units
(string-match "^[^(].* .*[^)]$" calc-den-units
)
526 (setq calc-den-units
(concat "(" calc-den-units
")")))
529 (message "%s per %s" calc-num-units calc-den-units
)
530 (message "%s" calc-num-units
))
532 (message "1 per %s" calc-den-units
)
533 (message "No units in expression"))))))
535 (defun calc-explain-units-rec (expr pow
)
536 (let ((u (math-check-unit-name expr
))
538 (if (and u
(not (math-zerop pow
)))
539 (let ((name (or (nth 2 u
) (symbol-name (car u
)))))
540 (if (eq (aref name
0) ?\
*)
541 (setq name
(substring name
1)))
542 (if (string-match "[^a-zA-Z0-9']" name
)
543 (if (string-match "^[a-zA-Z0-9' ()]*$" name
)
544 (while (setq pos
(string-match "[ ()]" name
))
545 (setq name
(concat (substring name
0 pos
)
546 (if (eq (aref name pos
) 32) "-" "")
547 (substring name
(1+ pos
)))))
548 (setq name
(concat "(" name
")"))))
549 (or (eq (nth 1 expr
) (car u
))
550 (setq name
(concat (nth 2 (assq (aref (symbol-name
553 (if (and (string-match "[^a-zA-Z0-9']" name
)
554 (not (memq (car u
) '(mHg gf
))))
557 (cond ((or (math-equal-int pow
1)
558 (math-equal-int pow -
1)))
559 ((or (math-equal-int pow
2)
560 (math-equal-int pow -
2))
561 (if (equal (nth 4 u
) '((m .
1)))
562 (setq name
(concat "Square-" name
))
563 (setq name
(concat name
"-squared"))))
564 ((or (math-equal-int pow
3)
565 (math-equal-int pow -
3))
566 (if (equal (nth 4 u
) '((m .
1)))
567 (setq name
(concat "Cubic-" name
))
568 (setq name
(concat name
"-cubed"))))
570 (setq name
(concat name
"^"
571 (math-format-number (math-abs pow
))))))
573 (setq calc-num-units
(if calc-num-units
574 (concat calc-num-units
" " name
)
576 (setq calc-den-units
(if calc-den-units
577 (concat calc-den-units
" " name
)
579 (cond ((eq (car-safe expr
) '*)
580 (calc-explain-units-rec (nth 1 expr
) pow
)
581 (calc-explain-units-rec (nth 2 expr
) pow
))
582 ((eq (car-safe expr
) '/)
583 (calc-explain-units-rec (nth 1 expr
) pow
)
584 (calc-explain-units-rec (nth 2 expr
) (- pow
)))
585 ((memq (car-safe expr
) '(neg + -
))
586 (calc-explain-units-rec (nth 1 expr
) pow
))
587 ((and (eq (car-safe expr
) '^
)
588 (math-realp (nth 2 expr
)))
589 (calc-explain-units-rec (nth 1 expr
)
590 (math-mul pow
(nth 2 expr
))))))))
592 (defun calc-simplify-units ()
595 (calc-with-default-simplification
596 (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1))))))
598 (defun calc-view-units-table (n)
600 (and n
(setq math-units-table-buffer-valid nil
))
601 (let ((win (get-buffer-window "*Units Table*")))
604 math-units-table-buffer-valid
)
606 (bury-buffer (window-buffer win
))
607 (let ((curwin (selected-window)))
609 (switch-to-buffer nil
)
610 (select-window curwin
)))
611 (math-build-units-table-buffer nil
))))
613 (defun calc-enter-units-table (n)
615 (and n
(setq math-units-table-buffer-valid nil
))
616 (math-build-units-table-buffer t
)
617 (message "%s" (substitute-command-keys "Type \\[calc] to return to the Calculator")))
619 (defun calc-define-unit (uname desc
)
620 (interactive "SDefine unit name: \nsDescription: ")
622 (let ((form (calc-top-n 1))
623 (unit (assq uname math-additional-units
)))
625 (setq math-additional-units
626 (cons (setq unit
(list uname nil nil
))
627 math-additional-units
)
628 math-units-table nil
))
629 (setcar (cdr unit
) (and (not (and (eq (car-safe form
) 'var
)
630 (eq (nth 1 form
) uname
)))
631 (not (math-equal-int form
1))
632 (math-format-flat-expr form
0)))
633 (setcar (cdr (cdr unit
)) (and (not (equal desc
""))
635 (calc-invalidate-units-table))
637 (defun calc-undefine-unit (uname)
638 (interactive "SUndefine unit name: ")
640 (let ((unit (assq uname math-additional-units
)))
642 (if (assq uname math-standard-units
)
643 (error "\"%s\" is a predefined unit name" uname
)
644 (error "Unit name \"%s\" not found" uname
)))
645 (setq math-additional-units
(delq unit math-additional-units
)
646 math-units-table nil
)))
647 (calc-invalidate-units-table))
649 (defun calc-invalidate-units-table ()
650 (setq math-units-table nil
)
651 (let ((buf (get-buffer "*Units Table*")))
656 (goto-char (point-min))
657 (if (looking-at "Calculator Units Table")
658 (let ((inhibit-read-only t
))
659 (insert "(Obsolete) "))))))))
661 (defun calc-get-unit-definition (uname)
662 (interactive "SGet definition for unit: ")
664 (math-build-units-table)
665 (let ((unit (assq uname math-units-table
)))
667 (error "Unit name \"%s\" not found" uname
))
668 (let ((msg (nth 2 unit
)))
670 (if (string-match "^\\*" msg
)
671 (setq msg
(substring msg
1)))
672 (setq msg
(symbol-name uname
)))
675 (calc-enter-result 0 "ugdf" (nth 1 unit
))
676 (message "Derived unit: %s" msg
))
677 (calc-enter-result 0 "ugdf" (list 'var uname
680 (symbol-name uname
)))))
681 (message "Base unit: %s" msg
))))))
683 (defun calc-permanent-units ()
687 (set-buffer (find-file-noselect (substitute-in-file-name
688 calc-settings-file
)))
689 (goto-char (point-min))
690 (if (and (search-forward ";;; Custom units stored by Calc" nil t
)
694 (search-forward "\n;;; End of custom units" nil t
)))
698 (delete-region pos
(point)))
699 (goto-char (point-max))
702 (insert ";;; Custom units stored by Calc on " (current-time-string) "\n")
703 (if math-additional-units
705 (insert "(setq math-additional-units '(\n")
706 (let ((list math-additional-units
))
708 (insert " (" (symbol-name (car (car list
))) " "
709 (if (nth 1 (car list
))
710 (if (stringp (nth 1 (car list
)))
711 (prin1-to-string (nth 1 (car list
)))
712 (prin1-to-string (math-format-flat-expr
713 (nth 1 (car list
)) 0)))
716 (prin1-to-string (nth 2 (car list
)))
718 (setq list
(cdr list
))))
720 (insert ";;; (no custom units defined)\n"))
721 (insert ";;; End of custom units\n")
725 ;; The variable math-cu-unit-list is local to math-build-units-table,
726 ;; but is used by math-compare-unit-names, which is called (indirectly)
727 ;; by math-build-units-table.
728 ;; math-cu-unit-list is also local to math-convert-units, but is used
729 ;; by math-convert-units-rec, which is called by math-convert-units.
730 (defvar math-cu-unit-list
)
732 (defun math-build-units-table ()
734 (let* ((combined-units (append math-additional-units
735 math-standard-units
))
736 (math-cu-unit-list (mapcar 'car combined-units
))
738 (message "Building units table...")
739 (setq math-units-table-buffer-valid nil
)
740 (setq tab
(mapcar (function
744 (if (stringp (nth 1 x
))
745 (let ((exp (math-read-plain-expr
747 (if (eq (car-safe exp
) 'error
)
748 (error "Format error in definition of %s in units table: %s"
755 (list (cons (car x
) 1))))))
757 (let ((math-units-table tab
))
758 (mapc 'math-find-base-units tab
))
759 (message "Building units table...done")
760 (setq math-units-table tab
))))
762 ;; The variables math-fbu-base and math-fbu-entry are local to
763 ;; math-find-base-units, but are used by math-find-base-units-rec,
764 ;; which is called by math-find-base-units.
765 (defvar math-fbu-base
)
766 (defvar math-fbu-entry
)
768 (defun math-find-base-units (math-fbu-entry)
769 (if (eq (nth 4 math-fbu-entry
) 'boom
)
770 (error "Circular definition involving unit %s" (car math-fbu-entry
)))
771 (or (nth 4 math-fbu-entry
)
773 (setcar (nthcdr 4 math-fbu-entry
) 'boom
)
774 (math-find-base-units-rec (nth 1 math-fbu-entry
) 1)
776 (error "Dimensionless definition for unit %s" (car math-fbu-entry
)))
777 (while (eq (cdr (car math-fbu-base
)) 0)
778 (setq math-fbu-base
(cdr math-fbu-base
)))
779 (let ((b math-fbu-base
))
781 (if (eq (cdr (car (cdr b
))) 0)
782 (setcdr b
(cdr (cdr b
)))
784 (setq math-fbu-base
(sort math-fbu-base
'math-compare-unit-names
))
785 (setcar (nthcdr 4 math-fbu-entry
) math-fbu-base
)
788 (defun math-compare-unit-names (a b
)
789 (memq (car b
) (cdr (memq (car a
) math-cu-unit-list
))))
791 (defun math-find-base-units-rec (expr pow
)
792 (let ((u (math-check-unit-name expr
)))
794 (let ((ulist (math-find-base-units u
)))
796 (let ((p (* (cdr (car ulist
)) pow
))
797 (old (assq (car (car ulist
)) math-fbu-base
)))
799 (setcdr old
(+ (cdr old
) p
))
801 (cons (cons (car (car ulist
)) p
) math-fbu-base
))))
802 (setq ulist
(cdr ulist
)))))
803 ((math-scalarp expr
))
804 ((and (eq (car expr
) '^
)
805 (integerp (nth 2 expr
)))
806 (math-find-base-units-rec (nth 1 expr
) (* pow
(nth 2 expr
))))
808 (math-find-base-units-rec (nth 1 expr
) pow
)
809 (math-find-base-units-rec (nth 2 expr
) pow
))
811 (math-find-base-units-rec (nth 1 expr
) pow
)
812 (math-find-base-units-rec (nth 2 expr
) (- pow
)))
813 ((eq (car expr
) 'neg
)
814 (math-find-base-units-rec (nth 1 expr
) pow
))
816 (math-find-base-units-rec (nth 1 expr
) pow
))
817 ((eq (car expr
) 'var
)
818 (or (eq (nth 1 expr
) 'pi
)
819 (error "Unknown name %s in defining expression for unit %s"
820 (nth 1 expr
) (car math-fbu-entry
))))
821 (t (error "Malformed defining expression for unit %s" (car math-fbu-entry
))))))
824 (defun math-units-in-expr-p (expr sub-exprs
)
826 (if (eq (car expr
) 'var
)
827 (math-check-unit-name expr
)
829 (memq (car expr
) '(* / ^
)))
830 (or (math-units-in-expr-p (nth 1 expr
) sub-exprs
)
831 (math-units-in-expr-p (nth 2 expr
) sub-exprs
))))))
833 (defun math-only-units-in-expr-p (expr)
835 (if (eq (car expr
) 'var
)
836 (math-check-unit-name expr
)
837 (if (memq (car expr
) '(* /))
838 (and (math-only-units-in-expr-p (nth 1 expr
))
839 (math-only-units-in-expr-p (nth 2 expr
)))
840 (and (eq (car expr
) '^
)
841 (and (math-only-units-in-expr-p (nth 1 expr
))
842 (math-realp (nth 2 expr
))))))))
844 (defun math-single-units-in-expr-p (expr)
845 (cond ((math-scalarp expr
) nil
)
846 ((eq (car expr
) 'var
)
847 (math-check-unit-name expr
))
849 (let ((u1 (math-single-units-in-expr-p (nth 1 expr
)))
850 (u2 (math-single-units-in-expr-p (nth 2 expr
))))
851 (or (and u1 u2
'wrong
)
855 (if (math-units-in-expr-p (nth 2 expr
) nil
)
857 (math-single-units-in-expr-p (nth 1 expr
))))
860 (defun math-check-unit-name (v)
861 (and (eq (car-safe v
) 'var
)
862 (or (assq (nth 1 v
) (or math-units-table
(math-build-units-table)))
863 (let ((name (symbol-name (nth 1 v
))))
864 (and (> (length name
) 1)
865 (assq (aref name
0) math-unit-prefixes
)
866 (or (assq (intern (substring name
1)) math-units-table
)
867 (and (eq (aref name
0) ?M
)
869 (eq (aref name
1) ?e
)
870 (eq (aref name
2) ?g
)
871 (assq (intern (substring name
3))
872 math-units-table
))))))))
874 ;; The variable math-which-standard is local to math-to-standard-units,
875 ;; but is used by math-to-standard-rec, which is called by
876 ;; math-to-standard-units.
877 (defvar math-which-standard
)
879 (defun math-to-standard-units (expr math-which-standard
)
880 (math-to-standard-rec expr
))
882 (defun math-to-standard-rec (expr)
883 (if (eq (car-safe expr
) 'var
)
884 (let ((u (math-check-unit-name expr
))
889 (setq expr
(math-to-standard-rec (nth 1 u
)))
890 (let ((st (assq (car u
) math-which-standard
)))
892 (setq expr
(nth 1 st
))
893 (setq expr
(list 'var
(car u
)
894 (intern (concat "var-"
900 (nth 1 (assq (aref (symbol-name base
) 0)
907 (if (Math-primp expr
)
910 (mapcar 'math-to-standard-rec
(cdr expr
))))))
912 (defun math-apply-units (expr units ulist
&optional pure
)
913 (setq expr
(math-simplify-units expr
))
917 (or (math-numberp expr
)
918 (error "Incompatible units"))
920 (setq value
(math-div expr
(nth 1 (car ulist
)))
921 value
(math-floor (let ((calc-internal-prec
922 (1- calc-internal-prec
)))
923 (math-normalize value
)))
924 new
(math-add new
(math-mul value
(car (car ulist
))))
925 expr
(math-sub expr
(math-mul value
(nth 1 (car ulist
))))
927 (math-add new
(math-mul (math-div expr
(nth 1 (car ulist
)))
931 (math-simplify-units (list '* expr units
)))))
933 (defvar math-decompose-units-cache nil
)
934 (defun math-decompose-units (units)
935 (let ((u (math-check-unit-name units
)))
936 (and u
(eq (car-safe (nth 1 u
)) '+)
937 (setq units
(nth 1 u
))))
938 (setq units
(calcFunc-expand units
))
939 (and (eq (car-safe units
) '+)
940 (let ((entry (list units calc-internal-prec calc-prefer-frac
)))
941 (or (equal entry
(car math-decompose-units-cache
))
945 (while (eq (car-safe utemp
) '+)
946 (setq ulist
(cons (math-decompose-unit-part (nth 2 utemp
))
948 utemp
(nth 1 utemp
)))
949 (setq ulist
(cons (math-decompose-unit-part utemp
) ulist
)
951 (while (setq utemp
(cdr utemp
))
952 (unless (equal (nth 2 (car utemp
)) (nth 2 (car ulist
)))
953 (error "Inconsistent units in sum")))
954 (setq math-decompose-units-cache
959 (not (Math-lessp (nth 1 x
)
961 (cdr math-decompose-units-cache
))))
963 (defun math-decompose-unit-part (unit)
965 (math-is-multiple (math-simplify-units (math-to-standard-units
969 ;; The variable math-fcu-u is local to math-find-compatible-unit,
970 ;; but is used by math-find-compatible-rec which is called by
971 ;; math-find-compatible-unit.
974 (defun math-find-compatible-unit (expr unit
)
975 (let ((math-fcu-u (math-check-unit-name unit
)))
977 (math-find-compatible-unit-rec expr
1))))
979 (defun math-find-compatible-unit-rec (expr pow
)
980 (cond ((eq (car-safe expr
) '*)
981 (or (math-find-compatible-unit-rec (nth 1 expr
) pow
)
982 (math-find-compatible-unit-rec (nth 2 expr
) pow
)))
983 ((eq (car-safe expr
) '/)
984 (or (math-find-compatible-unit-rec (nth 1 expr
) pow
)
985 (math-find-compatible-unit-rec (nth 2 expr
) (- pow
))))
986 ((and (eq (car-safe expr
) '^
)
987 (integerp (nth 2 expr
)))
988 (math-find-compatible-unit-rec (nth 1 expr
) (* pow
(nth 2 expr
))))
990 (let ((u2 (math-check-unit-name expr
)))
991 (if (equal (nth 4 math-fcu-u
) (nth 4 u2
))
994 ;; The variables math-cu-new-units and math-cu-pure are local to
995 ;; math-convert-units, but are used by math-convert-units-rec,
996 ;; which is called by math-convert-units.
997 (defvar math-cu-new-units
)
998 (defvar math-cu-pure
)
1000 (defun math-convert-units (expr math-cu-new-units
&optional math-cu-pure
)
1001 (if (eq (car-safe math-cu-new-units
) 'var
)
1002 (let ((unew (assq (nth 1 math-cu-new-units
)
1003 (math-build-units-table))))
1004 (if (eq (car-safe (nth 1 unew
)) '+)
1005 (setq math-cu-new-units
(nth 1 unew
)))))
1006 (math-with-extra-prec 2
1007 (let ((compat (and (not math-cu-pure
)
1008 (math-find-compatible-unit expr math-cu-new-units
)))
1009 (math-cu-unit-list nil
)
1010 (math-combining-units nil
))
1012 (math-simplify-units
1013 (math-mul (math-mul (math-simplify-units
1014 (math-div expr
(math-pow (car compat
)
1016 (math-pow math-cu-new-units
(cdr compat
)))
1017 (math-simplify-units
1018 (math-to-standard-units
1019 (math-pow (math-div (car compat
) math-cu-new-units
)
1022 (when (setq math-cu-unit-list
(math-decompose-units math-cu-new-units
))
1023 (setq math-cu-new-units
(nth 2 (car math-cu-unit-list
))))
1024 (when (eq (car-safe expr
) '+)
1025 (setq expr
(math-simplify-units expr
)))
1026 (if (math-units-in-expr-p expr t
)
1027 (math-convert-units-rec expr
)
1028 (math-apply-units (math-to-standard-units
1029 (list '/ expr math-cu-new-units
) nil
)
1030 math-cu-new-units math-cu-unit-list math-cu-pure
))))))
1032 (defun math-convert-units-rec (expr)
1033 (if (math-units-in-expr-p expr nil
)
1034 (math-apply-units (math-to-standard-units
1035 (list '/ expr math-cu-new-units
) nil
)
1036 math-cu-new-units math-cu-unit-list math-cu-pure
)
1037 (if (Math-primp expr
)
1040 (mapcar 'math-convert-units-rec
(cdr expr
))))))
1042 (defun math-convert-temperature (expr old new
&optional pure
)
1043 (let* ((units (math-single-units-in-expr-p expr
))
1045 (if (or (null units
)
1046 (equal (nth 1 old
) (car units
)))
1047 (math-check-unit-name old
)
1048 (error "Inconsistent temperature units"))
1050 (unew (math-check-unit-name new
)))
1051 (unless (and (consp unew
) (nth 3 unew
))
1052 (error "Not a valid temperature unit"))
1053 (unless (and (consp uold
) (nth 3 uold
))
1054 (error "Not a pure temperature expression"))
1055 (let ((v (car uold
)))
1056 (setq expr
(list '/ expr
(list 'var v
1057 (intern (concat "var-"
1058 (symbol-name v
)))))))
1059 (or (eq (nth 3 uold
) (nth 3 unew
))
1060 (cond ((eq (nth 3 uold
) 'K
)
1061 (setq expr
(list '- expr
'(/ 27315 100)))
1062 (if (eq (nth 3 unew
) 'F
)
1063 (setq expr
(list '+ (list '* expr
'(/ 9 5)) 32))))
1064 ((eq (nth 3 uold
) 'C
)
1065 (if (eq (nth 3 unew
) 'F
)
1066 (setq expr
(list '+ (list '* expr
'(/ 9 5)) 32))
1067 (setq expr
(list '+ expr
'(/ 27315 100)))))
1069 (setq expr
(list '* (list '- expr
32) '(/ 5 9)))
1070 (if (eq (nth 3 unew
) 'K
)
1071 (setq expr
(list '+ expr
'(/ 27315 100)))))))
1074 (list '* expr new
))))
1078 (defun math-simplify-units (a)
1079 (let ((math-simplifying-units t
)
1080 (calc-matrix-mode 'scalar
))
1082 (defalias 'calcFunc-usimplify
'math-simplify-units
)
1084 ;; The function created by math-defsimplify uses the variable
1085 ;; math-simplify-expr, and so is used by functions in math-defsimplify
1086 (defvar math-simplify-expr
)
1088 (math-defsimplify (+ -
)
1089 (and math-simplifying-units
1090 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1091 (let* ((units (math-extract-units (nth 1 math-simplify-expr
)))
1092 (ratio (math-simplify (math-to-standard-units
1093 (list '/ (nth 2 math-simplify-expr
) units
) nil
))))
1094 (if (math-units-in-expr-p ratio nil
)
1096 (calc-record-why "*Inconsistent units" math-simplify-expr
)
1098 (list '* (math-add (math-remove-units (nth 1 math-simplify-expr
))
1099 (if (eq (car math-simplify-expr
) '-
)
1100 (math-neg ratio
) ratio
))
1104 (math-simplify-units-prod))
1106 (defun math-simplify-units-prod ()
1107 (and math-simplifying-units
1108 calc-autorange-units
1109 (Math-realp (nth 1 math-simplify-expr
))
1110 (let* ((num (math-float (nth 1 math-simplify-expr
)))
1111 (xpon (calcFunc-xpon num
))
1112 (unitp (cdr (cdr math-simplify-expr
)))
1114 (pow (if (eq (car math-simplify-expr
) '*) 1 -
1))
1116 (and (eq (car-safe unit
) '*)
1117 (setq unitp
(cdr unit
)
1119 (and (eq (car-safe unit
) '^
)
1120 (integerp (nth 2 unit
))
1121 (setq pow
(* pow
(nth 2 unit
))
1124 (and (setq u
(math-check-unit-name unit
))
1127 (>= xpon
(if (eq (car u
) 'm
) 1 3)))
1130 (reverse math-unit-prefixes
)
1131 math-unit-prefixes
))
1134 (or (eq (car u
) (nth 1 unit
))
1139 math-unit-prefixes
))))))
1140 (setq xpon
(+ xpon uxpon
))
1142 (or (memq (car (car p
)) '(?d ?D ?h ?H
))
1143 (and (eq (car (car p
)) ?c
)
1144 (not (eq (car u
) 'm
)))
1145 (< xpon
(setq pxpon
(* (nth 2 (nth 1 (car p
)))
1148 (setq pname
(math-build-var-name
1149 (if (eq (car (car p
)) 0)
1151 (concat (char-to-string
1155 (and (/= (car (car p
)) 0)
1157 math-units-table
)))))
1161 (or (not (eq p pref
))
1162 (< xpon
(+ pxpon
(* (math-abs pow
) 3))))
1164 (setcar (cdr math-simplify-expr
)
1165 (let ((calc-prefer-frac nil
))
1166 (calcFunc-scf (nth 1 math-simplify-expr
)
1168 (setcar unitp pname
)
1169 math-simplify-expr
)))))))
1171 (defvar math-try-cancel-units
)
1174 (and math-simplifying-units
1175 (let ((np (cdr math-simplify-expr
))
1176 (math-try-cancel-units 0)
1178 (setq n
(if (eq (car-safe (nth 2 math-simplify-expr
)) '*)
1179 (cdr (nth 2 math-simplify-expr
))
1180 (nthcdr 2 math-simplify-expr
)))
1181 (if (math-realp (car n
))
1183 (setcar (cdr math-simplify-expr
) (math-mul (nth 1 math-simplify-expr
)
1184 (let ((calc-prefer-frac nil
))
1185 (math-div 1 (car n
)))))
1187 (while (eq (car-safe (setq n
(car np
))) '*)
1188 (math-simplify-units-divisor (cdr n
) (cdr (cdr math-simplify-expr
)))
1189 (setq np
(cdr (cdr n
))))
1190 (math-simplify-units-divisor np
(cdr (cdr math-simplify-expr
)))
1191 (if (eq math-try-cancel-units
0)
1192 (let* ((math-simplifying-units nil
)
1193 (base (math-simplify
1194 (math-to-standard-units math-simplify-expr nil
))))
1195 (if (Math-numberp base
)
1196 (setq math-simplify-expr base
))))
1197 (if (eq (car-safe math-simplify-expr
) '/)
1198 (math-simplify-units-prod))
1199 math-simplify-expr
)))
1201 (defun math-simplify-units-divisor (np dp
)
1204 (while (eq (car-safe (setq d
(car dp
))) '*)
1205 (when (setq temp
(math-simplify-units-quotient n
(nth 1 d
)))
1206 (setcar np
(setq n temp
))
1208 (setq dp
(cdr (cdr d
))))
1209 (when (setq temp
(math-simplify-units-quotient n d
))
1210 (setcar np
(setq n temp
))
1213 ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.
1214 (defun math-simplify-units-quotient (n d
)
1217 (when (and (eq (car-safe n
) '^
)
1218 (integerp (nth 2 n
)))
1219 (setq pow1
(nth 2 n
) n
(nth 1 n
)))
1220 (when (and (eq (car-safe d
) '^
)
1221 (integerp (nth 2 d
)))
1222 (setq pow2
(nth 2 d
) d
(nth 1 d
)))
1223 (let ((un (math-check-unit-name n
))
1224 (ud (math-check-unit-name d
)))
1226 (if (and (equal (nth 4 un
) (nth 4 ud
))
1229 (math-to-standard-units (list '/ n d
) nil
)
1230 (list '^
(math-to-standard-units (list '/ n d
) nil
) pow1
))
1237 (and (eq (car (car un
)) (car (car ud1
)))
1238 (setq math-try-cancel-units
1239 (+ math-try-cancel-units
1240 (- (* (cdr (car un
)) pow1
)
1241 (* (cdr (car ud
)) pow2
)))))
1242 (setq ud1
(cdr ud1
)))
1247 (and math-simplifying-units
1248 (math-realp (nth 2 math-simplify-expr
))
1249 (if (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1250 (list (car (nth 1 math-simplify-expr
))
1251 (list '^
(nth 1 (nth 1 math-simplify-expr
))
1252 (nth 2 math-simplify-expr
))
1253 (list '^
(nth 2 (nth 1 math-simplify-expr
))
1254 (nth 2 math-simplify-expr
)))
1255 (math-simplify-units-pow (nth 1 math-simplify-expr
)
1256 (nth 2 math-simplify-expr
)))))
1258 (math-defsimplify calcFunc-sqrt
1259 (and math-simplifying-units
1260 (if (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1261 (list (car (nth 1 math-simplify-expr
))
1262 (list 'calcFunc-sqrt
(nth 1 (nth 1 math-simplify-expr
)))
1263 (list 'calcFunc-sqrt
(nth 2 (nth 1 math-simplify-expr
))))
1264 (math-simplify-units-pow (nth 1 math-simplify-expr
) '(frac 1 2)))))
1266 (math-defsimplify (calcFunc-floor
1276 (and math-simplifying-units
1277 (= (length math-simplify-expr
) 2)
1278 (if (math-only-units-in-expr-p (nth 1 math-simplify-expr
))
1279 (nth 1 math-simplify-expr
)
1280 (if (and (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1281 (or (math-only-units-in-expr-p
1282 (nth 1 (nth 1 math-simplify-expr
)))
1283 (math-only-units-in-expr-p
1284 (nth 2 (nth 1 math-simplify-expr
)))))
1285 (list (car (nth 1 math-simplify-expr
))
1286 (cons (car math-simplify-expr
)
1287 (cons (nth 1 (nth 1 math-simplify-expr
))
1288 (cdr (cdr math-simplify-expr
))))
1289 (cons (car math-simplify-expr
)
1290 (cons (nth 2 (nth 1 math-simplify-expr
))
1291 (cdr (cdr math-simplify-expr
)))))))))
1293 (defun math-simplify-units-pow (a pow
)
1294 (if (and (eq (car-safe a
) '^
)
1295 (math-check-unit-name (nth 1 a
))
1296 (math-realp (nth 2 a
)))
1297 (list '^
(nth 1 a
) (math-mul pow
(nth 2 a
)))
1298 (let* ((u (math-check-unit-name a
))
1299 (pf (math-to-simple-fraction pow
))
1300 (d (and (eq (car-safe pf
) 'frac
) (nth 2 pf
))))
1302 (math-units-are-multiple u d
)
1303 (list '^
(math-to-standard-units a nil
) pow
)))))
1306 (defun math-units-are-multiple (u n
)
1308 (while (and u
(= (%
(cdr (car u
)) n
) 0))
1312 (math-defsimplify calcFunc-sin
1313 (and math-simplifying-units
1314 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1315 (let ((rad (math-simplify-units
1317 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1318 (calc-angle-mode 'rad
))
1319 (and (eq (car-safe rad
) '*)
1320 (math-realp (nth 1 rad
))
1321 (eq (car-safe (nth 2 rad
)) 'var
)
1322 (eq (nth 1 (nth 2 rad
)) 'rad
)
1323 (list 'calcFunc-sin
(nth 1 rad
))))))
1325 (math-defsimplify calcFunc-cos
1326 (and math-simplifying-units
1327 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1328 (let ((rad (math-simplify-units
1330 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1331 (calc-angle-mode 'rad
))
1332 (and (eq (car-safe rad
) '*)
1333 (math-realp (nth 1 rad
))
1334 (eq (car-safe (nth 2 rad
)) 'var
)
1335 (eq (nth 1 (nth 2 rad
)) 'rad
)
1336 (list 'calcFunc-cos
(nth 1 rad
))))))
1338 (math-defsimplify calcFunc-tan
1339 (and math-simplifying-units
1340 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1341 (let ((rad (math-simplify-units
1343 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1344 (calc-angle-mode 'rad
))
1345 (and (eq (car-safe rad
) '*)
1346 (math-realp (nth 1 rad
))
1347 (eq (car-safe (nth 2 rad
)) 'var
)
1348 (eq (nth 1 (nth 2 rad
)) 'rad
)
1349 (list 'calcFunc-tan
(nth 1 rad
))))))
1351 (math-defsimplify calcFunc-sec
1352 (and math-simplifying-units
1353 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1354 (let ((rad (math-simplify-units
1356 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1357 (calc-angle-mode 'rad
))
1358 (and (eq (car-safe rad
) '*)
1359 (math-realp (nth 1 rad
))
1360 (eq (car-safe (nth 2 rad
)) 'var
)
1361 (eq (nth 1 (nth 2 rad
)) 'rad
)
1362 (list 'calcFunc-sec
(nth 1 rad
))))))
1364 (math-defsimplify calcFunc-csc
1365 (and math-simplifying-units
1366 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1367 (let ((rad (math-simplify-units
1369 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1370 (calc-angle-mode 'rad
))
1371 (and (eq (car-safe rad
) '*)
1372 (math-realp (nth 1 rad
))
1373 (eq (car-safe (nth 2 rad
)) 'var
)
1374 (eq (nth 1 (nth 2 rad
)) 'rad
)
1375 (list 'calcFunc-csc
(nth 1 rad
))))))
1377 (math-defsimplify calcFunc-cot
1378 (and math-simplifying-units
1379 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1380 (let ((rad (math-simplify-units
1382 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1383 (calc-angle-mode 'rad
))
1384 (and (eq (car-safe rad
) '*)
1385 (math-realp (nth 1 rad
))
1386 (eq (car-safe (nth 2 rad
)) 'var
)
1387 (eq (nth 1 (nth 2 rad
)) 'rad
)
1388 (list 'calcFunc-cot
(nth 1 rad
))))))
1391 (defun math-remove-units (expr)
1392 (if (math-check-unit-name expr
)
1394 (if (Math-primp expr
)
1397 (mapcar 'math-remove-units
(cdr expr
))))))
1399 (defun math-extract-units (expr)
1400 (if (memq (car-safe expr
) '(* /))
1402 (mapcar 'math-extract-units
(cdr expr
)))
1403 (if (math-check-unit-name expr
) expr
1)))
1405 (defun math-build-units-table-buffer (enter-buffer)
1406 (if (not (and math-units-table math-units-table-buffer-valid
1407 (get-buffer "*Units Table*")))
1408 (let ((buf (get-buffer-create "*Units Table*"))
1409 (uptr (math-build-units-table))
1410 (calc-language (if (eq calc-language
'big
) nil calc-language
))
1411 (calc-float-format '(float 0))
1412 (calc-group-digits nil
)
1413 (calc-number-radix 10)
1414 (calc-point-char ".")
1418 (message "Formatting units table...")
1420 (let ((inhibit-read-only t
))
1422 (insert "Calculator Units Table:\n\n")
1423 (insert "Unit Type Definition Description\n\n")
1427 (when (eq (car u
) 'm
)
1429 (setq shadowed
(and std
(assq (car u
) math-additional-units
)))
1432 (eq (aref name
0) ?\
*))
1433 (unless (eq uptr math-units-table
)
1435 (setq name
(substring name
1)))
1437 (and shadowed
(insert "("))
1438 (insert (symbol-name (car u
)))
1439 (and shadowed
(insert ")"))
1443 (insert (symbol-name (nth 3 u
))))
1449 (and shadowed
(insert "("))
1451 (insert (math-format-value (nth 1 u
) 80))
1452 (insert (symbol-name (car u
))))
1453 (and shadowed
(insert ")"))
1459 (insert " (redefined above)")
1461 (insert " (base unit)")))
1463 (setq uptr
(cdr uptr
)))
1464 (insert "\n\nUnit Prefix Table:\n\n")
1465 (setq uptr math-unit-prefixes
)
1468 (insert " " (char-to-string (car u
)))
1469 (if (equal (nth 1 u
) (nth 1 (nth 1 uptr
)))
1470 (insert " " (char-to-string (car (car (setq uptr
(cdr uptr
)))))
1473 (insert "10^" (int-to-string (nth 2 (nth 1 u
))))
1475 (insert " " (nth 2 u
) "\n")
1476 (while (eq (car (car (setq uptr
(cdr uptr
)))) 0)))
1479 (message "Formatting units table...done"))
1480 (setq math-units-table-buffer-valid t
)
1481 (let ((oldbuf (current-buffer)))
1483 (goto-char (point-min))
1484 (set-buffer oldbuf
))
1487 (display-buffer buf
)))
1489 (pop-to-buffer (get-buffer "*Units Table*"))
1490 (display-buffer (get-buffer "*Units Table*")))))
1492 (provide 'calc-units
)
1495 ;; coding: iso-latin-1
1498 ;; arch-tag: e993314f-3adc-4191-be61-4ef8874881c4
1499 ;;; calc-units.el ends here