(math-zerop, math-negp, math-looks-negp)
[bpt/emacs.git] / lisp / calc / calc-misc.el
CommitLineData
21adaa28 1;;; calc-misc.el --- miscellaneous functions for Calc
dac12d80 2
58ba2f8f 3;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004
f0fa15c5 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
dac12d80
CW
5
6;; Author: David Gillespie <daveg@synaptics.com>
e8fff8ed 7;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
136211a9
EZ
8
9;; This file is part of GNU Emacs.
10
7c671b23
GM
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
075969b4 13;; the Free Software Foundation; either version 3, or (at your option)
7c671b23
GM
14;; any later version.
15
136211a9 16;; GNU Emacs is distributed in the hope that it will be useful,
7c671b23
GM
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.
20
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.
136211a9 25
dac12d80 26;;; Commentary:
136211a9 27
dac12d80 28;;; Code:
136211a9
EZ
29
30;; This file is autoloaded from calc.el.
136211a9 31
877dc4f5 32(require 'calc)
136211a9
EZ
33(require 'calc-macs)
34
136211a9 35(defun calc-dispatch-help (arg)
dcf4a535 36 "C-x* is a prefix key sequence; follow it with one of these letters:
136211a9
EZ
37
38For turning Calc on and off:
39 C calc. Start the Calculator in a window at the bottom of the screen.
40 O calc-other-window. Start the Calculator but don't select its window.
41 B calc-big-or-small. Control whether to use the full Emacs screen for Calc.
42 Q quick-calc. Use the Calculator in the minibuffer.
43 K calc-keypad. Start the Calculator in keypad mode (X window system only).
44 E calc-embedded. Use the Calculator on a formula in this editing buffer.
45 J calc-embedded-select. Like E, but select appropriate half of => or :=.
46 W calc-embedded-word. Like E, but activate a single word, i.e., a number.
47 Z calc-user-invocation. Invoke Calc in the way you defined with `Z I' cmd.
48 X calc-quit. Turn Calc off.
49
50For moving data into and out of Calc:
51 G calc-grab-region. Grab the region defined by mark and point into Calc.
52 R calc-grab-rectangle. Grab the rectangle defined by mark, point into Calc.
53 : calc-grab-sum-down. Grab a rectangle and sum the columns.
54 _ calc-grab-sum-across. Grab a rectangle and sum the rows.
55 Y calc-copy-to-buffer. Copy a value from the stack into the editing buffer.
56
57For use with Embedded mode:
58 A calc-embedded-activate. Find and activate all :='s and =>'s in buffer.
59 D calc-embedded-duplicate. Make a copy of this formula and select it.
60 F calc-embedded-new-formula. Insert a new formula at current point.
61 N calc-embedded-next. Advance cursor to next known formula in buffer.
62 P calc-embedded-previous. Advance cursor to previous known formula.
63 U calc-embedded-update-formula. Re-evaluate formula at point.
64 ` calc-embedded-edit. Use calc-edit to edit formula at point.
65
66Documentation:
67 I calc-info. Read the Calculator manual in the Emacs Info system.
68 T calc-tutorial. Run the Calculator Tutorial using the Emacs Info system.
69 S calc-summary. Read the Summary from the Calculator manual in Info.
70
71Miscellaneous:
72 L calc-load-everything. Load all parts of the Calculator into memory.
73 M read-kbd-macro. Read a region of keystroke names as a keyboard macro.
74 0 (zero) calc-reset. Reset Calc stack and modes to default state.
75
dcf4a535
JB
76Press `*' twice (`C-x * *') to turn Calc on or off using the same
77Calc user interface as before (either C-x * C or C-x * K; initially C-x * C).
78"
136211a9
EZ
79 (interactive "P")
80 (calc-check-defines)
81 (if calc-dispatch-help
82 (progn
83 (save-window-excursion
84 (describe-function 'calc-dispatch-help)
85 (let ((win (get-buffer-window "*Help*")))
86 (if win
87 (let (key)
88 (select-window win)
89 (while (progn
90 (message "Calc options: Calc, Keypad, ... %s"
91 "press SPC, DEL to scroll, C-g to cancel")
92 (memq (car (setq key (calc-read-key t)))
93 '(? ?\C-h ?\C-? ?\C-v ?\M-v)))
94 (condition-case err
95 (if (memq (car key) '(? ?\C-v))
96 (scroll-up)
97 (scroll-down))
98 (error (beep))))
99 (calc-unread-command (cdr key))))))
100 (calc-do-dispatch nil))
101 (let ((calc-dispatch-help t))
60e4c5a7 102 (calc-do-dispatch arg))))
136211a9
EZ
103
104
105(defun calc-big-or-small (arg)
106 "Toggle Calc between full-screen and regular mode."
107 (interactive "P")
108 (let ((cwin (get-buffer-window "*Calculator*"))
109 (twin (get-buffer-window "*Calc Trail*"))
110 (kwin (get-buffer-window "*Calc Keypad*")))
111 (if cwin
112 (setq calc-full-mode
113 (if kwin
8f66f479
EZ
114 (and twin (eq (window-width twin) (frame-width)))
115 (eq (window-height cwin) (1- (frame-height))))))
136211a9
EZ
116 (setq calc-full-mode (if arg
117 (> (prefix-numeric-value arg) 0)
118 (not calc-full-mode)))
119 (if kwin
120 (progn
121 (calc-quit)
122 (calc-do-keypad calc-full-mode nil))
123 (if cwin
124 (progn
125 (calc-quit)
126 (calc nil calc-full-mode nil))))
127 (message (if calc-full-mode
dac12d80
CW
128 "Now using full screen for Calc"
129 "Now using partial screen for Calc"))))
136211a9 130
1f5a0f5d 131(defun calc-other-window (&optional interactive)
136211a9 132 "Invoke the Calculator in another window."
1f5a0f5d 133 (interactive "p")
136211a9
EZ
134 (if (memq major-mode '(calc-mode calc-trail-mode))
135 (progn
136 (other-window 1)
137 (if (memq major-mode '(calc-mode calc-trail-mode))
138 (other-window 1)))
139 (if (get-buffer-window "*Calculator*")
140 (calc-quit)
141 (let ((win (selected-window)))
1f5a0f5d 142 (calc nil win interactive)))))
136211a9
EZ
143
144(defun another-calc ()
145 "Create another, independent Calculator buffer."
146 (interactive)
147 (if (eq major-mode 'calc-mode)
ab58914b
JB
148 (mapc (function
149 (lambda (v)
150 (set-default v (symbol-value v)))) calc-local-var-list))
136211a9
EZ
151 (set-buffer (generate-new-buffer "*Calculator*"))
152 (pop-to-buffer (current-buffer))
60e4c5a7 153 (calc-mode))
136211a9 154
136211a9
EZ
155(defun calc-info ()
156 "Run the Emacs Info system on the Calculator documentation."
157 (interactive)
136211a9 158 (select-window (get-largest-window))
b23d58ff 159 (info "Calc"))
136211a9 160
65ce291b
JB
161(defun calc-info-goto-node (node)
162 "Go to a node in the Calculator info documentation."
163 (interactive)
164 (select-window (get-largest-window))
1565a620 165 (info (concat "(Calc)" node)))
65ce291b 166
136211a9
EZ
167(defun calc-tutorial ()
168 "Run the Emacs Info system on the Calculator Tutorial."
169 (interactive)
170 (if (get-buffer-window "*Calculator*")
171 (calc-quit))
65ce291b 172 (calc-info-goto-node "Interactive Tutorial")
136211a9 173 (calc-other-window)
60e4c5a7 174 (message "Welcome to the Calc Tutorial!"))
136211a9
EZ
175
176(defun calc-info-summary ()
177 "Run the Emacs Info system on the Calculator Summary."
178 (interactive)
65ce291b 179 (calc-info-goto-node "Summary"))
136211a9
EZ
180
181(defun calc-help ()
182 (interactive)
183 (let ((msgs (append
184 '("Press `h' for complete help; press `?' repeatedly for a summary"
185 "Letter keys: Negate; Precision; Yank; Why; Xtended cmd; Quit"
186 "Letter keys: SHIFT + Undo, reDo; Keep-args; Inverse, Hyperbolic"
187 "Letter keys: SHIFT + sQrt; Sin, Cos, Tan; Exp, Ln, logB"
188 "Letter keys: SHIFT + Floor, Round; Abs, conJ, arG; Pi"
189 "Letter keys: SHIFT + Num-eval; More-recn; eXec-kbd-macro"
190 "Other keys: +, -, *, /, ^, \\ (int div), : (frac div)"
191 "Other keys: & (1/x), | (concat), % (modulo), ! (factorial)"
192 "Other keys: ' (alg-entry), = (eval), ` (edit); M-RET (last-args)"
193 "Other keys: SPC/RET (enter/dup), LFD (over); < > (scroll horiz)"
194 "Other keys: DEL (drop), M-DEL (drop-above); { } (scroll vert)"
195 "Other keys: TAB (swap/roll-dn), M-TAB (roll-up)"
196 "Other keys: [ , ; ] (vector), ( , ) (complex), ( ; ) (polar)"
197 "Prefix keys: Algebra, Binary/business, Convert, Display"
198 "Prefix keys: Functions, Graphics, Help, J (select)"
199 "Prefix keys: Kombinatorics/statistics, Modes, Store/recall"
200 "Prefix keys: Trail/time, Units/statistics, Vector/matrix"
201 "Prefix keys: Z (user), SHIFT + Z (define)"
202 "Prefix keys: prefix + ? gives further help for that prefix")
203 (list (format
204 " Calc %s by Dave Gillespie, daveg@synaptics.com"
205 calc-version)))))
206 (if calc-full-help-flag
207 msgs
208 (if (or calc-inverse-flag calc-hyperbolic-flag)
209 (if calc-inverse-flag
210 (if calc-hyperbolic-flag
211 (calc-inv-hyp-prefix-help)
212 (calc-inverse-prefix-help))
213 (calc-hyperbolic-prefix-help))
214 (setq calc-help-phase
215 (if (eq this-command last-command)
216 (% (1+ calc-help-phase) (1+ (length msgs)))
217 0))
218 (let ((msg (nth calc-help-phase msgs)))
219 (message "%s" (if msg
220 (concat msg ":"
221 (make-string (- (apply 'max
222 (mapcar 'length
223 msgs))
224 (length msg)) 32)
225 " [?=MORE]")
60e4c5a7 226 "")))))))
136211a9
EZ
227
228
229
230
231;;;; Stack and buffer management.
232
f58af899
JB
233;; The variable calc-last-why-command is set in calc-do-handly-whys
234;; and used in calc-why (in calc-stuff.el).
235(defvar calc-last-why-command)
136211a9
EZ
236
237(defun calc-do-handle-whys ()
238 (setq calc-why (sort calc-next-why
239 (function
240 (lambda (x y)
241 (and (eq (car x) '*) (not (eq (car y) '*))))))
242 calc-next-why nil)
243 (if (and calc-why (or (eq calc-auto-why t)
244 (and (eq (car (car calc-why)) '*)
245 calc-auto-why)))
246 (progn
c6d32405 247 (require 'calc-ext)
136211a9
EZ
248 (calc-explain-why (car calc-why)
249 (if (eq calc-auto-why t)
250 (cdr calc-why)
251 (if calc-auto-why
252 (eq (car (nth 1 calc-why)) '*))))
253 (setq calc-last-why-command this-command)
60e4c5a7 254 (calc-clear-command-flag 'clear-message))))
136211a9
EZ
255
256(defun calc-record-why (&rest stuff)
257 (if (eq (car stuff) 'quiet)
258 (setq stuff (cdr stuff))
259 (if (and (symbolp (car stuff))
260 (cdr stuff)
261 (or (Math-objectp (nth 1 stuff))
262 (and (Math-vectorp (nth 1 stuff))
263 (math-constp (nth 1 stuff)))
264 (math-infinitep (nth 1 stuff))))
265 (setq stuff (cons '* stuff))
266 (if (and (stringp (car stuff))
267 (string-match "\\`\\*" (car stuff)))
268 (setq stuff (cons '* (cons (substring (car stuff) 1)
269 (cdr stuff)))))))
270 (setq calc-next-why (cons stuff calc-next-why))
60e4c5a7 271 nil)
136211a9
EZ
272
273;;; True if A is a constant or vector of constants. [P x] [Public]
274(defun math-constp (a)
275 (or (Math-scalarp a)
276 (and (memq (car a) '(sdev intv mod vec))
277 (progn
278 (while (and (setq a (cdr a))
279 (or (Math-scalarp (car a)) ; optimization
280 (math-constp (car a)))))
60e4c5a7 281 (null a)))))
136211a9
EZ
282
283
284(defun calc-roll-down-stack (n &optional m)
285 (if (< n 0)
286 (calc-roll-up-stack (- n) m)
287 (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size)))
288 (or m (setq m 1))
289 (and (> n 1)
290 (< m n)
291 (if (and calc-any-selections
292 (not calc-use-selections))
293 (calc-roll-down-with-selections n m)
294 (calc-pop-push-list n
295 (append (calc-top-list m 1)
60e4c5a7 296 (calc-top-list (- n m) (1+ m))))))))
136211a9
EZ
297
298(defun calc-roll-up-stack (n &optional m)
299 (if (< n 0)
300 (calc-roll-down-stack (- n) m)
301 (if (or (= n 0) (> n (calc-stack-size))) (setq n (calc-stack-size)))
302 (or m (setq m 1))
303 (and (> n 1)
304 (< m n)
305 (if (and calc-any-selections
306 (not calc-use-selections))
307 (calc-roll-up-with-selections n m)
308 (calc-pop-push-list n
309 (append (calc-top-list (- n m) 1)
60e4c5a7 310 (calc-top-list m (- n m -1))))))))
136211a9
EZ
311
312
313(defun calc-do-refresh ()
314 (if calc-hyperbolic-flag
315 (progn
316 (setq calc-display-dirty t)
317 nil)
318 (calc-refresh)
60e4c5a7 319 t))
136211a9
EZ
320
321
322(defun calc-record-list (vals &optional prefix)
323 (while vals
324 (or (eq (car vals) 'top-of-stack)
325 (progn
326 (calc-record (car vals) prefix)
327 (setq prefix "...")))
60e4c5a7 328 (setq vals (cdr vals))))
136211a9
EZ
329
330
331(defun calc-last-args-stub (arg)
332 (interactive "p")
c6d32405 333 (require 'calc-ext)
60e4c5a7 334 (calc-last-args arg))
136211a9
EZ
335
336
337(defun calc-power (arg)
338 (interactive "P")
339 (calc-slow-wrapper
c6d32405 340 (if (and (featurep 'calc-ext)
136211a9
EZ
341 (calc-is-inverse))
342 (calc-binary-op "root" 'calcFunc-nroot arg nil nil)
60e4c5a7 343 (calc-binary-op "^" 'calcFunc-pow arg nil nil '^))))
136211a9
EZ
344
345(defun calc-mod (arg)
346 (interactive "P")
347 (calc-slow-wrapper
60e4c5a7 348 (calc-binary-op "%" 'calcFunc-mod arg nil nil '%)))
136211a9
EZ
349
350(defun calc-inv (arg)
351 (interactive "P")
352 (calc-slow-wrapper
60e4c5a7 353 (calc-unary-op "inv" 'calcFunc-inv arg)))
136211a9
EZ
354
355(defun calc-percent ()
356 (interactive)
357 (calc-slow-wrapper
358 (calc-pop-push-record-list
60e4c5a7 359 1 "%" (list (list 'calcFunc-percent (calc-top-n 1))))))
136211a9
EZ
360
361
362(defun calc-over (n)
363 (interactive "P")
364 (if n
365 (calc-enter (- (prefix-numeric-value n)))
60e4c5a7 366 (calc-enter -2)))
136211a9
EZ
367
368
369(defun calc-pop-above (n)
370 (interactive "P")
371 (if n
372 (calc-pop (- (prefix-numeric-value n)))
60e4c5a7 373 (calc-pop -2)))
136211a9
EZ
374
375(defun calc-roll-down (n)
376 (interactive "P")
377 (calc-wrapper
378 (let ((nn (prefix-numeric-value n)))
379 (cond ((null n)
380 (calc-roll-down-stack 2))
381 ((> nn 0)
382 (calc-roll-down-stack nn))
383 ((= nn 0)
384 (calc-pop-push-list (calc-stack-size)
385 (reverse
386 (calc-top-list (calc-stack-size)))))
387 (t
60e4c5a7 388 (calc-roll-down-stack (calc-stack-size) (- nn)))))))
136211a9
EZ
389
390(defun calc-roll-up (n)
391 (interactive "P")
392 (calc-wrapper
393 (let ((nn (prefix-numeric-value n)))
394 (cond ((null n)
395 (calc-roll-up-stack 3))
396 ((> nn 0)
397 (calc-roll-up-stack nn))
398 ((= nn 0)
399 (calc-pop-push-list (calc-stack-size)
400 (reverse
401 (calc-top-list (calc-stack-size)))))
402 (t
60e4c5a7 403 (calc-roll-up-stack (calc-stack-size) (- nn)))))))
136211a9
EZ
404
405
406
407
408;;; Other commands.
409
410(defun calc-num-prefix-name (n)
411 (cond ((eq n '-) "- ")
412 ((equal n '(4)) "C-u ")
413 ((consp n) (format "%d " (car n)))
414 ((integerp n) (format "%d " n))
60e4c5a7 415 (t "")))
136211a9
EZ
416
417(defun calc-missing-key (n)
418 "This is a placeholder for a command which needs to be loaded from calc-ext.
419When this key is used, calc-ext (the Calculator extensions module) will be
420loaded and the keystroke automatically re-typed."
421 (interactive "P")
c6d32405 422 (require 'calc-ext)
136211a9
EZ
423 (if (keymapp (key-binding (char-to-string last-command-char)))
424 (message "%s%c-" (calc-num-prefix-name n) last-command-char))
425 (calc-unread-command)
60e4c5a7 426 (setq prefix-arg n))
136211a9
EZ
427
428(defun calc-shift-Y-prefix-help ()
429 (interactive)
c6d32405 430 (require 'calc-ext)
60e4c5a7 431 (calc-do-prefix-help calc-Y-help-msgs "other" ?Y))
136211a9
EZ
432
433
434
435
436(defun calcDigit-letter ()
437 (interactive)
438 (if (calc-minibuffer-contains "[-+]?\\(1[1-9]\\|[2-9][0-9]\\)#.*")
439 (progn
440 (setq last-command-char (upcase last-command-char))
441 (calcDigit-key))
60e4c5a7 442 (calcDigit-nondigit)))
136211a9
EZ
443
444
445;; A Lisp version of temp_minibuffer_message from minibuf.c.
446(defun calc-temp-minibuffer-message (m)
447 (let ((savemax (point-max)))
448 (save-excursion
449 (goto-char (point-max))
450 (insert m))
451 (let ((okay nil))
452 (unwind-protect
453 (progn
454 (sit-for 2)
455 (identity 1) ; this forces a call to QUIT; in bytecode.c.
456 (setq okay t))
457 (progn
458 (delete-region savemax (point-max))
60e4c5a7 459 (or okay (abort-recursive-edit)))))))
136211a9
EZ
460
461
462(put 'math-with-extra-prec 'lisp-indent-hook 1)
463
464
465;;; Concatenate two vectors, or a vector and an object. [V O O] [Public]
466(defun math-concat (v1 v2)
467 (if (stringp v1)
468 (concat v1 v2)
c6d32405 469 (require 'calc-ext)
136211a9
EZ
470 (if (and (or (math-objvecp v1) (math-known-scalarp v1))
471 (or (math-objvecp v2) (math-known-scalarp v2)))
472 (append (if (and (math-vectorp v1)
473 (or (math-matrixp v1)
474 (not (math-matrixp v2))))
475 v1
476 (list 'vec v1))
477 (if (and (math-vectorp v2)
478 (or (math-matrixp v2)
479 (not (math-matrixp v1))))
480 (cdr v2)
481 (list v2)))
60e4c5a7 482 (list '| v1 v2))))
136211a9
EZ
483
484
485;;; True if A is zero. Works for un-normalized values. [P n] [Public]
486(defun math-zerop (a)
487 (if (consp a)
488 (cond ((memq (car a) '(bigpos bigneg))
489 (while (eq (car (setq a (cdr a))) 0))
490 (null a))
491 ((memq (car a) '(frac float polar mod))
492 (math-zerop (nth 1 a)))
493 ((eq (car a) 'cplx)
494 (and (math-zerop (nth 1 a)) (math-zerop (nth 2 a))))
495 ((eq (car a) 'hms)
496 (and (math-zerop (nth 1 a))
497 (math-zerop (nth 2 a))
498 (math-zerop (nth 3 a)))))
60e4c5a7 499 (eq a 0)))
136211a9
EZ
500
501
502;;; True if A is real and negative. [P n] [Public]
503
504(defun math-negp (a)
505 (if (consp a)
506 (cond ((eq (car a) 'bigpos) nil)
507 ((eq (car a) 'bigneg) (cdr a))
508 ((memq (car a) '(float frac))
509 (Math-integer-negp (nth 1 a)))
510 ((eq (car a) 'hms)
511 (if (math-zerop (nth 1 a))
512 (if (math-zerop (nth 2 a))
513 (math-negp (nth 3 a))
514 (math-negp (nth 2 a)))
515 (math-negp (nth 1 a))))
516 ((eq (car a) 'date)
517 (math-negp (nth 1 a)))
518 ((eq (car a) 'intv)
519 (or (math-negp (nth 3 a))
520 (and (math-zerop (nth 3 a))
521 (memq (nth 1 a) '(0 2)))))
522 ((equal a '(neg (var inf var-inf))) t))
60e4c5a7 523 (< a 0)))
136211a9
EZ
524
525;;; True if A is a negative number or an expression the starts with '-'.
526(defun math-looks-negp (a) ; [P x] [Public]
527 (or (Math-negp a)
528 (eq (car-safe a) 'neg)
529 (and (memq (car-safe a) '(* /))
530 (or (math-looks-negp (nth 1 a))
531 (math-looks-negp (nth 2 a))))
532 (and (eq (car-safe a) '-)
60e4c5a7 533 (math-looks-negp (nth 1 a)))))
136211a9
EZ
534
535
536;;; True if A is real and positive. [P n] [Public]
537(defun math-posp (a)
538 (if (consp a)
539 (cond ((eq (car a) 'bigpos) (cdr a))
540 ((eq (car a) 'bigneg) nil)
541 ((memq (car a) '(float frac))
542 (Math-integer-posp (nth 1 a)))
543 ((eq (car a) 'hms)
544 (if (math-zerop (nth 1 a))
545 (if (math-zerop (nth 2 a))
546 (math-posp (nth 3 a))
547 (math-posp (nth 2 a)))
548 (math-posp (nth 1 a))))
549 ((eq (car a) 'date)
550 (math-posp (nth 1 a)))
551 ((eq (car a) 'mod)
552 (not (math-zerop (nth 1 a))))
553 ((eq (car a) 'intv)
554 (or (math-posp (nth 2 a))
555 (and (math-zerop (nth 2 a))
556 (memq (nth 1 a) '(0 1)))))
557 ((equal a '(var inf var-inf)) t))
60e4c5a7 558 (> a 0)))
136211a9 559
dac12d80
CW
560(defalias 'math-fixnump 'integerp)
561(defalias 'math-fixnatnump 'natnump)
136211a9
EZ
562
563
564;;; True if A is an even integer. [P R R] [Public]
565(defun math-evenp (a)
566 (if (consp a)
567 (and (memq (car a) '(bigpos bigneg))
568 (= (% (nth 1 a) 2) 0))
60e4c5a7 569 (= (% a 2) 0)))
136211a9
EZ
570
571;;; Compute A / 2, for small or big integer A. [I i]
572;;; If A is negative, type of truncation is undefined.
573(defun math-div2 (a)
574 (if (consp a)
575 (if (cdr a)
576 (math-normalize (cons (car a) (math-div2-bignum (cdr a))))
577 0)
60e4c5a7 578 (/ a 2)))
136211a9
EZ
579
580(defun math-div2-bignum (a) ; [l l]
581 (if (cdr a)
f164b8c8 582 (cons (+ (/ (car a) 2) (* (% (nth 1 a) 2) (/ math-bignum-digit-size 2)))
136211a9 583 (math-div2-bignum (cdr a)))
60e4c5a7 584 (list (/ (car a) 2))))
136211a9
EZ
585
586
587;;; Reject an argument to a calculator function. [Public]
588(defun math-reject-arg (&optional a p option)
589 (if option
590 (calc-record-why option p a)
591 (if p
592 (calc-record-why p a)))
60e4c5a7 593 (signal 'wrong-type-argument (and a (if p (list p a) (list a)))))
136211a9
EZ
594
595
596;;; Coerce A to be an integer (by truncation toward zero). [I N] [Public]
276d2865
JB
597
598;; The variable math-trunc-prec is local to math-trunc, but used by
599;; math-trunc-fancy in calc-arith.el, which is called by math-trunc.
600
601(defun math-trunc (a &optional math-trunc-prec)
602 (cond (math-trunc-prec
c6d32405 603 (require 'calc-ext)
276d2865 604 (math-trunc-special a math-trunc-prec))
136211a9
EZ
605 ((Math-integerp a) a)
606 ((Math-looks-negp a)
607 (math-neg (math-trunc (math-neg a))))
608 ((eq (car a) 'float)
609 (math-scale-int (nth 1 a) (nth 2 a)))
c6d32405 610 (t (require 'calc-ext)
60e4c5a7 611 (math-trunc-fancy a))))
dac12d80 612(defalias 'calcFunc-trunc 'math-trunc)
136211a9
EZ
613
614;;; Coerce A to be an integer (by truncation toward minus infinity). [I N]
276d2865
JB
615
616;; The variable math-floor-prec is local to math-floor, but used by
617;; math-floor-fancy in calc-arith.el, which is called by math-floor.
618
619(defun math-floor (a &optional math-floor-prec) ; [Public]
620 (cond (math-floor-prec
c6d32405 621 (require 'calc-ext)
276d2865 622 (math-floor-special a math-floor-prec))
136211a9
EZ
623 ((Math-integerp a) a)
624 ((Math-messy-integerp a) (math-trunc a))
625 ((Math-realp a)
626 (if (Math-negp a)
627 (math-add (math-trunc a) -1)
628 (math-trunc a)))
c6d32405 629 (t (require 'calc-ext)
60e4c5a7 630 (math-floor-fancy a))))
dac12d80 631(defalias 'calcFunc-floor 'math-floor)
136211a9
EZ
632
633
634(defun math-imod (a b) ; [I I I] [Public]
635 (if (and (not (consp a)) (not (consp b)))
636 (if (= b 0)
637 (math-reject-arg a "*Division by zero")
638 (% a b))
60e4c5a7 639 (cdr (math-idivmod a b))))
136211a9
EZ
640
641
642(defun calcFunc-inv (m)
643 (if (Math-vectorp m)
644 (progn
c6d32405 645 (require 'calc-ext)
136211a9
EZ
646 (if (math-square-matrixp m)
647 (or (math-with-extra-prec 2 (math-matrix-inv-raw m))
648 (math-reject-arg m "*Singular matrix"))
649 (math-reject-arg m 'square-matrixp)))
5c0e273a
JB
650 (if (and
651 (require 'calc-arith)
652 (math-known-matrixp m))
653 (math-pow m -1)
654 (math-div 1 m))))
136211a9
EZ
655
656(defun math-do-working (msg arg)
8f66f479 657 (or executing-kbd-macro
136211a9
EZ
658 (progn
659 (calc-set-command-flag 'clear-message)
660 (if math-working-step
661 (if math-working-step-2
662 (setq msg (format "[%d/%d] %s"
663 math-working-step math-working-step-2 msg))
664 (setq msg (format "[%d] %s" math-working-step msg))))
665 (message "Working... %s = %s" msg
60e4c5a7 666 (math-showing-full-precision (math-format-number arg))))))
136211a9
EZ
667
668
669;;; Compute A modulo B, defined in terms of truncation toward minus infinity.
670(defun math-mod (a b) ; [R R R] [Public]
671 (cond ((and (Math-zerop a) (not (eq (car-safe a) 'mod))) a)
672 ((Math-zerop b)
673 (math-reject-arg a "*Division by zero"))
674 ((and (Math-natnump a) (Math-natnump b))
675 (math-imod a b))
676 ((and (Math-anglep a) (Math-anglep b))
677 (math-sub a (math-mul (math-floor (math-div a b)) b)))
c6d32405 678 (t (require 'calc-ext)
60e4c5a7 679 (math-mod-fancy a b))))
136211a9
EZ
680
681
682
683;;; General exponentiation.
684
685(defun math-pow (a b) ; [O O N] [Public]
686 (cond ((equal b '(var nan var-nan))
687 b)
688 ((Math-zerop a)
689 (if (and (Math-scalarp b) (Math-posp b))
690 (if (math-floatp b) (math-float a) a)
c6d32405 691 (require 'calc-ext)
136211a9
EZ
692 (math-pow-of-zero a b)))
693 ((or (eq a 1) (eq b 1)) a)
694 ((or (equal a '(float 1 0)) (equal b '(float 1 0))) a)
695 ((Math-zerop b)
696 (if (Math-scalarp a)
697 (if (or (math-floatp a) (math-floatp b))
698 '(float 1 0) 1)
c6d32405 699 (require 'calc-ext)
136211a9
EZ
700 (math-pow-zero a b)))
701 ((and (Math-integerp b) (or (Math-numberp a) (Math-vectorp a)))
702 (if (and (equal a '(float 1 1)) (integerp b))
703 (math-make-float 1 b)
704 (math-with-extra-prec 2
705 (math-ipow a b))))
706 (t
c6d32405 707 (require 'calc-ext)
60e4c5a7 708 (math-pow-fancy a b))))
136211a9
EZ
709
710(defun math-ipow (a n) ; [O O I] [Public]
711 (cond ((Math-integer-negp n)
712 (math-ipow (math-div 1 a) (Math-integer-neg n)))
713 ((not (consp n))
714 (if (and (Math-ratp a) (> n 20))
715 (math-iipow-show a n)
716 (math-iipow a n)))
717 ((math-evenp n)
718 (math-ipow (math-mul a a) (math-div2 n)))
719 (t
720 (math-mul a (math-ipow (math-mul a a)
60e4c5a7 721 (math-div2 (math-add n -1)))))))
136211a9
EZ
722
723(defun math-iipow (a n) ; [O O S]
724 (cond ((= n 0) 1)
725 ((= n 1) a)
726 ((= (% n 2) 0) (math-iipow (math-mul a a) (/ n 2)))
60e4c5a7 727 (t (math-mul a (math-iipow (math-mul a a) (/ n 2))))))
136211a9
EZ
728
729(defun math-iipow-show (a n) ; [O O S]
730 (math-working "pow" a)
731 (let ((val (cond
732 ((= n 0) 1)
733 ((= n 1) a)
734 ((= (% n 2) 0) (math-iipow-show (math-mul a a) (/ n 2)))
735 (t (math-mul a (math-iipow-show (math-mul a a) (/ n 2)))))))
736 (math-working "pow" val)
60e4c5a7 737 val))
136211a9
EZ
738
739
740(defun math-read-radix-digit (dig) ; [D S; Z S]
741 (if (> dig ?9)
742 (if (< dig ?A)
743 nil
744 (- dig 55))
745 (if (>= dig ?0)
746 (- dig ?0)
60e4c5a7 747 nil)))
136211a9
EZ
748
749
136211a9
EZ
750;;; Bug reporting
751
afb55b71 752(defun report-calc-bug ()
136211a9
EZ
753 "Report a bug in Calc, the GNU Emacs calculator.
754Prompts for bug subject. Leaves you in a mail buffer."
afb55b71
CW
755 (interactive)
756 (let ((reporter-prompt-for-summary-p t))
757 (reporter-submit-bug-report calc-bug-address "Calc" '(calc-version)
758 nil nil
759 "Please describe exactly what actions triggered the bug and the
8f148852
CW
760precise symptoms of the bug. If possible, include a backtrace by
761doing 'M-x toggle-debug-on-error', then reproducing the bug.
afb55b71 762" )))
dac12d80 763(defalias 'calc-report-bug 'report-calc-bug)
136211a9 764
877dc4f5
JB
765(provide 'calc-misc)
766
ab5796a9 767;;; arch-tag: 7984d9d0-62e5-41dc-afb8-e904b975f250
60e4c5a7 768;;; calc-misc.el ends here