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