1 ;;; calc-units.el --- unit conversion functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <belanger@truman.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
39 ;;; Units table last updated 9-Jan-91 by Ulrich Mueller (ulm@vsnhd1.cern.ch)
40 ;;; with some additions by Przemek Klosowski (przemek@rrdstrad.nist.gov)
41 ;;; Updated April 2002 by Jochen Küpper
43 ;;; for CODATA 1998 see one of
44 ;;; - Journal of Physical and Chemical Reference Data, 28(6), 1713-1852, 1999.
45 ;;; - Reviews of Modern Physics, 72(2), 351-495, 2000.
46 ;;; for CODATA 2005 see
47 ;;; - http://physics.nist.gov/cuu/Constants/index.html
49 (defvar math-standard-units
52 ( in
"2.54 cm" "Inch" )
55 ( mi
"5280 ft" "Mile" )
56 ( au
"149597870691 m" "Astronomical Unit" ) ;; NASA JPL (http://neo.jpl.nasa.gov/glossary/au.html)
57 ( lyr
"9460536207068016 m" "Light Year" )
58 ( pc
"206264.80625 au" "Parsec" )
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
"1e-10 m" "Angstrom" )
65 ( mfi
"mi+ft+in" "Miles + feet + inches" )
67 ( texpt
"in/72.27" "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/66536 texpt" "Scaled TeX point" )
75 ( hect
"10000 m^2" "*Hectare" )
77 ( acre
"mi^2 / 640" "Acre" )
78 ( b
"1e-28 m^2" "Barn" )
81 ( L
"1e-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 ( tsp
"4.92892159375 ml" "Teaspoon" )
91 ( vol
"tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" )
92 ( galC
"4.54609 L" "Canadian Gallon" )
93 ( galUK
"4.546092 L" "UK Gallon" )
98 ( min
"60 s" "Minute" )
99 ( hr
"60 min" "Hour" )
100 ( day
"24 hr" "Day" )
101 ( wk
"7 day" "Week" )
102 ( hms
"wk+day+hr+min+s" "Hours, minutes, seconds" )
103 ( yr
"365.25 day" "Year" )
107 ( mph
"mi/hr" "*Miles per hour" )
108 ( kph
"km/hr" "Kilometers per hour" )
109 ( knot
"nmi/hr" "Knot" )
110 ( c
"299792458 m/s" "Speed of light" ) ;;; CODATA 2005
113 ( ga
"9.80665 m/s^2" "*\"g\" acceleration" ) ;; CODATA 2005
117 ( lb
"16 oz" "Pound (mass)" )
118 ( oz
"28.349523125 g" "Ounce (mass)" )
119 ( ton
"2000 lb" "Ton" )
120 ( tpo
"ton+lb+oz" "Tons + pounds + ounces (mass)" )
121 ( t
"1000 kg" "Metric ton" )
122 ( tonUK
"1016.0469088 kg" "UK ton" )
123 ( lbt
"12 ozt" "Troy pound" )
124 ( ozt
"31.103475 g" "Troy ounce" )
125 ( ct
".2 g" "Carat" )
126 ( u
"1.66053886e-27 kg" "Unified atomic mass" ) ;; CODATA 2005
129 ( N
"m kg/s^2" "*Newton" )
130 ( dyn
"1e-5 N" "Dyne" )
131 ( gf
"ga g" "Gram (force)" )
132 ( lbf
"4.44822161526 N" "Pound (force)" )
133 ( kip
"1000 lbf" "Kilopound (force)" )
134 ( pdl
"0.138255 N" "Poundal" )
138 ( erg
"1e-7 J" "Erg" )
139 ( cal
"4.1868 J" "International Table Calorie" )
140 ( Btu
"1055.05585262 J" "International Table Btu" )
141 ( eV
"ech V" "Electron volt" )
142 ( ev
"eV" "Electron volt" )
143 ( therm
"105506000 J" "EEC therm" )
144 ( invcm
"h c/cm" "Energy in inverse centimeters" )
145 ( Kayser
"invcm" "Kayser (inverse centimeter energy)" )
146 ( men
"100/invcm" "Inverse energy in meters" )
147 ( Hzen
"h Hz" "Energy in Hertz")
148 ( Ken
"k K" "Energy in Kelvins")
149 ( Wh
"W hr" "Watt hour")
150 ( Ws
"W s" "Watt second")
154 ( hp
"745.7 W" "Horsepower" )
157 ( K nil
"*Degree Kelvin" K
)
158 ( dK
"K" "Degree Kelvin" K
)
159 ( degK
"K" "Degree Kelvin" K
)
160 ( dC
"K" "Degree Celsius" C
)
161 ( degC
"K" "Degree Celsius" C
)
162 ( dF
"(5/9) K" "Degree Fahrenheit" F
)
163 ( degF
"(5/9) K" "Degree Fahrenheit" F
)
166 ( Pa
"N/m^2" "*Pascal" )
167 ( bar
"1e5 Pa" "Bar" )
168 ( atm
"101325 Pa" "Standard atmosphere" ) ;; CODATA 2005
169 ( Torr
" 1.333224e2 Pa" "Torr" ) ;; NIST (http://physics.nist.gov/Pubs/SP811/appenB9.html)
170 ( mHg
"1000 Torr" "Meter of mercury" )
171 ( inHg
"25.4 mmHg" "Inch of mercury" )
172 ( inH2O
"2.490889e2 Pa" "Inch of water" ) ;; NIST (http://physics.nist.gov/Pubs/SP811/appenB9.html)
173 ( psi
"6894.75729317 Pa" "Pound per square inch" )
176 ( P
"0.1 Pa s" "*Poise" )
177 ( St
"1e-4 m^2/s" "Stokes" )
181 ( C
"A s" "Coulomb" )
182 ( Fdy
"ech Nav" "Faraday" )
183 ( e
"1.60217653e-19 C" "Elementary charge" ) ;; CODATA 2005
184 ( ech
"1.60217653e-19 C" "Elementary charge" ) ;; CODATA 2005
188 ( S
"A/V" "Siemens" )
191 ( T
"Wb/m^2" "Tesla" )
192 ( Gs
"1e-4 T" "Gauss" )
195 ;; Luminous intensity
196 ( cd nil
"*Candela" )
197 ( sb
"1e4 cd/m^2" "Stilb" )
198 ( lm
"cd sr" "Lumen" )
199 ( lx
"lm/m^2" "Lux" )
200 ( ph
"1e4 lx" "Phot" )
201 ( fc
"10.76391 lx" "Footcandle" ) ;; NIST (http://physics.nist.gov/Pubs/SP811/appenB9.html)
202 ( lam
"1e4 lm/m^2" "Lambert" )
203 ( flam
"3.426259 cd/m^2" "Footlambert" ) ;; NIST (http://physics.nist.gov/Pubs/SP811/appenB9.html)
206 ( Bq
"1/s" "*Becquerel" )
207 ( Ci
"3.7e10 Bq" "Curie" )
209 ( Sv
"Gy" "Sievert" )
210 ( R
"2.58e-4 C/kg" "Roentgen" )
211 ( rd
".01 Gy" "Rad" )
214 ;; Amount of substance
218 ( rad nil
"*Radian" )
219 ( circ
"2 pi rad" "Full circle" )
220 ( rev
"circ" "Full revolution" )
221 ( deg
"circ/360" "Degree" )
222 ( arcmin
"deg/60" "Arc minute" )
223 ( arcsec
"arcmin/60" "Arc second" )
224 ( grad
"circ/400" "Grade" )
225 ( rpm
"rev/min" "Revolutions per minute" )
228 ( sr nil
"*Steradian" )
230 ;; Other physical quantities
231 ( h
"6.6260693e-34 J s" "*Planck's constant" ) ;; CODATA 2005
232 ( hbar
"h / 2 pi" "Planck's constant" )
233 ( mu0
"4 pi 1e-7 H/m" "Permeability of vacuum" )
234 ( G
"6.6742e-11 m^3/kg^1/s^2" "Gravitational constant" ) ;; CODATA 2005
235 ( Nav
"6.02214115e23 / mol" "Avagadro's constant" ) ;; CODATA 2005
236 ( me
"9.1093826e-31 kg" "Electron rest mass" ) ;; CODATA 2005
237 ( mp
"1.67262171e-27 kg" "Proton rest mass" ) ;; CODATA 2005
238 ( mn
"1.67492728e-27 kg" "Neutron rest mass" ) ;; CODATA 2005
239 ( mmu
"1.88353140e-28 kg" "Muon rest mass" ) ;; CODATA 2005
240 ( Ryd
"10973731.568525 /m" "Rydberg's constant" ) ;; CODATA 2005
241 ( k
"1.3806505e-23 J/K" "Boltzmann's constant" ) ;; CODATA 2005
242 ( alpha
"7.297352568e-3" "Fine structure constant" ) ;; CODATA 2005
243 ( muB
"927.400949e-26 J/T" "Bohr magneton" ) ;; CODATA 2005
244 ( muN
"5.05078343e-27 J/T" "Nuclear magneton" ) ;; CODATA 2005
245 ( mue
"-928.476412e-26 J/T" "Electron magnetic moment" ) ;; CODATA 2005
246 ( mup
"1.41060671e-26 J/T" "Proton magnetic moment" ) ;; CODATA 2005
247 ( R0
"8.314472 J/mol/K" "Molar gas constant" ) ;; CODATA 2005
248 ( V0
"22.710981e-3 m^3/mol" "Standard volume of ideal gas" )))
251 (defvar math-additional-units nil
252 "*Additional units table for user-defined units.
253 Must be formatted like math-standard-units.
254 If this is changed, be sure to set math-units-table to nil to ensure
255 that the combined units table will be rebuilt.")
257 (defvar math-unit-prefixes
258 '( ( ?Y
(float 1 24) "Yotta" )
259 ( ?Z
(float 1 21) "Zetta" )
260 ( ?E
(float 1 18) "Exa" )
261 ( ?P
(float 1 15) "Peta" )
262 ( ?T
(float 1 12) "Tera" )
263 ( ?G
(float 1 9) "Giga" )
264 ( ?M
(float 1 6) "Mega" )
265 ( ?k
(float 1 3) "Kilo" )
266 ( ?K
(float 1 3) "Kilo" )
267 ( ?h
(float 1 2) "Hecto" )
268 ( ?H
(float 1 2) "Hecto" )
269 ( ?D
(float 1 1) "Deka" )
270 ( 0 (float 1 0) nil
)
271 ( ?d
(float 1 -
1) "Deci" )
272 ( ?c
(float 1 -
2) "Centi" )
273 ( ?m
(float 1 -
3) "Milli" )
274 ( ?u
(float 1 -
6) "Micro" )
275 ( ?n
(float 1 -
9) "Nano" )
276 ( ?p
(float 1 -
12) "Pico" )
277 ( ?f
(float 1 -
15) "Femto" )
278 ( ?a
(float 1 -
18) "Atto" )
279 ( ?z
(float 1 -
21) "zepto" )
280 ( ?y
(float 1 -
24) "yocto" )))
282 (defvar math-standard-units-systems
284 ( si
( ( g
'(* (var kg var-kg
) (float 1 -
3)) ) ) )
285 ( mks
( ( g
'(* (var kg var-kg
) (float 1 -
3)) ) ) )
286 ( cgs
( ( m
'(* (var cm var-cm
) 100 ) ) ) )))
288 (defvar math-units-table nil
289 "Internal units table derived from math-defined-units.
290 Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
292 (defvar math-units-table-buffer-valid nil
)
296 (defun calc-base-units ()
299 (let ((calc-autorange-units nil
))
300 (calc-enter-result 1 "bsun" (math-simplify-units
301 (math-to-standard-units (calc-top-n 1)
304 (defun calc-quick-units ()
307 (let* ((num (- last-command-char ?
0))
308 (pos (if (= num
0) 10 num
))
309 (units (calc-var-value 'var-Units
))
310 (expr (calc-top-n 1)))
311 (unless (and (>= num
0) (<= num
9))
312 (error "Bad unit number"))
313 (unless (math-vectorp units
)
314 (error "No \"quick units\" are defined"))
315 (unless (< pos
(length units
))
316 (error "Unit number %d not defined" pos
))
317 (if (math-units-in-expr-p expr nil
)
318 (calc-enter-result 1 (format "cun%d" num
)
319 (math-convert-units expr
(nth pos units
)))
320 (calc-enter-result 1 (format "*un%d" num
)
322 (math-mul expr
(nth pos units
))))))))
324 (defun calc-convert-units (&optional old-units new-units
)
327 (let ((expr (calc-top-n 1))
331 (unless (math-units-in-expr-p expr t
)
332 (let ((uold (or old-units
334 (setq uoldname
(read-string "Old units: "))
335 (if (equal uoldname
"")
339 (if (string-match "\\` */" uoldname
)
340 (setq uoldname
(concat "1" uoldname
)))
341 (math-read-expr uoldname
))))))
342 (when (eq (car-safe uold
) 'error
)
343 (error "Bad format in units expression: %s" (nth 1 uold
)))
344 (setq expr
(math-mul expr uold
))))
346 (setq new-units
(read-string (if uoldname
347 (concat "Old units: "
351 (when (string-match "\\` */" new-units
)
352 (setq new-units
(concat "1" new-units
)))
353 (setq units
(math-read-expr new-units
))
354 (when (eq (car-safe units
) 'error
)
355 (error "Bad format in units expression: %s" (nth 2 units
)))
356 (let ((unew (math-units-in-expr-p units t
))
357 (std (and (eq (car-safe units
) 'var
)
358 (assq (nth 1 units
) math-standard-units-systems
))))
360 (calc-enter-result 1 "cvun" (math-simplify-units
361 (math-to-standard-units expr
364 (error "No units specified"))
365 (calc-enter-result 1 "cvun"
368 (and uoldname
(not (equal uoldname
"1"))))))))))
370 (defun calc-autorange-units (arg)
373 (calc-change-mode 'calc-autorange-units arg nil t
)
374 (message (if calc-autorange-units
375 "Adjusting target unit prefix automatically"
376 "Using target units exactly"))))
378 (defun calc-convert-temperature (&optional old-units new-units
)
381 (let ((expr (calc-top-n 1))
385 (setq uold
(or old-units
386 (let ((units (math-single-units-in-expr-p expr
)))
389 (list 'var
(car units
)
390 (intern (concat "var-"
393 (error "Not a pure temperature expression"))
395 (setq uoldname
(read-string
396 "Old temperature units: ")))))))
397 (when (eq (car-safe uold
) 'error
)
398 (error "Bad format in units expression: %s" (nth 2 uold
)))
399 (or (math-units-in-expr-p expr nil
)
400 (setq expr
(math-mul expr uold
)))
401 (setq unew
(or new-units
403 (read-string (if uoldname
404 (concat "Old temperature units: "
407 "New temperature units: ")))))
408 (when (eq (car-safe unew
) 'error
)
409 (error "Bad format in units expression: %s" (nth 2 unew
)))
410 (calc-enter-result 1 "cvtm" (math-simplify-units
411 (math-convert-temperature expr uold unew
414 (defun calc-remove-units ()
417 (calc-enter-result 1 "rmun" (math-simplify-units
418 (math-remove-units (calc-top-n 1))))))
420 (defun calc-extract-units ()
423 (calc-enter-result 1 "rmun" (math-simplify-units
424 (math-extract-units (calc-top-n 1))))))
426 ;; The variables calc-num-units and calc-den-units are local to
427 ;; calc-explain-units, but are used by calc-explain-units-rec,
428 ;; which is called by calc-explain-units.
429 (defvar calc-num-units
)
430 (defvar calc-den-units
)
432 (defun calc-explain-units ()
435 (let ((calc-num-units nil
)
436 (calc-den-units nil
))
437 (calc-explain-units-rec (calc-top-n 1) 1)
438 (and calc-den-units
(string-match "^[^(].* .*[^)]$" calc-den-units
)
439 (setq calc-den-units
(concat "(" calc-den-units
")")))
442 (message "%s per %s" calc-num-units calc-den-units
)
443 (message "%s" calc-num-units
))
445 (message "1 per %s" calc-den-units
)
446 (message "No units in expression"))))))
448 (defun calc-explain-units-rec (expr pow
)
449 (let ((u (math-check-unit-name expr
))
451 (if (and u
(not (math-zerop pow
)))
452 (let ((name (or (nth 2 u
) (symbol-name (car u
)))))
453 (if (eq (aref name
0) ?\
*)
454 (setq name
(substring name
1)))
455 (if (string-match "[^a-zA-Z0-9']" name
)
456 (if (string-match "^[a-zA-Z0-9' ()]*$" name
)
457 (while (setq pos
(string-match "[ ()]" name
))
458 (setq name
(concat (substring name
0 pos
)
459 (if (eq (aref name pos
) 32) "-" "")
460 (substring name
(1+ pos
)))))
461 (setq name
(concat "(" name
")"))))
462 (or (eq (nth 1 expr
) (car u
))
463 (setq name
(concat (nth 2 (assq (aref (symbol-name
466 (if (and (string-match "[^a-zA-Z0-9']" name
)
467 (not (memq (car u
) '(mHg gf
))))
470 (cond ((or (math-equal-int pow
1)
471 (math-equal-int pow -
1)))
472 ((or (math-equal-int pow
2)
473 (math-equal-int pow -
2))
474 (if (equal (nth 4 u
) '((m .
1)))
475 (setq name
(concat "Square-" name
))
476 (setq name
(concat name
"-squared"))))
477 ((or (math-equal-int pow
3)
478 (math-equal-int pow -
3))
479 (if (equal (nth 4 u
) '((m .
1)))
480 (setq name
(concat "Cubic-" name
))
481 (setq name
(concat name
"-cubed"))))
483 (setq name
(concat name
"^"
484 (math-format-number (math-abs pow
))))))
486 (setq calc-num-units
(if calc-num-units
487 (concat calc-num-units
" " name
)
489 (setq calc-den-units
(if calc-den-units
490 (concat calc-den-units
" " name
)
492 (cond ((eq (car-safe expr
) '*)
493 (calc-explain-units-rec (nth 1 expr
) pow
)
494 (calc-explain-units-rec (nth 2 expr
) pow
))
495 ((eq (car-safe expr
) '/)
496 (calc-explain-units-rec (nth 1 expr
) pow
)
497 (calc-explain-units-rec (nth 2 expr
) (- pow
)))
498 ((memq (car-safe expr
) '(neg + -
))
499 (calc-explain-units-rec (nth 1 expr
) pow
))
500 ((and (eq (car-safe expr
) '^
)
501 (math-realp (nth 2 expr
)))
502 (calc-explain-units-rec (nth 1 expr
)
503 (math-mul pow
(nth 2 expr
))))))))
505 (defun calc-simplify-units ()
508 (calc-with-default-simplification
509 (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1))))))
511 (defun calc-view-units-table (n)
513 (and n
(setq math-units-table-buffer-valid nil
))
514 (let ((win (get-buffer-window "*Units Table*")))
517 math-units-table-buffer-valid
)
519 (bury-buffer (window-buffer win
))
520 (let ((curwin (selected-window)))
522 (switch-to-buffer nil
)
523 (select-window curwin
)))
524 (math-build-units-table-buffer nil
))))
526 (defun calc-enter-units-table (n)
528 (and n
(setq math-units-table-buffer-valid nil
))
529 (math-build-units-table-buffer t
)
530 (message "%s" (substitute-command-keys "Type \\[calc] to return to the Calculator")))
532 (defun calc-define-unit (uname desc
)
533 (interactive "SDefine unit name: \nsDescription: ")
535 (let ((form (calc-top-n 1))
536 (unit (assq uname math-additional-units
)))
538 (setq math-additional-units
539 (cons (setq unit
(list uname nil nil
))
540 math-additional-units
)
541 math-units-table nil
))
542 (setcar (cdr unit
) (and (not (and (eq (car-safe form
) 'var
)
543 (eq (nth 1 form
) uname
)))
544 (not (math-equal-int form
1))
545 (math-format-flat-expr form
0)))
546 (setcar (cdr (cdr unit
)) (and (not (equal desc
""))
548 (calc-invalidate-units-table))
550 (defun calc-undefine-unit (uname)
551 (interactive "SUndefine unit name: ")
553 (let ((unit (assq uname math-additional-units
)))
555 (if (assq uname math-standard-units
)
556 (error "\"%s\" is a predefined unit name" uname
)
557 (error "Unit name \"%s\" not found" uname
)))
558 (setq math-additional-units
(delq unit math-additional-units
)
559 math-units-table nil
)))
560 (calc-invalidate-units-table))
562 (defun calc-invalidate-units-table ()
563 (setq math-units-table nil
)
564 (let ((buf (get-buffer "*Units Table*")))
569 (goto-char (point-min))
570 (if (looking-at "Calculator Units Table")
571 (let ((inhibit-read-only t
))
572 (insert "(Obsolete) "))))))))
574 (defun calc-get-unit-definition (uname)
575 (interactive "SGet definition for unit: ")
577 (math-build-units-table)
578 (let ((unit (assq uname math-units-table
)))
580 (error "Unit name \"%s\" not found" uname
))
581 (let ((msg (nth 2 unit
)))
583 (if (string-match "^\\*" msg
)
584 (setq msg
(substring msg
1)))
585 (setq msg
(symbol-name uname
)))
588 (calc-enter-result 0 "ugdf" (nth 1 unit
))
589 (message "Derived unit: %s" msg
))
590 (calc-enter-result 0 "ugdf" (list 'var uname
593 (symbol-name uname
)))))
594 (message "Base unit: %s" msg
))))))
596 (defun calc-permanent-units ()
600 (set-buffer (find-file-noselect (substitute-in-file-name
601 calc-settings-file
)))
602 (goto-char (point-min))
603 (if (and (search-forward ";;; Custom units stored by Calc" nil t
)
607 (search-forward "\n;;; End of custom units" nil t
)))
611 (delete-region pos
(point)))
612 (goto-char (point-max))
615 (insert ";;; Custom units stored by Calc on " (current-time-string) "\n")
616 (if math-additional-units
618 (insert "(setq math-additional-units '(\n")
619 (let ((list math-additional-units
))
621 (insert " (" (symbol-name (car (car list
))) " "
622 (if (nth 1 (car list
))
623 (if (stringp (nth 1 (car list
)))
624 (prin1-to-string (nth 1 (car list
)))
625 (prin1-to-string (math-format-flat-expr
626 (nth 1 (car list
)) 0)))
629 (prin1-to-string (nth 2 (car list
)))
631 (setq list
(cdr list
))))
633 (insert ";;; (no custom units defined)\n"))
634 (insert ";;; End of custom units\n")
638 ;; The variable math-cu-unit-list is local to math-build-units-table,
639 ;; but is used by math-compare-unit-names, which is called (indirectly)
640 ;; by math-build-units-table.
641 ;; math-cu-unit-list is also local to math-convert-units, but is used
642 ;; by math-convert-units-rec, which is called by math-convert-units.
643 (defvar math-cu-unit-list
)
645 (defun math-build-units-table ()
647 (let* ((combined-units (append math-additional-units
648 math-standard-units
))
649 (math-cu-unit-list (mapcar 'car combined-units
))
651 (message "Building units table...")
652 (setq math-units-table-buffer-valid nil
)
653 (setq tab
(mapcar (function
657 (if (stringp (nth 1 x
))
658 (let ((exp (math-read-plain-expr
660 (if (eq (car-safe exp
) 'error
)
661 (error "Format error in definition of %s in units table: %s"
668 (list (cons (car x
) 1))))))
670 (let ((math-units-table tab
))
671 (mapcar 'math-find-base-units tab
))
672 (message "Building units table...done")
673 (setq math-units-table tab
))))
675 ;; The variables math-fbu-base and math-fbu-entry are local to
676 ;; math-find-base-units, but are used by math-find-base-units-rec,
677 ;; which is called by math-find-base-units.
678 (defvar math-fbu-base
)
679 (defvar math-fbu-entry
)
681 (defun math-find-base-units (math-fbu-entry)
682 (if (eq (nth 4 math-fbu-entry
) 'boom
)
683 (error "Circular definition involving unit %s" (car math-fbu-entry
)))
684 (or (nth 4 math-fbu-entry
)
686 (setcar (nthcdr 4 math-fbu-entry
) 'boom
)
687 (math-find-base-units-rec (nth 1 math-fbu-entry
) 1)
689 (error "Dimensionless definition for unit %s" (car math-fbu-entry
)))
690 (while (eq (cdr (car math-fbu-base
)) 0)
691 (setq math-fbu-base
(cdr math-fbu-base
)))
692 (let ((b math-fbu-base
))
694 (if (eq (cdr (car (cdr b
))) 0)
695 (setcdr b
(cdr (cdr b
)))
697 (setq math-fbu-base
(sort math-fbu-base
'math-compare-unit-names
))
698 (setcar (nthcdr 4 math-fbu-entry
) math-fbu-base
)
701 (defun math-compare-unit-names (a b
)
702 (memq (car b
) (cdr (memq (car a
) math-cu-unit-list
))))
704 (defun math-find-base-units-rec (expr pow
)
705 (let ((u (math-check-unit-name expr
)))
707 (let ((ulist (math-find-base-units u
)))
709 (let ((p (* (cdr (car ulist
)) pow
))
710 (old (assq (car (car ulist
)) math-fbu-base
)))
712 (setcdr old
(+ (cdr old
) p
))
714 (cons (cons (car (car ulist
)) p
) math-fbu-base
))))
715 (setq ulist
(cdr ulist
)))))
716 ((math-scalarp expr
))
717 ((and (eq (car expr
) '^
)
718 (integerp (nth 2 expr
)))
719 (math-find-base-units-rec (nth 1 expr
) (* pow
(nth 2 expr
))))
721 (math-find-base-units-rec (nth 1 expr
) pow
)
722 (math-find-base-units-rec (nth 2 expr
) pow
))
724 (math-find-base-units-rec (nth 1 expr
) pow
)
725 (math-find-base-units-rec (nth 2 expr
) (- pow
)))
726 ((eq (car expr
) 'neg
)
727 (math-find-base-units-rec (nth 1 expr
) pow
))
729 (math-find-base-units-rec (nth 1 expr
) pow
))
730 ((eq (car expr
) 'var
)
731 (or (eq (nth 1 expr
) 'pi
)
732 (error "Unknown name %s in defining expression for unit %s"
733 (nth 1 expr
) (car math-fbu-entry
))))
734 (t (error "Malformed defining expression for unit %s" (car math-fbu-entry
))))))
737 (defun math-units-in-expr-p (expr sub-exprs
)
739 (if (eq (car expr
) 'var
)
740 (math-check-unit-name expr
)
742 (memq (car expr
) '(* / ^
)))
743 (or (math-units-in-expr-p (nth 1 expr
) sub-exprs
)
744 (math-units-in-expr-p (nth 2 expr
) sub-exprs
))))))
746 (defun math-only-units-in-expr-p (expr)
748 (if (eq (car expr
) 'var
)
749 (math-check-unit-name expr
)
750 (if (memq (car expr
) '(* /))
751 (and (math-only-units-in-expr-p (nth 1 expr
))
752 (math-only-units-in-expr-p (nth 2 expr
)))
753 (and (eq (car expr
) '^
)
754 (and (math-only-units-in-expr-p (nth 1 expr
))
755 (math-realp (nth 2 expr
))))))))
757 (defun math-single-units-in-expr-p (expr)
758 (cond ((math-scalarp expr
) nil
)
759 ((eq (car expr
) 'var
)
760 (math-check-unit-name expr
))
762 (let ((u1 (math-single-units-in-expr-p (nth 1 expr
)))
763 (u2 (math-single-units-in-expr-p (nth 2 expr
))))
764 (or (and u1 u2
'wrong
)
768 (if (math-units-in-expr-p (nth 2 expr
) nil
)
770 (math-single-units-in-expr-p (nth 1 expr
))))
773 (defun math-check-unit-name (v)
774 (and (eq (car-safe v
) 'var
)
775 (or (assq (nth 1 v
) (or math-units-table
(math-build-units-table)))
776 (let ((name (symbol-name (nth 1 v
))))
777 (and (> (length name
) 1)
778 (assq (aref name
0) math-unit-prefixes
)
779 (or (assq (intern (substring name
1)) math-units-table
)
780 (and (eq (aref name
0) ?M
)
782 (eq (aref name
1) ?e
)
783 (eq (aref name
2) ?g
)
784 (assq (intern (substring name
3))
785 math-units-table
))))))))
787 ;; The variable math-which-standard is local to math-to-standard-units,
788 ;; but is used by math-to-standard-rec, which is called by
789 ;; math-to-standard-units.
790 (defvar math-which-standard
)
792 (defun math-to-standard-units (expr math-which-standard
)
793 (math-to-standard-rec expr
))
795 (defun math-to-standard-rec (expr)
796 (if (eq (car-safe expr
) 'var
)
797 (let ((u (math-check-unit-name expr
))
802 (setq expr
(math-to-standard-rec (nth 1 u
)))
803 (let ((st (assq (car u
) math-which-standard
)))
805 (setq expr
(nth 1 st
))
806 (setq expr
(list 'var
(car u
)
807 (intern (concat "var-"
813 (nth 1 (assq (aref (symbol-name base
) 0)
820 (if (Math-primp expr
)
823 (mapcar 'math-to-standard-rec
(cdr expr
))))))
825 (defun math-apply-units (expr units ulist
&optional pure
)
826 (setq expr
(math-simplify-units expr
))
830 (or (math-numberp expr
)
831 (error "Incompatible units"))
833 (setq value
(math-div expr
(nth 1 (car ulist
)))
834 value
(math-floor (let ((calc-internal-prec
835 (1- calc-internal-prec
)))
836 (math-normalize value
)))
837 new
(math-add new
(math-mul value
(car (car ulist
))))
838 expr
(math-sub expr
(math-mul value
(nth 1 (car ulist
))))
840 (math-add new
(math-mul (math-div expr
(nth 1 (car ulist
)))
844 (math-simplify-units (list '* expr units
)))))
846 (defvar math-decompose-units-cache nil
)
847 (defun math-decompose-units (units)
848 (let ((u (math-check-unit-name units
)))
849 (and u
(eq (car-safe (nth 1 u
)) '+)
850 (setq units
(nth 1 u
))))
851 (setq units
(calcFunc-expand units
))
852 (and (eq (car-safe units
) '+)
853 (let ((entry (list units calc-internal-prec calc-prefer-frac
)))
854 (or (equal entry
(car math-decompose-units-cache
))
858 (while (eq (car-safe utemp
) '+)
859 (setq ulist
(cons (math-decompose-unit-part (nth 2 utemp
))
861 utemp
(nth 1 utemp
)))
862 (setq ulist
(cons (math-decompose-unit-part utemp
) ulist
)
864 (while (setq utemp
(cdr utemp
))
865 (unless (equal (nth 2 (car utemp
)) (nth 2 (car ulist
)))
866 (error "Inconsistent units in sum")))
867 (setq math-decompose-units-cache
872 (not (Math-lessp (nth 1 x
)
874 (cdr math-decompose-units-cache
))))
876 (defun math-decompose-unit-part (unit)
878 (math-is-multiple (math-simplify-units (math-to-standard-units
882 ;; The variable math-fcu-u is local to math-find-compatible-unit,
883 ;; but is used by math-find-compatible-rec which is called by
884 ;; math-find-compatible-unit.
887 (defun math-find-compatible-unit (expr unit
)
888 (let ((math-fcu-u (math-check-unit-name unit
)))
890 (math-find-compatible-unit-rec expr
1))))
892 (defun math-find-compatible-unit-rec (expr pow
)
893 (cond ((eq (car-safe expr
) '*)
894 (or (math-find-compatible-unit-rec (nth 1 expr
) pow
)
895 (math-find-compatible-unit-rec (nth 2 expr
) pow
)))
896 ((eq (car-safe expr
) '/)
897 (or (math-find-compatible-unit-rec (nth 1 expr
) pow
)
898 (math-find-compatible-unit-rec (nth 2 expr
) (- pow
))))
899 ((and (eq (car-safe expr
) '^
)
900 (integerp (nth 2 expr
)))
901 (math-find-compatible-unit-rec (nth 1 expr
) (* pow
(nth 2 expr
))))
903 (let ((u2 (math-check-unit-name expr
)))
904 (if (equal (nth 4 math-fcu-u
) (nth 4 u2
))
907 ;; The variables math-cu-new-units and math-cu-pure are local to
908 ;; math-convert-units, but are used by math-convert-units-rec,
909 ;; which is called by math-convert-units.
910 (defvar math-cu-new-units
)
911 (defvar math-cu-pure
)
913 (defun math-convert-units (expr math-cu-new-units
&optional math-cu-pure
)
914 (if (eq (car-safe math-cu-new-units
) 'var
)
915 (let ((unew (assq (nth 1 math-cu-new-units
)
916 (math-build-units-table))))
917 (if (eq (car-safe (nth 1 unew
)) '+)
918 (setq math-cu-new-units
(nth 1 unew
)))))
919 (math-with-extra-prec 2
920 (let ((compat (and (not math-cu-pure
)
921 (math-find-compatible-unit expr math-cu-new-units
)))
922 (math-cu-unit-list nil
)
923 (math-combining-units nil
))
926 (math-mul (math-mul (math-simplify-units
927 (math-div expr
(math-pow (car compat
)
929 (math-pow math-cu-new-units
(cdr compat
)))
931 (math-to-standard-units
932 (math-pow (math-div (car compat
) math-cu-new-units
)
935 (when (setq math-cu-unit-list
(math-decompose-units math-cu-new-units
))
936 (setq math-cu-new-units
(nth 2 (car math-cu-unit-list
))))
937 (when (eq (car-safe expr
) '+)
938 (setq expr
(math-simplify-units expr
)))
939 (if (math-units-in-expr-p expr t
)
940 (math-convert-units-rec expr
)
941 (math-apply-units (math-to-standard-units
942 (list '/ expr math-cu-new-units
) nil
)
943 math-cu-new-units math-cu-unit-list math-cu-pure
))))))
945 (defun math-convert-units-rec (expr)
946 (if (math-units-in-expr-p expr nil
)
947 (math-apply-units (math-to-standard-units
948 (list '/ expr math-cu-new-units
) nil
)
949 math-cu-new-units math-cu-unit-list math-cu-pure
)
950 (if (Math-primp expr
)
953 (mapcar 'math-convert-units-rec
(cdr expr
))))))
955 (defun math-convert-temperature (expr old new
&optional pure
)
956 (let* ((units (math-single-units-in-expr-p expr
))
959 (equal (nth 1 old
) (car units
)))
960 (math-check-unit-name old
)
961 (error "Inconsistent temperature units"))
963 (unew (math-check-unit-name new
)))
964 (unless (and (consp unew
) (nth 3 unew
))
965 (error "Not a valid temperature unit"))
966 (unless (and (consp uold
) (nth 3 uold
))
967 (error "Not a pure temperature expression"))
968 (let ((v (car uold
)))
969 (setq expr
(list '/ expr
(list 'var v
970 (intern (concat "var-"
971 (symbol-name v
)))))))
972 (or (eq (nth 3 uold
) (nth 3 unew
))
973 (cond ((eq (nth 3 uold
) 'K
)
974 (setq expr
(list '- expr
'(float 27315 -
2)))
975 (if (eq (nth 3 unew
) 'F
)
976 (setq expr
(list '+ (list '* expr
'(frac 9 5)) 32))))
977 ((eq (nth 3 uold
) 'C
)
978 (if (eq (nth 3 unew
) 'F
)
979 (setq expr
(list '+ (list '* expr
'(frac 9 5)) 32))
980 (setq expr
(list '+ expr
'(float 27315 -
2)))))
982 (setq expr
(list '* (list '- expr
32) '(frac 5 9)))
983 (if (eq (nth 3 unew
) 'K
)
984 (setq expr
(list '+ expr
'(float 27315 -
2)))))))
987 (list '* expr new
))))
991 (defun math-simplify-units (a)
992 (let ((math-simplifying-units t
)
993 (calc-matrix-mode 'scalar
))
995 (defalias 'calcFunc-usimplify
'math-simplify-units
)
997 ;; The function created by math-defsimplify uses the variable
998 ;; math-simplify-expr, and so is used by functions in math-defsimplify
999 (defvar math-simplify-expr
)
1001 (math-defsimplify (+ -
)
1002 (and math-simplifying-units
1003 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1004 (let* ((units (math-extract-units (nth 1 math-simplify-expr
)))
1005 (ratio (math-simplify (math-to-standard-units
1006 (list '/ (nth 2 math-simplify-expr
) units
) nil
))))
1007 (if (math-units-in-expr-p ratio nil
)
1009 (calc-record-why "*Inconsistent units" math-simplify-expr
)
1011 (list '* (math-add (math-remove-units (nth 1 math-simplify-expr
))
1012 (if (eq (car math-simplify-expr
) '-
)
1013 (math-neg ratio
) ratio
))
1017 (math-simplify-units-prod))
1019 (defun math-simplify-units-prod ()
1020 (and math-simplifying-units
1021 calc-autorange-units
1022 (Math-realp (nth 1 math-simplify-expr
))
1023 (let* ((num (math-float (nth 1 math-simplify-expr
)))
1024 (xpon (calcFunc-xpon num
))
1025 (unitp (cdr (cdr math-simplify-expr
)))
1027 (pow (if (eq (car math-simplify-expr
) '*) 1 -
1))
1029 (and (eq (car-safe unit
) '*)
1030 (setq unitp
(cdr unit
)
1032 (and (eq (car-safe unit
) '^
)
1033 (integerp (nth 2 unit
))
1034 (setq pow
(* pow
(nth 2 unit
))
1037 (and (setq u
(math-check-unit-name unit
))
1040 (>= xpon
(if (eq (car u
) 'm
) 1 3)))
1043 (reverse math-unit-prefixes
)
1044 math-unit-prefixes
))
1047 (or (eq (car u
) (nth 1 unit
))
1052 math-unit-prefixes
))))))
1053 (setq xpon
(+ xpon uxpon
))
1055 (or (memq (car (car p
)) '(?d ?D ?h ?H
))
1056 (and (eq (car (car p
)) ?c
)
1057 (not (eq (car u
) 'm
)))
1058 (< xpon
(setq pxpon
(* (nth 2 (nth 1 (car p
)))
1061 (setq pname
(math-build-var-name
1062 (if (eq (car (car p
)) 0)
1064 (concat (char-to-string
1068 (and (/= (car (car p
)) 0)
1070 math-units-table
)))))
1074 (or (not (eq p pref
))
1075 (< xpon
(+ pxpon
(* (math-abs pow
) 3))))
1077 (setcar (cdr math-simplify-expr
)
1078 (let ((calc-prefer-frac nil
))
1079 (calcFunc-scf (nth 1 math-simplify-expr
)
1081 (setcar unitp pname
)
1082 math-simplify-expr
)))))))
1084 (defvar math-try-cancel-units
)
1087 (and math-simplifying-units
1088 (let ((np (cdr math-simplify-expr
))
1089 (math-try-cancel-units 0)
1091 (setq n
(if (eq (car-safe (nth 2 math-simplify-expr
)) '*)
1092 (cdr (nth 2 math-simplify-expr
))
1093 (nthcdr 2 math-simplify-expr
)))
1094 (if (math-realp (car n
))
1096 (setcar (cdr math-simplify-expr
) (math-mul (nth 1 math-simplify-expr
)
1097 (let ((calc-prefer-frac nil
))
1098 (math-div 1 (car n
)))))
1100 (while (eq (car-safe (setq n
(car np
))) '*)
1101 (math-simplify-units-divisor (cdr n
) (cdr (cdr math-simplify-expr
)))
1102 (setq np
(cdr (cdr n
))))
1103 (math-simplify-units-divisor np
(cdr (cdr math-simplify-expr
)))
1104 (if (eq math-try-cancel-units
0)
1105 (let* ((math-simplifying-units nil
)
1106 (base (math-simplify
1107 (math-to-standard-units math-simplify-expr nil
))))
1108 (if (Math-numberp base
)
1109 (setq math-simplify-expr base
))))
1110 (if (eq (car-safe math-simplify-expr
) '/)
1111 (math-simplify-units-prod))
1112 math-simplify-expr
)))
1114 (defun math-simplify-units-divisor (np dp
)
1117 (while (eq (car-safe (setq d
(car dp
))) '*)
1118 (when (setq temp
(math-simplify-units-quotient n
(nth 1 d
)))
1119 (setcar np
(setq n temp
))
1121 (setq dp
(cdr (cdr d
))))
1122 (when (setq temp
(math-simplify-units-quotient n d
))
1123 (setcar np
(setq n temp
))
1126 ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.
1127 (defun math-simplify-units-quotient (n d
)
1130 (when (and (eq (car-safe n
) '^
)
1131 (integerp (nth 2 n
)))
1132 (setq pow1
(nth 2 n
) n
(nth 1 n
)))
1133 (when (and (eq (car-safe d
) '^
)
1134 (integerp (nth 2 d
)))
1135 (setq pow2
(nth 2 d
) d
(nth 1 d
)))
1136 (let ((un (math-check-unit-name n
))
1137 (ud (math-check-unit-name d
)))
1139 (if (and (equal (nth 4 un
) (nth 4 ud
))
1141 (math-to-standard-units (list '/ n d
) nil
)
1148 (and (eq (car (car un
)) (car (car ud1
)))
1149 (setq math-try-cancel-units
1150 (+ math-try-cancel-units
1151 (- (* (cdr (car un
)) pow1
)
1152 (* (cdr (car ud
)) pow2
)))))
1153 (setq ud1
(cdr ud1
)))
1158 (and math-simplifying-units
1159 (math-realp (nth 2 math-simplify-expr
))
1160 (if (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1161 (list (car (nth 1 math-simplify-expr
))
1162 (list '^
(nth 1 (nth 1 math-simplify-expr
))
1163 (nth 2 math-simplify-expr
))
1164 (list '^
(nth 2 (nth 1 math-simplify-expr
))
1165 (nth 2 math-simplify-expr
)))
1166 (math-simplify-units-pow (nth 1 math-simplify-expr
)
1167 (nth 2 math-simplify-expr
)))))
1169 (math-defsimplify calcFunc-sqrt
1170 (and math-simplifying-units
1171 (if (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1172 (list (car (nth 1 math-simplify-expr
))
1173 (list 'calcFunc-sqrt
(nth 1 (nth 1 math-simplify-expr
)))
1174 (list 'calcFunc-sqrt
(nth 2 (nth 1 math-simplify-expr
))))
1175 (math-simplify-units-pow (nth 1 math-simplify-expr
) '(frac 1 2)))))
1177 (math-defsimplify (calcFunc-floor
1187 (and math-simplifying-units
1188 (= (length math-simplify-expr
) 2)
1189 (if (math-only-units-in-expr-p (nth 1 math-simplify-expr
))
1190 (nth 1 math-simplify-expr
)
1191 (if (and (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1192 (or (math-only-units-in-expr-p
1193 (nth 1 (nth 1 math-simplify-expr
)))
1194 (math-only-units-in-expr-p
1195 (nth 2 (nth 1 math-simplify-expr
)))))
1196 (list (car (nth 1 math-simplify-expr
))
1197 (cons (car math-simplify-expr
)
1198 (cons (nth 1 (nth 1 math-simplify-expr
))
1199 (cdr (cdr math-simplify-expr
))))
1200 (cons (car math-simplify-expr
)
1201 (cons (nth 2 (nth 1 math-simplify-expr
))
1202 (cdr (cdr math-simplify-expr
)))))))))
1204 (defun math-simplify-units-pow (a pow
)
1205 (if (and (eq (car-safe a
) '^
)
1206 (math-check-unit-name (nth 1 a
))
1207 (math-realp (nth 2 a
)))
1208 (list '^
(nth 1 a
) (math-mul pow
(nth 2 a
)))
1209 (let* ((u (math-check-unit-name a
))
1210 (pf (math-to-simple-fraction pow
))
1211 (d (and (eq (car-safe pf
) 'frac
) (nth 2 pf
))))
1213 (math-units-are-multiple u d
)
1214 (list '^
(math-to-standard-units a nil
) pow
)))))
1217 (defun math-units-are-multiple (u n
)
1219 (while (and u
(= (%
(cdr (car u
)) n
) 0))
1223 (math-defsimplify calcFunc-sin
1224 (and math-simplifying-units
1225 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1226 (let ((rad (math-simplify-units
1228 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1229 (calc-angle-mode 'rad
))
1230 (and (eq (car-safe rad
) '*)
1231 (math-realp (nth 1 rad
))
1232 (eq (car-safe (nth 2 rad
)) 'var
)
1233 (eq (nth 1 (nth 2 rad
)) 'rad
)
1234 (list 'calcFunc-sin
(nth 1 rad
))))))
1236 (math-defsimplify calcFunc-cos
1237 (and math-simplifying-units
1238 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1239 (let ((rad (math-simplify-units
1241 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1242 (calc-angle-mode 'rad
))
1243 (and (eq (car-safe rad
) '*)
1244 (math-realp (nth 1 rad
))
1245 (eq (car-safe (nth 2 rad
)) 'var
)
1246 (eq (nth 1 (nth 2 rad
)) 'rad
)
1247 (list 'calcFunc-cos
(nth 1 rad
))))))
1249 (math-defsimplify calcFunc-tan
1250 (and math-simplifying-units
1251 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1252 (let ((rad (math-simplify-units
1254 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1255 (calc-angle-mode 'rad
))
1256 (and (eq (car-safe rad
) '*)
1257 (math-realp (nth 1 rad
))
1258 (eq (car-safe (nth 2 rad
)) 'var
)
1259 (eq (nth 1 (nth 2 rad
)) 'rad
)
1260 (list 'calcFunc-tan
(nth 1 rad
))))))
1262 (math-defsimplify calcFunc-sec
1263 (and math-simplifying-units
1264 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1265 (let ((rad (math-simplify-units
1267 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1268 (calc-angle-mode 'rad
))
1269 (and (eq (car-safe rad
) '*)
1270 (math-realp (nth 1 rad
))
1271 (eq (car-safe (nth 2 rad
)) 'var
)
1272 (eq (nth 1 (nth 2 rad
)) 'rad
)
1273 (list 'calcFunc-sec
(nth 1 rad
))))))
1275 (math-defsimplify calcFunc-csc
1276 (and math-simplifying-units
1277 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1278 (let ((rad (math-simplify-units
1280 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1281 (calc-angle-mode 'rad
))
1282 (and (eq (car-safe rad
) '*)
1283 (math-realp (nth 1 rad
))
1284 (eq (car-safe (nth 2 rad
)) 'var
)
1285 (eq (nth 1 (nth 2 rad
)) 'rad
)
1286 (list 'calcFunc-csc
(nth 1 rad
))))))
1288 (math-defsimplify calcFunc-cot
1289 (and math-simplifying-units
1290 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1291 (let ((rad (math-simplify-units
1293 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1294 (calc-angle-mode 'rad
))
1295 (and (eq (car-safe rad
) '*)
1296 (math-realp (nth 1 rad
))
1297 (eq (car-safe (nth 2 rad
)) 'var
)
1298 (eq (nth 1 (nth 2 rad
)) 'rad
)
1299 (list 'calcFunc-cot
(nth 1 rad
))))))
1302 (defun math-remove-units (expr)
1303 (if (math-check-unit-name expr
)
1305 (if (Math-primp expr
)
1308 (mapcar 'math-remove-units
(cdr expr
))))))
1310 (defun math-extract-units (expr)
1311 (if (memq (car-safe expr
) '(* /))
1313 (mapcar 'math-extract-units
(cdr expr
)))
1314 (if (math-check-unit-name expr
) expr
1)))
1316 (defun math-build-units-table-buffer (enter-buffer)
1317 (if (not (and math-units-table math-units-table-buffer-valid
1318 (get-buffer "*Units Table*")))
1319 (let ((buf (get-buffer-create "*Units Table*"))
1320 (uptr (math-build-units-table))
1321 (calc-language (if (eq calc-language
'big
) nil calc-language
))
1322 (calc-float-format '(float 0))
1323 (calc-group-digits nil
)
1324 (calc-number-radix 10)
1325 (calc-point-char ".")
1329 (message "Formatting units table...")
1331 (let ((inhibit-read-only t
))
1333 (insert "Calculator Units Table:\n\n")
1334 (insert "Unit Type Definition Description\n\n")
1338 (when (eq (car u
) 'm
)
1340 (setq shadowed
(and std
(assq (car u
) math-additional-units
)))
1343 (eq (aref name
0) ?\
*))
1344 (unless (eq uptr math-units-table
)
1346 (setq name
(substring name
1)))
1348 (and shadowed
(insert "("))
1349 (insert (symbol-name (car u
)))
1350 (and shadowed
(insert ")"))
1354 (insert (symbol-name (nth 3 u
))))
1360 (and shadowed
(insert "("))
1362 (insert (math-format-value (nth 1 u
) 80))
1363 (insert (symbol-name (car u
))))
1364 (and shadowed
(insert ")"))
1370 (insert " (redefined above)")
1372 (insert " (base unit)")))
1374 (setq uptr
(cdr uptr
)))
1375 (insert "\n\nUnit Prefix Table:\n\n")
1376 (setq uptr math-unit-prefixes
)
1379 (insert " " (char-to-string (car u
)))
1380 (if (equal (nth 1 u
) (nth 1 (nth 1 uptr
)))
1381 (insert " " (char-to-string (car (car (setq uptr
(cdr uptr
)))))
1384 (insert "10^" (int-to-string (nth 2 (nth 1 u
))))
1386 (insert " " (nth 2 u
) "\n")
1387 (while (eq (car (car (setq uptr
(cdr uptr
)))) 0)))
1390 (message "Formatting units table...done"))
1391 (setq math-units-table-buffer-valid t
)
1392 (let ((oldbuf (current-buffer)))
1394 (goto-char (point-min))
1395 (set-buffer oldbuf
))
1398 (display-buffer buf
)))
1400 (pop-to-buffer (get-buffer "*Units Table*"))
1401 (display-buffer (get-buffer "*Units Table*")))))
1403 (provide 'calc-units
)
1406 ;; coding: iso-latin-1
1409 ;;; arch-tag: e993314f-3adc-4191-be61-4ef8874881c4
1410 ;;; calc-units.el ends here