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