1 ;;; calc-units.el --- unit conversion functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
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 ;;; Updated August 2007, using
44 ;;; CODATA (http://physics.nist.gov/cuu/Constants/index.html)
45 ;;; NIST (http://physics.nist.gov/Pubs/SP811/appenB9.html)
46 ;;; ESUWM (Encyclopaedia of Scientific Units, Weights and
47 ;;; Measures, by François Cardarelli)
48 ;;; All conversions are exact unless otherwise noted.
50 (defvar math-standard-units
53 ( in
"254*10^(-2) cm" "Inch" )
56 ( mi
"5280 ft" "Mile" )
57 ( au
"149597870691. m" "Astronomical Unit" )
58 ;; (approx) NASA JPL (http://neo.jpl.nasa.gov/glossary/au.html)
59 ( lyr
"c yr" "Light Year" )
60 ( pc
"3.0856775854e16 m" "Parsec" ) ;; (approx) ESUWM
61 ( nmi
"1852 m" "Nautical Mile" )
62 ( fath
"6 ft" "Fathom" )
63 ( mu
"1 um" "Micron" )
64 ( mil
"in/1000" "Mil" )
65 ( point
"in/72" "Point (1/72 inch)" )
66 ( Ang
"10^(-10) m" "Angstrom" )
67 ( mfi
"mi+ft+in" "Miles + feet + inches" )
69 ( texpt
"(100/7227) in" "Point (TeX conventions)" )
70 ( texpc
"12 texpt" "Pica" )
71 ( texbp
"point" "Big point (TeX conventions)" )
72 ( texdd
"(1238/1157) texpt" "Didot point" )
73 ( texcc
"12 texdd" "Cicero" )
74 ( texsp
"(1/65536) texpt" "Scaled TeX point" )
77 ( hect
"10000 m^2" "*Hectare" )
79 ( acre
"mi^2 / 640" "Acre" )
80 ( b
"10^(-28) m^2" "Barn" )
83 ( L
"10^(-3) m^3" "*Liter" )
85 ( gal
"4 qt" "US Gallon" )
88 ( cup
"8 ozfl" "Cup" )
89 ( ozfl
"2 tbsp" "Fluid Ounce" )
90 ( floz
"2 tbsp" "Fluid Ounce" )
91 ( tbsp
"3 tsp" "Tablespoon" )
92 ;; ESUWM defines a US gallon as 231 in^3.
93 ;; That gives the following exact value for tsp.
94 ( tsp
"492892159375*10^(-11) ml" "Teaspoon" )
95 ( vol
"tsp+tbsp+ozfl+cup+pt+qt+gal" "Gallons + ... + teaspoons" )
96 ( galC
"galUK" "Canadian Gallon" )
97 ( galUK
"454609*10^(-5) L" "UK Gallon" ) ;; NIST
102 ( min
"60 s" "Minute" )
103 ( hr
"60 min" "Hour" )
104 ( day
"24 hr" "Day" )
105 ( wk
"7 day" "Week" )
106 ( hms
"wk+day+hr+min+s" "Hours, minutes, seconds" )
107 ( yr
"365.25 day" "Year" ) ;; (approx, but keep)
111 ( mph
"mi/hr" "*Miles per hour" )
112 ( kph
"km/hr" "Kilometers per hour" )
113 ( knot
"nmi/hr" "Knot" )
114 ( c
"299792458 m/s" "Speed of light" ) ;;; CODATA
117 ( ga
"980665*10^(-5) m/s^2" "*\"g\" acceleration" ) ;; CODATA
121 ( lb
"16 oz" "Pound (mass)" )
122 ( oz
"28349523125*10^(-9) g" "Ounce (mass)" ) ;; ESUWM
123 ( ton
"2000 lb" "Ton" )
124 ( tpo
"ton+lb+oz" "Tons + pounds + ounces (mass)" )
125 ( t
"1000 kg" "Metric ton" )
126 ( tonUK
"10160469088*10^(-7) kg" "UK ton" ) ;; ESUWM
127 ( lbt
"12 ozt" "Troy pound" )
128 ( ozt
"31.10347680 g" "Troy ounce" ) ;; (approx) ESUWM
129 ( ct
"(2/10) g" "Carat" ) ;; ESUWM
130 ( u
"1.660538782e-27 kg" "Unified atomic mass" );;(approx) CODATA
133 ( N
"m kg/s^2" "*Newton" )
134 ( dyn
"10^(-5) N" "Dyne" )
135 ( gf
"ga g" "Gram (force)" )
136 ( lbf
"ga lb" "Pound (force)" )
137 ( kip
"1000 lbf" "Kilopound (force)" )
138 ( pdl
"138254954376*10^(-12) N" "Poundal" ) ;; ESUWM
142 ( erg
"10^(-7) J" "Erg" )
143 ( cal
"4.18674 J" "International Table Calorie" );;(approx) ESUWM
144 ( Btu
"105505585262*10^(-8) J" "International Table Btu" ) ;; ESUWM
145 ( eV
"ech V" "Electron volt" )
146 ( ev
"eV" "Electron volt" )
147 ( therm
"105506000 J" "EEC therm" )
148 ( invcm
"h c/cm" "Energy in inverse centimeters" )
149 ( Kayser
"invcm" "Kayser (inverse centimeter energy)" )
150 ( men
"100/invcm" "Inverse energy in meters" )
151 ( Hzen
"h Hz" "Energy in Hertz")
152 ( Ken
"k K" "Energy in Kelvins")
153 ( Wh
"W hr" "Watt hour")
154 ( Ws
"W s" "Watt second")
158 ( hp
"745.699871581 W" "Horsepower" ) ;;(approx) ESUWM
161 ( K nil
"*Degree Kelvin" K
)
162 ( dK
"K" "Degree Kelvin" K
)
163 ( degK
"K" "Degree Kelvin" K
)
164 ( dC
"K" "Degree Celsius" C
)
165 ( degC
"K" "Degree Celsius" C
)
166 ( dF
"(5/9) K" "Degree Fahrenheit" F
)
167 ( degF
"(5/9) K" "Degree Fahrenheit" F
)
170 ( Pa
"N/m^2" "*Pascal" )
171 ( bar
"10^5 Pa" "Bar" )
172 ( atm
"101325 Pa" "Standard atmosphere" ) ;; CODATA
173 ( Torr
"1.333224e2 Pa" "Torr" ) ;;(approx) NIST
174 ( mHg
"1000 Torr" "Meter of mercury" )
175 ( inHg
"254*10^(-1) mmHg" "Inch of mercury" )
176 ( inH2O
"2.490889e2 Pa" "Inch of water" ) ;;(approx) NIST
177 ( psi
"lbf/in^2" "Pounds per square inch" )
180 ( P
"(1/10) Pa s" "*Poise" )
181 ( St
"10^(-4) m^2/s" "Stokes" )
185 ( C
"A s" "Coulomb" )
186 ( Fdy
"ech Nav" "Faraday" )
187 ( e
"ech" "Elementary charge" )
188 ( ech
"1.602176487e-19 C" "Elementary charge" ) ;;(approx) CODATA
192 ( S
"A/V" "Siemens" )
195 ( T
"Wb/m^2" "Tesla" )
196 ( Gs
"10^(-4) T" "Gauss" )
199 ;; Luminous intensity
200 ( cd nil
"*Candela" )
201 ( sb
"10000 cd/m^2" "Stilb" )
202 ( lm
"cd sr" "Lumen" )
203 ( lx
"lm/m^2" "Lux" )
204 ( ph
"10000 lx" "Phot" )
205 ( fc
"10.76391 lx" "Footcandle" ) ;;(approx) NIST
206 ( lam
"10000 lm/m^2" "Lambert" )
207 ( flam
"3.426259 cd/m^2" "Footlambert" ) ;;(approx) NIST
210 ( Bq
"1/s" "*Becquerel" )
211 ( Ci
"37*10^9 Bq" "Curie" ) ;; ESUWM
213 ( Sv
"Gy" "Sievert" )
214 ( R
"258*10^(-6) C/kg" "Roentgen" ) ;; NIST
215 ( rd
"(1/100) Gy" "Rad" )
218 ;; Amount of substance
222 ( rad nil
"*Radian" )
223 ( circ
"2 pi rad" "Full circle" )
224 ( rev
"circ" "Full revolution" )
225 ( deg
"circ/360" "Degree" )
226 ( arcmin
"deg/60" "Arc minute" )
227 ( arcsec
"arcmin/60" "Arc second" )
228 ( grad
"circ/400" "Grade" )
229 ( rpm
"rev/min" "Revolutions per minute" )
232 ( sr nil
"*Steradian" )
234 ;; Other physical quantities
235 ;; The values are from CODATA, and are approximate.
236 ( h
"6.62606896e-34 J s" "*Planck's constant" )
237 ( hbar
"h / (2 pi)" "Planck's constant" )
238 ( mu0
"4 pi 10^(-7) H/m" "Permeability of vacuum" )
239 ( G
"6.67428e-11 m^3/(kg s^2)" "Gravitational constant" )
240 ( Nav
"6.02214179e23 / mol" "Avagadro's constant" )
241 ( me
"9.10938215e-31 kg" "Electron rest mass" )
242 ( mp
"1.672621637e-27 kg" "Proton rest mass" )
243 ( mn
"1.674927211e-27 kg" "Neutron rest mass" )
244 ( mmu
"1.88353130e-28 kg" "Muon rest mass" )
245 ( Ryd
"10973731.568527 /m" "Rydberg's constant" )
246 ( k
"1.3806504e-23 J/K" "Boltzmann's constant" )
247 ( alpha
"7.2973525376e-3" "Fine structure constant" )
248 ( muB
"927.400915e-26 J/T" "Bohr magneton" )
249 ( muN
"5.05078324e-27 J/T" "Nuclear magneton" )
250 ( mue
"-928.476377e-26 J/T" "Electron magnetic moment" )
251 ( mup
"1.410606662e-26 J/T" "Proton magnetic moment" )
252 ( R0
"8.314472 J/(mol K)" "Molar gas constant" )
253 ( V0
"22.710981e-3 m^3/mol" "Standard volume of ideal gas" )))
256 (defvar math-additional-units nil
257 "*Additional units table for user-defined units.
258 Must be formatted like math-standard-units.
259 If this is changed, be sure to set math-units-table to nil to ensure
260 that the combined units table will be rebuilt.")
262 (defvar math-unit-prefixes
263 '( ( ?Y
(^
10 24) "Yotta" )
264 ( ?Z
(^
10 21) "Zetta" )
265 ( ?E
(^
10 18) "Exa" )
266 ( ?P
(^
10 15) "Peta" )
267 ( ?T
(^
10 12) "Tera" )
268 ( ?G
(^
10 9) "Giga" )
269 ( ?M
(^
10 6) "Mega" )
270 ( ?k
(^
10 3) "Kilo" )
271 ( ?K
(^
10 3) "Kilo" )
272 ( ?h
(^
10 2) "Hecto" )
273 ( ?H
(^
10 2) "Hecto" )
274 ( ?D
(^
10 1) "Deka" )
276 ( ?d
(^
10 -
1) "Deci" )
277 ( ?c
(^
10 -
2) "Centi" )
278 ( ?m
(^
10 -
3) "Milli" )
279 ( ?u
(^
10 -
6) "Micro" )
280 ( ?n
(^
10 -
9) "Nano" )
281 ( ?p
(^
10 -
12) "Pico" )
282 ( ?f
(^
10 -
15) "Femto" )
283 ( ?a
(^
10 -
18) "Atto" )
284 ( ?z
(^
10 -
21) "zepto" )
285 ( ?y
(^
10 -
24) "yocto" )))
287 (defvar math-standard-units-systems
289 ( si
( ( g
'(/ (var kg var-kg
) 1000) ) ) )
290 ( mks
( ( g
'(/ (var kg var-kg
) 1000) ) ) )
291 ( cgs
( ( m
'(* (var cm var-cm
) 100 ) ) ) )))
293 (defvar math-units-table nil
294 "Internal units table derived from math-defined-units.
295 Entries are (SYMBOL EXPR DOC-STRING TEMP-TYPE BASE-UNITS).")
297 (defvar math-units-table-buffer-valid nil
)
301 (defun calc-base-units ()
304 (let ((calc-autorange-units nil
))
305 (calc-enter-result 1 "bsun" (math-simplify-units
306 (math-to-standard-units (calc-top-n 1)
309 (defun calc-quick-units ()
312 (let* ((num (- last-command-char ?
0))
313 (pos (if (= num
0) 10 num
))
314 (units (calc-var-value 'var-Units
))
315 (expr (calc-top-n 1)))
316 (unless (and (>= num
0) (<= num
9))
317 (error "Bad unit number"))
318 (unless (math-vectorp units
)
319 (error "No \"quick units\" are defined"))
320 (unless (< pos
(length units
))
321 (error "Unit number %d not defined" pos
))
322 (if (math-units-in-expr-p expr nil
)
323 (calc-enter-result 1 (format "cun%d" num
)
324 (math-convert-units expr
(nth pos units
)))
325 (calc-enter-result 1 (format "*un%d" num
)
327 (math-mul expr
(nth pos units
))))))))
329 (defun math-get-standard-units (expr)
330 "Return the standard units in EXPR."
333 (math-to-standard-units expr nil
))))
335 (defun math-get-units (expr)
336 "Return the units in EXPR."
338 (math-extract-units expr
)))
340 (defun math-make-unit-string (expr)
341 "Return EXPR in string form.
342 If EXPR is nil, return nil."
344 (let ((cexpr (math-compose-expr expr
0)))
345 (replace-regexp-in-string
349 (math-composition-to-string cexpr
))))))
351 (defvar math-default-units-table
352 (make-hash-table :test
'equal
)
353 "A table storing previously converted units.")
355 (defun math-get-default-units (expr)
356 "Get default units to use when converting the units in EXPR."
357 (let* ((units (math-get-units expr
))
358 (standard-units (math-get-standard-units expr
))
359 (default-units (gethash
361 math-default-units-table
)))
362 (if (equal units
(car default-units
))
363 (math-make-unit-string (cadr default-units
))
364 (math-make-unit-string (car default-units
)))))
366 (defun math-put-default-units (expr)
367 "Put the units in EXPR in the default units table."
368 (let* ((units (math-get-units expr
))
369 (standard-units (math-get-standard-units expr
))
370 (default-units (gethash
372 math-default-units-table
)))
375 (puthash standard-units
(list units
) math-default-units-table
))
376 ((not (equal units
(car default-units
)))
377 (puthash standard-units
378 (list units
(car default-units
))
379 math-default-units-table
)))))
382 (defun calc-convert-units (&optional old-units new-units
)
385 (let ((expr (calc-top-n 1))
390 (unless (math-units-in-expr-p expr t
)
391 (let ((uold (or old-units
393 (setq uoldname
(read-string "Old units: "))
394 (if (equal uoldname
"")
398 (if (string-match "\\` */" uoldname
)
399 (setq uoldname
(concat "1" uoldname
)))
400 (math-read-expr uoldname
))))))
401 (when (eq (car-safe uold
) 'error
)
402 (error "Bad format in units expression: %s" (nth 1 uold
)))
403 (setq expr
(math-mul expr uold
))))
405 (setq defunits
(math-get-default-units expr
))
409 (concat "Old units: "
421 (string= new-units
"")
423 (setq new-units defunits
)))
424 (when (string-match "\\` */" new-units
)
425 (setq new-units
(concat "1" new-units
)))
426 (setq units
(math-read-expr new-units
))
427 (when (eq (car-safe units
) 'error
)
428 (error "Bad format in units expression: %s" (nth 2 units
)))
429 (math-put-default-units units
)
430 (let ((unew (math-units-in-expr-p units t
))
431 (std (and (eq (car-safe units
) 'var
)
432 (assq (nth 1 units
) math-standard-units-systems
))))
434 (calc-enter-result 1 "cvun" (math-simplify-units
435 (math-to-standard-units expr
438 (error "No units specified"))
439 (calc-enter-result 1 "cvun"
442 (and uoldname
(not (equal uoldname
"1"))))))))))
444 (defun calc-autorange-units (arg)
447 (calc-change-mode 'calc-autorange-units arg nil t
)
448 (message (if calc-autorange-units
449 "Adjusting target unit prefix automatically"
450 "Using target units exactly"))))
452 (defun calc-convert-temperature (&optional old-units new-units
)
455 (let ((expr (calc-top-n 1))
460 (setq uold
(or old-units
461 (let ((units (math-single-units-in-expr-p expr
)))
464 (list 'var
(car units
)
465 (intern (concat "var-"
468 (error "Not a pure temperature expression"))
470 (setq uoldname
(read-string
471 "Old temperature units: ")))))))
472 (when (eq (car-safe uold
) 'error
)
473 (error "Bad format in units expression: %s" (nth 2 uold
)))
474 (or (math-units-in-expr-p expr nil
)
475 (setq expr
(math-mul expr uold
)))
476 (setq defunits
(math-get-default-units expr
))
477 (setq unew
(or new-units
482 (concat "Old temperature units: "
485 "New temperature units")
491 (when (eq (car-safe unew
) 'error
)
492 (error "Bad format in units expression: %s" (nth 2 unew
)))
493 (math-put-default-units unew
)
494 (calc-enter-result 1 "cvtm" (math-simplify-units
495 (math-convert-temperature expr uold unew
498 (defun calc-remove-units ()
501 (calc-enter-result 1 "rmun" (math-simplify-units
502 (math-remove-units (calc-top-n 1))))))
504 (defun calc-extract-units ()
507 (calc-enter-result 1 "rmun" (math-simplify-units
508 (math-extract-units (calc-top-n 1))))))
510 ;; The variables calc-num-units and calc-den-units are local to
511 ;; calc-explain-units, but are used by calc-explain-units-rec,
512 ;; which is called by calc-explain-units.
513 (defvar calc-num-units
)
514 (defvar calc-den-units
)
516 (defun calc-explain-units ()
519 (let ((calc-num-units nil
)
520 (calc-den-units nil
))
521 (calc-explain-units-rec (calc-top-n 1) 1)
522 (and calc-den-units
(string-match "^[^(].* .*[^)]$" calc-den-units
)
523 (setq calc-den-units
(concat "(" calc-den-units
")")))
526 (message "%s per %s" calc-num-units calc-den-units
)
527 (message "%s" calc-num-units
))
529 (message "1 per %s" calc-den-units
)
530 (message "No units in expression"))))))
532 (defun calc-explain-units-rec (expr pow
)
533 (let ((u (math-check-unit-name expr
))
535 (if (and u
(not (math-zerop pow
)))
536 (let ((name (or (nth 2 u
) (symbol-name (car u
)))))
537 (if (eq (aref name
0) ?\
*)
538 (setq name
(substring name
1)))
539 (if (string-match "[^a-zA-Z0-9']" name
)
540 (if (string-match "^[a-zA-Z0-9' ()]*$" name
)
541 (while (setq pos
(string-match "[ ()]" name
))
542 (setq name
(concat (substring name
0 pos
)
543 (if (eq (aref name pos
) 32) "-" "")
544 (substring name
(1+ pos
)))))
545 (setq name
(concat "(" name
")"))))
546 (or (eq (nth 1 expr
) (car u
))
547 (setq name
(concat (nth 2 (assq (aref (symbol-name
550 (if (and (string-match "[^a-zA-Z0-9']" name
)
551 (not (memq (car u
) '(mHg gf
))))
554 (cond ((or (math-equal-int pow
1)
555 (math-equal-int pow -
1)))
556 ((or (math-equal-int pow
2)
557 (math-equal-int pow -
2))
558 (if (equal (nth 4 u
) '((m .
1)))
559 (setq name
(concat "Square-" name
))
560 (setq name
(concat name
"-squared"))))
561 ((or (math-equal-int pow
3)
562 (math-equal-int pow -
3))
563 (if (equal (nth 4 u
) '((m .
1)))
564 (setq name
(concat "Cubic-" name
))
565 (setq name
(concat name
"-cubed"))))
567 (setq name
(concat name
"^"
568 (math-format-number (math-abs pow
))))))
570 (setq calc-num-units
(if calc-num-units
571 (concat calc-num-units
" " name
)
573 (setq calc-den-units
(if calc-den-units
574 (concat calc-den-units
" " name
)
576 (cond ((eq (car-safe expr
) '*)
577 (calc-explain-units-rec (nth 1 expr
) pow
)
578 (calc-explain-units-rec (nth 2 expr
) pow
))
579 ((eq (car-safe expr
) '/)
580 (calc-explain-units-rec (nth 1 expr
) pow
)
581 (calc-explain-units-rec (nth 2 expr
) (- pow
)))
582 ((memq (car-safe expr
) '(neg + -
))
583 (calc-explain-units-rec (nth 1 expr
) pow
))
584 ((and (eq (car-safe expr
) '^
)
585 (math-realp (nth 2 expr
)))
586 (calc-explain-units-rec (nth 1 expr
)
587 (math-mul pow
(nth 2 expr
))))))))
589 (defun calc-simplify-units ()
592 (calc-with-default-simplification
593 (calc-enter-result 1 "smun" (math-simplify-units (calc-top-n 1))))))
595 (defun calc-view-units-table (n)
597 (and n
(setq math-units-table-buffer-valid nil
))
598 (let ((win (get-buffer-window "*Units Table*")))
601 math-units-table-buffer-valid
)
603 (bury-buffer (window-buffer win
))
604 (let ((curwin (selected-window)))
606 (switch-to-buffer nil
)
607 (select-window curwin
)))
608 (math-build-units-table-buffer nil
))))
610 (defun calc-enter-units-table (n)
612 (and n
(setq math-units-table-buffer-valid nil
))
613 (math-build-units-table-buffer t
)
614 (message "%s" (substitute-command-keys "Type \\[calc] to return to the Calculator")))
616 (defun calc-define-unit (uname desc
)
617 (interactive "SDefine unit name: \nsDescription: ")
619 (let ((form (calc-top-n 1))
620 (unit (assq uname math-additional-units
)))
622 (setq math-additional-units
623 (cons (setq unit
(list uname nil nil
))
624 math-additional-units
)
625 math-units-table nil
))
626 (setcar (cdr unit
) (and (not (and (eq (car-safe form
) 'var
)
627 (eq (nth 1 form
) uname
)))
628 (not (math-equal-int form
1))
629 (math-format-flat-expr form
0)))
630 (setcar (cdr (cdr unit
)) (and (not (equal desc
""))
632 (calc-invalidate-units-table))
634 (defun calc-undefine-unit (uname)
635 (interactive "SUndefine unit name: ")
637 (let ((unit (assq uname math-additional-units
)))
639 (if (assq uname math-standard-units
)
640 (error "\"%s\" is a predefined unit name" uname
)
641 (error "Unit name \"%s\" not found" uname
)))
642 (setq math-additional-units
(delq unit math-additional-units
)
643 math-units-table nil
)))
644 (calc-invalidate-units-table))
646 (defun calc-invalidate-units-table ()
647 (setq math-units-table nil
)
648 (let ((buf (get-buffer "*Units Table*")))
653 (goto-char (point-min))
654 (if (looking-at "Calculator Units Table")
655 (let ((inhibit-read-only t
))
656 (insert "(Obsolete) "))))))))
658 (defun calc-get-unit-definition (uname)
659 (interactive "SGet definition for unit: ")
661 (math-build-units-table)
662 (let ((unit (assq uname math-units-table
)))
664 (error "Unit name \"%s\" not found" uname
))
665 (let ((msg (nth 2 unit
)))
667 (if (string-match "^\\*" msg
)
668 (setq msg
(substring msg
1)))
669 (setq msg
(symbol-name uname
)))
672 (calc-enter-result 0 "ugdf" (nth 1 unit
))
673 (message "Derived unit: %s" msg
))
674 (calc-enter-result 0 "ugdf" (list 'var uname
677 (symbol-name uname
)))))
678 (message "Base unit: %s" msg
))))))
680 (defun calc-permanent-units ()
684 (set-buffer (find-file-noselect (substitute-in-file-name
685 calc-settings-file
)))
686 (goto-char (point-min))
687 (if (and (search-forward ";;; Custom units stored by Calc" nil t
)
691 (search-forward "\n;;; End of custom units" nil t
)))
695 (delete-region pos
(point)))
696 (goto-char (point-max))
699 (insert ";;; Custom units stored by Calc on " (current-time-string) "\n")
700 (if math-additional-units
702 (insert "(setq math-additional-units '(\n")
703 (let ((list math-additional-units
))
705 (insert " (" (symbol-name (car (car list
))) " "
706 (if (nth 1 (car list
))
707 (if (stringp (nth 1 (car list
)))
708 (prin1-to-string (nth 1 (car list
)))
709 (prin1-to-string (math-format-flat-expr
710 (nth 1 (car list
)) 0)))
713 (prin1-to-string (nth 2 (car list
)))
715 (setq list
(cdr list
))))
717 (insert ";;; (no custom units defined)\n"))
718 (insert ";;; End of custom units\n")
722 ;; The variable math-cu-unit-list is local to math-build-units-table,
723 ;; but is used by math-compare-unit-names, which is called (indirectly)
724 ;; by math-build-units-table.
725 ;; math-cu-unit-list is also local to math-convert-units, but is used
726 ;; by math-convert-units-rec, which is called by math-convert-units.
727 (defvar math-cu-unit-list
)
729 (defun math-build-units-table ()
731 (let* ((combined-units (append math-additional-units
732 math-standard-units
))
733 (math-cu-unit-list (mapcar 'car combined-units
))
735 (message "Building units table...")
736 (setq math-units-table-buffer-valid nil
)
737 (setq tab
(mapcar (function
741 (if (stringp (nth 1 x
))
742 (let ((exp (math-read-plain-expr
744 (if (eq (car-safe exp
) 'error
)
745 (error "Format error in definition of %s in units table: %s"
752 (list (cons (car x
) 1))))))
754 (let ((math-units-table tab
))
755 (mapc 'math-find-base-units tab
))
756 (message "Building units table...done")
757 (setq math-units-table tab
))))
759 ;; The variables math-fbu-base and math-fbu-entry are local to
760 ;; math-find-base-units, but are used by math-find-base-units-rec,
761 ;; which is called by math-find-base-units.
762 (defvar math-fbu-base
)
763 (defvar math-fbu-entry
)
765 (defun math-find-base-units (math-fbu-entry)
766 (if (eq (nth 4 math-fbu-entry
) 'boom
)
767 (error "Circular definition involving unit %s" (car math-fbu-entry
)))
768 (or (nth 4 math-fbu-entry
)
770 (setcar (nthcdr 4 math-fbu-entry
) 'boom
)
771 (math-find-base-units-rec (nth 1 math-fbu-entry
) 1)
773 (error "Dimensionless definition for unit %s" (car math-fbu-entry
)))
774 (while (eq (cdr (car math-fbu-base
)) 0)
775 (setq math-fbu-base
(cdr math-fbu-base
)))
776 (let ((b math-fbu-base
))
778 (if (eq (cdr (car (cdr b
))) 0)
779 (setcdr b
(cdr (cdr b
)))
781 (setq math-fbu-base
(sort math-fbu-base
'math-compare-unit-names
))
782 (setcar (nthcdr 4 math-fbu-entry
) math-fbu-base
)
785 (defun math-compare-unit-names (a b
)
786 (memq (car b
) (cdr (memq (car a
) math-cu-unit-list
))))
788 (defun math-find-base-units-rec (expr pow
)
789 (let ((u (math-check-unit-name expr
)))
791 (let ((ulist (math-find-base-units u
)))
793 (let ((p (* (cdr (car ulist
)) pow
))
794 (old (assq (car (car ulist
)) math-fbu-base
)))
796 (setcdr old
(+ (cdr old
) p
))
798 (cons (cons (car (car ulist
)) p
) math-fbu-base
))))
799 (setq ulist
(cdr ulist
)))))
800 ((math-scalarp expr
))
801 ((and (eq (car expr
) '^
)
802 (integerp (nth 2 expr
)))
803 (math-find-base-units-rec (nth 1 expr
) (* pow
(nth 2 expr
))))
805 (math-find-base-units-rec (nth 1 expr
) pow
)
806 (math-find-base-units-rec (nth 2 expr
) pow
))
808 (math-find-base-units-rec (nth 1 expr
) pow
)
809 (math-find-base-units-rec (nth 2 expr
) (- pow
)))
810 ((eq (car expr
) 'neg
)
811 (math-find-base-units-rec (nth 1 expr
) pow
))
813 (math-find-base-units-rec (nth 1 expr
) pow
))
814 ((eq (car expr
) 'var
)
815 (or (eq (nth 1 expr
) 'pi
)
816 (error "Unknown name %s in defining expression for unit %s"
817 (nth 1 expr
) (car math-fbu-entry
))))
818 (t (error "Malformed defining expression for unit %s" (car math-fbu-entry
))))))
821 (defun math-units-in-expr-p (expr sub-exprs
)
823 (if (eq (car expr
) 'var
)
824 (math-check-unit-name expr
)
826 (memq (car expr
) '(* / ^
)))
827 (or (math-units-in-expr-p (nth 1 expr
) sub-exprs
)
828 (math-units-in-expr-p (nth 2 expr
) sub-exprs
))))))
830 (defun math-only-units-in-expr-p (expr)
832 (if (eq (car expr
) 'var
)
833 (math-check-unit-name expr
)
834 (if (memq (car expr
) '(* /))
835 (and (math-only-units-in-expr-p (nth 1 expr
))
836 (math-only-units-in-expr-p (nth 2 expr
)))
837 (and (eq (car expr
) '^
)
838 (and (math-only-units-in-expr-p (nth 1 expr
))
839 (math-realp (nth 2 expr
))))))))
841 (defun math-single-units-in-expr-p (expr)
842 (cond ((math-scalarp expr
) nil
)
843 ((eq (car expr
) 'var
)
844 (math-check-unit-name expr
))
846 (let ((u1 (math-single-units-in-expr-p (nth 1 expr
)))
847 (u2 (math-single-units-in-expr-p (nth 2 expr
))))
848 (or (and u1 u2
'wrong
)
852 (if (math-units-in-expr-p (nth 2 expr
) nil
)
854 (math-single-units-in-expr-p (nth 1 expr
))))
857 (defun math-check-unit-name (v)
858 (and (eq (car-safe v
) 'var
)
859 (or (assq (nth 1 v
) (or math-units-table
(math-build-units-table)))
860 (let ((name (symbol-name (nth 1 v
))))
861 (and (> (length name
) 1)
862 (assq (aref name
0) math-unit-prefixes
)
863 (or (assq (intern (substring name
1)) math-units-table
)
864 (and (eq (aref name
0) ?M
)
866 (eq (aref name
1) ?e
)
867 (eq (aref name
2) ?g
)
868 (assq (intern (substring name
3))
869 math-units-table
))))))))
871 ;; The variable math-which-standard is local to math-to-standard-units,
872 ;; but is used by math-to-standard-rec, which is called by
873 ;; math-to-standard-units.
874 (defvar math-which-standard
)
876 (defun math-to-standard-units (expr math-which-standard
)
877 (math-to-standard-rec expr
))
879 (defun math-to-standard-rec (expr)
880 (if (eq (car-safe expr
) 'var
)
881 (let ((u (math-check-unit-name expr
))
886 (setq expr
(math-to-standard-rec (nth 1 u
)))
887 (let ((st (assq (car u
) math-which-standard
)))
889 (setq expr
(nth 1 st
))
890 (setq expr
(list 'var
(car u
)
891 (intern (concat "var-"
897 (nth 1 (assq (aref (symbol-name base
) 0)
904 (if (Math-primp expr
)
907 (mapcar 'math-to-standard-rec
(cdr expr
))))))
909 (defun math-apply-units (expr units ulist
&optional pure
)
910 (setq expr
(math-simplify-units expr
))
914 (or (math-numberp expr
)
915 (error "Incompatible units"))
917 (setq value
(math-div expr
(nth 1 (car ulist
)))
918 value
(math-floor (let ((calc-internal-prec
919 (1- calc-internal-prec
)))
920 (math-normalize value
)))
921 new
(math-add new
(math-mul value
(car (car ulist
))))
922 expr
(math-sub expr
(math-mul value
(nth 1 (car ulist
))))
924 (math-add new
(math-mul (math-div expr
(nth 1 (car ulist
)))
928 (math-simplify-units (list '* expr units
)))))
930 (defvar math-decompose-units-cache nil
)
931 (defun math-decompose-units (units)
932 (let ((u (math-check-unit-name units
)))
933 (and u
(eq (car-safe (nth 1 u
)) '+)
934 (setq units
(nth 1 u
))))
935 (setq units
(calcFunc-expand units
))
936 (and (eq (car-safe units
) '+)
937 (let ((entry (list units calc-internal-prec calc-prefer-frac
)))
938 (or (equal entry
(car math-decompose-units-cache
))
942 (while (eq (car-safe utemp
) '+)
943 (setq ulist
(cons (math-decompose-unit-part (nth 2 utemp
))
945 utemp
(nth 1 utemp
)))
946 (setq ulist
(cons (math-decompose-unit-part utemp
) ulist
)
948 (while (setq utemp
(cdr utemp
))
949 (unless (equal (nth 2 (car utemp
)) (nth 2 (car ulist
)))
950 (error "Inconsistent units in sum")))
951 (setq math-decompose-units-cache
956 (not (Math-lessp (nth 1 x
)
958 (cdr math-decompose-units-cache
))))
960 (defun math-decompose-unit-part (unit)
962 (math-is-multiple (math-simplify-units (math-to-standard-units
966 ;; The variable math-fcu-u is local to math-find-compatible-unit,
967 ;; but is used by math-find-compatible-rec which is called by
968 ;; math-find-compatible-unit.
971 (defun math-find-compatible-unit (expr unit
)
972 (let ((math-fcu-u (math-check-unit-name unit
)))
974 (math-find-compatible-unit-rec expr
1))))
976 (defun math-find-compatible-unit-rec (expr pow
)
977 (cond ((eq (car-safe expr
) '*)
978 (or (math-find-compatible-unit-rec (nth 1 expr
) pow
)
979 (math-find-compatible-unit-rec (nth 2 expr
) pow
)))
980 ((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 ((and (eq (car-safe expr
) '^
)
984 (integerp (nth 2 expr
)))
985 (math-find-compatible-unit-rec (nth 1 expr
) (* pow
(nth 2 expr
))))
987 (let ((u2 (math-check-unit-name expr
)))
988 (if (equal (nth 4 math-fcu-u
) (nth 4 u2
))
991 ;; The variables math-cu-new-units and math-cu-pure are local to
992 ;; math-convert-units, but are used by math-convert-units-rec,
993 ;; which is called by math-convert-units.
994 (defvar math-cu-new-units
)
995 (defvar math-cu-pure
)
997 (defun math-convert-units (expr math-cu-new-units
&optional math-cu-pure
)
998 (if (eq (car-safe math-cu-new-units
) 'var
)
999 (let ((unew (assq (nth 1 math-cu-new-units
)
1000 (math-build-units-table))))
1001 (if (eq (car-safe (nth 1 unew
)) '+)
1002 (setq math-cu-new-units
(nth 1 unew
)))))
1003 (math-with-extra-prec 2
1004 (let ((compat (and (not math-cu-pure
)
1005 (math-find-compatible-unit expr math-cu-new-units
)))
1006 (math-cu-unit-list nil
)
1007 (math-combining-units nil
))
1009 (math-simplify-units
1010 (math-mul (math-mul (math-simplify-units
1011 (math-div expr
(math-pow (car compat
)
1013 (math-pow math-cu-new-units
(cdr compat
)))
1014 (math-simplify-units
1015 (math-to-standard-units
1016 (math-pow (math-div (car compat
) math-cu-new-units
)
1019 (when (setq math-cu-unit-list
(math-decompose-units math-cu-new-units
))
1020 (setq math-cu-new-units
(nth 2 (car math-cu-unit-list
))))
1021 (when (eq (car-safe expr
) '+)
1022 (setq expr
(math-simplify-units expr
)))
1023 (if (math-units-in-expr-p expr t
)
1024 (math-convert-units-rec expr
)
1025 (math-apply-units (math-to-standard-units
1026 (list '/ expr math-cu-new-units
) nil
)
1027 math-cu-new-units math-cu-unit-list math-cu-pure
))))))
1029 (defun math-convert-units-rec (expr)
1030 (if (math-units-in-expr-p expr nil
)
1031 (math-apply-units (math-to-standard-units
1032 (list '/ expr math-cu-new-units
) nil
)
1033 math-cu-new-units math-cu-unit-list math-cu-pure
)
1034 (if (Math-primp expr
)
1037 (mapcar 'math-convert-units-rec
(cdr expr
))))))
1039 (defun math-convert-temperature (expr old new
&optional pure
)
1040 (let* ((units (math-single-units-in-expr-p expr
))
1042 (if (or (null units
)
1043 (equal (nth 1 old
) (car units
)))
1044 (math-check-unit-name old
)
1045 (error "Inconsistent temperature units"))
1047 (unew (math-check-unit-name new
)))
1048 (unless (and (consp unew
) (nth 3 unew
))
1049 (error "Not a valid temperature unit"))
1050 (unless (and (consp uold
) (nth 3 uold
))
1051 (error "Not a pure temperature expression"))
1052 (let ((v (car uold
)))
1053 (setq expr
(list '/ expr
(list 'var v
1054 (intern (concat "var-"
1055 (symbol-name v
)))))))
1056 (or (eq (nth 3 uold
) (nth 3 unew
))
1057 (cond ((eq (nth 3 uold
) 'K
)
1058 (setq expr
(list '- expr
'(/ 27315 100)))
1059 (if (eq (nth 3 unew
) 'F
)
1060 (setq expr
(list '+ (list '* expr
'(/ 9 5)) 32))))
1061 ((eq (nth 3 uold
) 'C
)
1062 (if (eq (nth 3 unew
) 'F
)
1063 (setq expr
(list '+ (list '* expr
'(/ 9 5)) 32))
1064 (setq expr
(list '+ expr
'(/ 27315 100)))))
1066 (setq expr
(list '* (list '- expr
32) '(/ 5 9)))
1067 (if (eq (nth 3 unew
) 'K
)
1068 (setq expr
(list '+ expr
'(/ 27315 100)))))))
1071 (list '* expr new
))))
1075 (defun math-simplify-units (a)
1076 (let ((math-simplifying-units t
)
1077 (calc-matrix-mode 'scalar
))
1079 (defalias 'calcFunc-usimplify
'math-simplify-units
)
1081 ;; The function created by math-defsimplify uses the variable
1082 ;; math-simplify-expr, and so is used by functions in math-defsimplify
1083 (defvar math-simplify-expr
)
1085 (math-defsimplify (+ -
)
1086 (and math-simplifying-units
1087 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1088 (let* ((units (math-extract-units (nth 1 math-simplify-expr
)))
1089 (ratio (math-simplify (math-to-standard-units
1090 (list '/ (nth 2 math-simplify-expr
) units
) nil
))))
1091 (if (math-units-in-expr-p ratio nil
)
1093 (calc-record-why "*Inconsistent units" math-simplify-expr
)
1095 (list '* (math-add (math-remove-units (nth 1 math-simplify-expr
))
1096 (if (eq (car math-simplify-expr
) '-
)
1097 (math-neg ratio
) ratio
))
1101 (math-simplify-units-prod))
1103 (defun math-simplify-units-prod ()
1104 (and math-simplifying-units
1105 calc-autorange-units
1106 (Math-realp (nth 1 math-simplify-expr
))
1107 (let* ((num (math-float (nth 1 math-simplify-expr
)))
1108 (xpon (calcFunc-xpon num
))
1109 (unitp (cdr (cdr math-simplify-expr
)))
1111 (pow (if (eq (car math-simplify-expr
) '*) 1 -
1))
1113 (and (eq (car-safe unit
) '*)
1114 (setq unitp
(cdr unit
)
1116 (and (eq (car-safe unit
) '^
)
1117 (integerp (nth 2 unit
))
1118 (setq pow
(* pow
(nth 2 unit
))
1121 (and (setq u
(math-check-unit-name unit
))
1124 (>= xpon
(if (eq (car u
) 'm
) 1 3)))
1127 (reverse math-unit-prefixes
)
1128 math-unit-prefixes
))
1131 (or (eq (car u
) (nth 1 unit
))
1136 math-unit-prefixes
))))))
1137 (setq xpon
(+ xpon uxpon
))
1139 (or (memq (car (car p
)) '(?d ?D ?h ?H
))
1140 (and (eq (car (car p
)) ?c
)
1141 (not (eq (car u
) 'm
)))
1142 (< xpon
(setq pxpon
(* (nth 2 (nth 1 (car p
)))
1145 (setq pname
(math-build-var-name
1146 (if (eq (car (car p
)) 0)
1148 (concat (char-to-string
1152 (and (/= (car (car p
)) 0)
1154 math-units-table
)))))
1158 (or (not (eq p pref
))
1159 (< xpon
(+ pxpon
(* (math-abs pow
) 3))))
1161 (setcar (cdr math-simplify-expr
)
1162 (let ((calc-prefer-frac nil
))
1163 (calcFunc-scf (nth 1 math-simplify-expr
)
1165 (setcar unitp pname
)
1166 math-simplify-expr
)))))))
1168 (defvar math-try-cancel-units
)
1171 (and math-simplifying-units
1172 (let ((np (cdr math-simplify-expr
))
1173 (math-try-cancel-units 0)
1175 (setq n
(if (eq (car-safe (nth 2 math-simplify-expr
)) '*)
1176 (cdr (nth 2 math-simplify-expr
))
1177 (nthcdr 2 math-simplify-expr
)))
1178 (if (math-realp (car n
))
1180 (setcar (cdr math-simplify-expr
) (math-mul (nth 1 math-simplify-expr
)
1181 (let ((calc-prefer-frac nil
))
1182 (math-div 1 (car n
)))))
1184 (while (eq (car-safe (setq n
(car np
))) '*)
1185 (math-simplify-units-divisor (cdr n
) (cdr (cdr math-simplify-expr
)))
1186 (setq np
(cdr (cdr n
))))
1187 (math-simplify-units-divisor np
(cdr (cdr math-simplify-expr
)))
1188 (if (eq math-try-cancel-units
0)
1189 (let* ((math-simplifying-units nil
)
1190 (base (math-simplify
1191 (math-to-standard-units math-simplify-expr nil
))))
1192 (if (Math-numberp base
)
1193 (setq math-simplify-expr base
))))
1194 (if (eq (car-safe math-simplify-expr
) '/)
1195 (math-simplify-units-prod))
1196 math-simplify-expr
)))
1198 (defun math-simplify-units-divisor (np dp
)
1201 (while (eq (car-safe (setq d
(car dp
))) '*)
1202 (when (setq temp
(math-simplify-units-quotient n
(nth 1 d
)))
1203 (setcar np
(setq n temp
))
1205 (setq dp
(cdr (cdr d
))))
1206 (when (setq temp
(math-simplify-units-quotient n d
))
1207 (setcar np
(setq n temp
))
1210 ;; Simplify, e.g., "in / cm" to "2.54" in a units expression.
1211 (defun math-simplify-units-quotient (n d
)
1214 (when (and (eq (car-safe n
) '^
)
1215 (integerp (nth 2 n
)))
1216 (setq pow1
(nth 2 n
) n
(nth 1 n
)))
1217 (when (and (eq (car-safe d
) '^
)
1218 (integerp (nth 2 d
)))
1219 (setq pow2
(nth 2 d
) d
(nth 1 d
)))
1220 (let ((un (math-check-unit-name n
))
1221 (ud (math-check-unit-name d
)))
1223 (if (and (equal (nth 4 un
) (nth 4 ud
))
1225 (math-to-standard-units (list '/ n d
) nil
)
1232 (and (eq (car (car un
)) (car (car ud1
)))
1233 (setq math-try-cancel-units
1234 (+ math-try-cancel-units
1235 (- (* (cdr (car un
)) pow1
)
1236 (* (cdr (car ud
)) pow2
)))))
1237 (setq ud1
(cdr ud1
)))
1242 (and math-simplifying-units
1243 (math-realp (nth 2 math-simplify-expr
))
1244 (if (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1245 (list (car (nth 1 math-simplify-expr
))
1246 (list '^
(nth 1 (nth 1 math-simplify-expr
))
1247 (nth 2 math-simplify-expr
))
1248 (list '^
(nth 2 (nth 1 math-simplify-expr
))
1249 (nth 2 math-simplify-expr
)))
1250 (math-simplify-units-pow (nth 1 math-simplify-expr
)
1251 (nth 2 math-simplify-expr
)))))
1253 (math-defsimplify calcFunc-sqrt
1254 (and math-simplifying-units
1255 (if (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1256 (list (car (nth 1 math-simplify-expr
))
1257 (list 'calcFunc-sqrt
(nth 1 (nth 1 math-simplify-expr
)))
1258 (list 'calcFunc-sqrt
(nth 2 (nth 1 math-simplify-expr
))))
1259 (math-simplify-units-pow (nth 1 math-simplify-expr
) '(frac 1 2)))))
1261 (math-defsimplify (calcFunc-floor
1271 (and math-simplifying-units
1272 (= (length math-simplify-expr
) 2)
1273 (if (math-only-units-in-expr-p (nth 1 math-simplify-expr
))
1274 (nth 1 math-simplify-expr
)
1275 (if (and (memq (car-safe (nth 1 math-simplify-expr
)) '(* /))
1276 (or (math-only-units-in-expr-p
1277 (nth 1 (nth 1 math-simplify-expr
)))
1278 (math-only-units-in-expr-p
1279 (nth 2 (nth 1 math-simplify-expr
)))))
1280 (list (car (nth 1 math-simplify-expr
))
1281 (cons (car math-simplify-expr
)
1282 (cons (nth 1 (nth 1 math-simplify-expr
))
1283 (cdr (cdr math-simplify-expr
))))
1284 (cons (car math-simplify-expr
)
1285 (cons (nth 2 (nth 1 math-simplify-expr
))
1286 (cdr (cdr math-simplify-expr
)))))))))
1288 (defun math-simplify-units-pow (a pow
)
1289 (if (and (eq (car-safe a
) '^
)
1290 (math-check-unit-name (nth 1 a
))
1291 (math-realp (nth 2 a
)))
1292 (list '^
(nth 1 a
) (math-mul pow
(nth 2 a
)))
1293 (let* ((u (math-check-unit-name a
))
1294 (pf (math-to-simple-fraction pow
))
1295 (d (and (eq (car-safe pf
) 'frac
) (nth 2 pf
))))
1297 (math-units-are-multiple u d
)
1298 (list '^
(math-to-standard-units a nil
) pow
)))))
1301 (defun math-units-are-multiple (u n
)
1303 (while (and u
(= (%
(cdr (car u
)) n
) 0))
1307 (math-defsimplify calcFunc-sin
1308 (and math-simplifying-units
1309 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1310 (let ((rad (math-simplify-units
1312 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1313 (calc-angle-mode 'rad
))
1314 (and (eq (car-safe rad
) '*)
1315 (math-realp (nth 1 rad
))
1316 (eq (car-safe (nth 2 rad
)) 'var
)
1317 (eq (nth 1 (nth 2 rad
)) 'rad
)
1318 (list 'calcFunc-sin
(nth 1 rad
))))))
1320 (math-defsimplify calcFunc-cos
1321 (and math-simplifying-units
1322 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1323 (let ((rad (math-simplify-units
1325 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1326 (calc-angle-mode 'rad
))
1327 (and (eq (car-safe rad
) '*)
1328 (math-realp (nth 1 rad
))
1329 (eq (car-safe (nth 2 rad
)) 'var
)
1330 (eq (nth 1 (nth 2 rad
)) 'rad
)
1331 (list 'calcFunc-cos
(nth 1 rad
))))))
1333 (math-defsimplify calcFunc-tan
1334 (and math-simplifying-units
1335 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1336 (let ((rad (math-simplify-units
1338 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1339 (calc-angle-mode 'rad
))
1340 (and (eq (car-safe rad
) '*)
1341 (math-realp (nth 1 rad
))
1342 (eq (car-safe (nth 2 rad
)) 'var
)
1343 (eq (nth 1 (nth 2 rad
)) 'rad
)
1344 (list 'calcFunc-tan
(nth 1 rad
))))))
1346 (math-defsimplify calcFunc-sec
1347 (and math-simplifying-units
1348 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1349 (let ((rad (math-simplify-units
1351 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1352 (calc-angle-mode 'rad
))
1353 (and (eq (car-safe rad
) '*)
1354 (math-realp (nth 1 rad
))
1355 (eq (car-safe (nth 2 rad
)) 'var
)
1356 (eq (nth 1 (nth 2 rad
)) 'rad
)
1357 (list 'calcFunc-sec
(nth 1 rad
))))))
1359 (math-defsimplify calcFunc-csc
1360 (and math-simplifying-units
1361 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1362 (let ((rad (math-simplify-units
1364 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1365 (calc-angle-mode 'rad
))
1366 (and (eq (car-safe rad
) '*)
1367 (math-realp (nth 1 rad
))
1368 (eq (car-safe (nth 2 rad
)) 'var
)
1369 (eq (nth 1 (nth 2 rad
)) 'rad
)
1370 (list 'calcFunc-csc
(nth 1 rad
))))))
1372 (math-defsimplify calcFunc-cot
1373 (and math-simplifying-units
1374 (math-units-in-expr-p (nth 1 math-simplify-expr
) nil
)
1375 (let ((rad (math-simplify-units
1377 (math-to-standard-units (nth 1 math-simplify-expr
) nil
))))
1378 (calc-angle-mode 'rad
))
1379 (and (eq (car-safe rad
) '*)
1380 (math-realp (nth 1 rad
))
1381 (eq (car-safe (nth 2 rad
)) 'var
)
1382 (eq (nth 1 (nth 2 rad
)) 'rad
)
1383 (list 'calcFunc-cot
(nth 1 rad
))))))
1386 (defun math-remove-units (expr)
1387 (if (math-check-unit-name expr
)
1389 (if (Math-primp expr
)
1392 (mapcar 'math-remove-units
(cdr expr
))))))
1394 (defun math-extract-units (expr)
1395 (if (memq (car-safe expr
) '(* /))
1397 (mapcar 'math-extract-units
(cdr expr
)))
1398 (if (math-check-unit-name expr
) expr
1)))
1400 (defun math-build-units-table-buffer (enter-buffer)
1401 (if (not (and math-units-table math-units-table-buffer-valid
1402 (get-buffer "*Units Table*")))
1403 (let ((buf (get-buffer-create "*Units Table*"))
1404 (uptr (math-build-units-table))
1405 (calc-language (if (eq calc-language
'big
) nil calc-language
))
1406 (calc-float-format '(float 0))
1407 (calc-group-digits nil
)
1408 (calc-number-radix 10)
1409 (calc-point-char ".")
1413 (message "Formatting units table...")
1415 (let ((inhibit-read-only t
))
1417 (insert "Calculator Units Table:\n\n")
1418 (insert "Unit Type Definition Description\n\n")
1422 (when (eq (car u
) 'm
)
1424 (setq shadowed
(and std
(assq (car u
) math-additional-units
)))
1427 (eq (aref name
0) ?\
*))
1428 (unless (eq uptr math-units-table
)
1430 (setq name
(substring name
1)))
1432 (and shadowed
(insert "("))
1433 (insert (symbol-name (car u
)))
1434 (and shadowed
(insert ")"))
1438 (insert (symbol-name (nth 3 u
))))
1444 (and shadowed
(insert "("))
1446 (insert (math-format-value (nth 1 u
) 80))
1447 (insert (symbol-name (car u
))))
1448 (and shadowed
(insert ")"))
1454 (insert " (redefined above)")
1456 (insert " (base unit)")))
1458 (setq uptr
(cdr uptr
)))
1459 (insert "\n\nUnit Prefix Table:\n\n")
1460 (setq uptr math-unit-prefixes
)
1463 (insert " " (char-to-string (car u
)))
1464 (if (equal (nth 1 u
) (nth 1 (nth 1 uptr
)))
1465 (insert " " (char-to-string (car (car (setq uptr
(cdr uptr
)))))
1468 (insert "10^" (int-to-string (nth 2 (nth 1 u
))))
1470 (insert " " (nth 2 u
) "\n")
1471 (while (eq (car (car (setq uptr
(cdr uptr
)))) 0)))
1474 (message "Formatting units table...done"))
1475 (setq math-units-table-buffer-valid t
)
1476 (let ((oldbuf (current-buffer)))
1478 (goto-char (point-min))
1479 (set-buffer oldbuf
))
1482 (display-buffer buf
)))
1484 (pop-to-buffer (get-buffer "*Units Table*"))
1485 (display-buffer (get-buffer "*Units Table*")))))
1487 (provide 'calc-units
)
1490 ;; coding: iso-latin-1
1493 ;;; arch-tag: e993314f-3adc-4191-be61-4ef8874881c4
1494 ;;; calc-units.el ends here