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