Mercurial > emacs
annotate lisp/calc/calc-bin.el @ 41266:c08a55ae8e5d
(calc-wrapper, calc-slow-wrapper)
(math-showing-full-precision, math-with-extra-prec, math-working)
(calc-with-default-simplification)
(calc-with-trail-buffer): Use backtick.
(Math-zerop, Math-integer-negp, Math-integer-posp, Math-negp)
(Math-looks-negp, Math-posp, Math-integerp, Math-natnump)
(Math-ratp, Math-realp, Math-anglep, Math-numberp, Math-scalarp)
(Math-vectorp, Math-messy-integerp, Math-objectp, Math-objvecp)
(Math-integer-neg, Math-equal, Math-lessp, Math-primp)
(Math-num-integerp, Math-bignum-test, Math-equal-int)
(Math-natnum-lessp, math-format-radix-digit): Change to `defsubst'.
(calc-record-compilation-date-macro): Deleted. Callers updated.
(math-format-radix-digit): Move to calc-bin.el.
Change all toplevel `setq' forms to `defvar' forms, and move them
before their first use. Use `when', `unless'. Remove trailing
periods from error forms. Add description and headers suggested by
Emacs Lisp coding conventions.
author | Colin Walters <walters@gnu.org> |
---|---|
date | Mon, 19 Nov 2001 07:35:49 +0000 |
parents | a78b609cb4b1 |
children | fcd507927105 |
rev | line source |
---|---|
40785 | 1 ;; Calculator for GNU Emacs, part II [calc-bin.el] |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
40785 | 3 ;; Written by Dave Gillespie, daveg@synaptics.com. |
4 | |
5 ;; This file is part of GNU Emacs. | |
6 | |
7 ;; GNU Emacs is distributed in the hope that it will be useful, | |
8 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
9 ;; accepts responsibility to anyone for the consequences of using it | |
10 ;; or for whether it serves any particular purpose or works at all, | |
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
12 ;; License for full details. | |
13 | |
14 ;; Everyone is granted permission to copy, modify and redistribute | |
15 ;; GNU Emacs, but only under the conditions described in the | |
16 ;; GNU Emacs General Public License. A copy of this license is | |
17 ;; supposed to have been given to you along with GNU Emacs so you | |
18 ;; can know your rights and responsibilities. It should be in a | |
19 ;; file named COPYING. Among other things, the copyright notice | |
20 ;; and this notice must be preserved on all copies. | |
21 | |
22 | |
23 | |
24 ;; This file is autoloaded from calc-ext.el. | |
25 (require 'calc-ext) | |
26 | |
27 (require 'calc-macs) | |
28 | |
29 (defun calc-Need-calc-bin () nil) | |
30 | |
31 | |
32 ;;; b-prefix binary commands. | |
33 | |
34 (defun calc-and (n) | |
35 (interactive "P") | |
36 (calc-slow-wrapper | |
37 (calc-enter-result 2 "and" | |
38 (append '(calcFunc-and) | |
39 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
40 (and n (list (prefix-numeric-value n))))))) |
40785 | 41 |
42 (defun calc-or (n) | |
43 (interactive "P") | |
44 (calc-slow-wrapper | |
45 (calc-enter-result 2 "or" | |
46 (append '(calcFunc-or) | |
47 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
48 (and n (list (prefix-numeric-value n))))))) |
40785 | 49 |
50 (defun calc-xor (n) | |
51 (interactive "P") | |
52 (calc-slow-wrapper | |
53 (calc-enter-result 2 "xor" | |
54 (append '(calcFunc-xor) | |
55 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
56 (and n (list (prefix-numeric-value n))))))) |
40785 | 57 |
58 (defun calc-diff (n) | |
59 (interactive "P") | |
60 (calc-slow-wrapper | |
61 (calc-enter-result 2 "diff" | |
62 (append '(calcFunc-diff) | |
63 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
64 (and n (list (prefix-numeric-value n))))))) |
40785 | 65 |
66 (defun calc-not (n) | |
67 (interactive "P") | |
68 (calc-slow-wrapper | |
69 (calc-enter-result 1 "not" | |
70 (append '(calcFunc-not) | |
71 (calc-top-list-n 1) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
72 (and n (list (prefix-numeric-value n))))))) |
40785 | 73 |
74 (defun calc-lshift-binary (n) | |
75 (interactive "P") | |
76 (calc-slow-wrapper | |
77 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
78 (calc-enter-result hyp "lsh" | |
79 (append '(calcFunc-lsh) | |
80 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
81 (and n (list (prefix-numeric-value n)))))))) |
40785 | 82 |
83 (defun calc-rshift-binary (n) | |
84 (interactive "P") | |
85 (calc-slow-wrapper | |
86 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
87 (calc-enter-result hyp "rsh" | |
88 (append '(calcFunc-rsh) | |
89 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
90 (and n (list (prefix-numeric-value n)))))))) |
40785 | 91 |
92 (defun calc-lshift-arith (n) | |
93 (interactive "P") | |
94 (calc-slow-wrapper | |
95 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
96 (calc-enter-result hyp "ash" | |
97 (append '(calcFunc-ash) | |
98 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
99 (and n (list (prefix-numeric-value n)))))))) |
40785 | 100 |
101 (defun calc-rshift-arith (n) | |
102 (interactive "P") | |
103 (calc-slow-wrapper | |
104 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
105 (calc-enter-result hyp "rash" | |
106 (append '(calcFunc-rash) | |
107 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
108 (and n (list (prefix-numeric-value n)))))))) |
40785 | 109 |
110 (defun calc-rotate-binary (n) | |
111 (interactive "P") | |
112 (calc-slow-wrapper | |
113 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
114 (calc-enter-result hyp "rot" | |
115 (append '(calcFunc-rot) | |
116 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
117 (and n (list (prefix-numeric-value n)))))))) |
40785 | 118 |
119 (defun calc-clip (n) | |
120 (interactive "P") | |
121 (calc-slow-wrapper | |
122 (calc-enter-result 1 "clip" | |
123 (append '(calcFunc-clip) | |
124 (calc-top-list-n 1) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
125 (and n (list (prefix-numeric-value n))))))) |
40785 | 126 |
127 (defun calc-word-size (n) | |
128 (interactive "P") | |
129 (calc-wrapper | |
130 (or n (setq n (read-string (format "Binary word size: (default %d) " | |
131 calc-word-size)))) | |
132 (setq n (if (stringp n) | |
133 (if (equal n "") | |
134 calc-word-size | |
135 (if (string-match "\\`[-+]?[0-9]+\\'" n) | |
136 (string-to-int n) | |
137 (error "Expected an integer"))) | |
138 (prefix-numeric-value n))) | |
139 (or (= n calc-word-size) | |
140 (if (> (math-abs n) 100) | |
141 (calc-change-mode 'calc-word-size n calc-leading-zeros) | |
142 (calc-change-mode '(calc-word-size calc-previous-modulo) | |
143 (list n (math-power-of-2 (math-abs n))) | |
144 calc-leading-zeros))) | |
145 (if (< n 0) | |
146 (message "Binary word size is %d bits (2's complement)." (- n)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
147 (message "Binary word size is %d bits." n)))) |
40785 | 148 |
149 | |
150 | |
151 | |
152 | |
153 ;;; d-prefix mode commands. | |
154 | |
155 (defun calc-radix (n) | |
156 (interactive "NDisplay radix (2-36): ") | |
157 (calc-wrapper | |
158 (if (and (>= n 2) (<= n 36)) | |
159 (progn | |
160 (calc-change-mode 'calc-number-radix n t) | |
161 ;; also change global value so minibuffer sees it | |
162 (setq-default calc-number-radix calc-number-radix)) | |
163 (setq n calc-number-radix)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
164 (message "Number radix is %d." n))) |
40785 | 165 |
166 (defun calc-decimal-radix () | |
167 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
168 (calc-radix 10)) |
40785 | 169 |
170 (defun calc-binary-radix () | |
171 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
172 (calc-radix 2)) |
40785 | 173 |
174 (defun calc-octal-radix () | |
175 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
176 (calc-radix 8)) |
40785 | 177 |
178 (defun calc-hex-radix () | |
179 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
180 (calc-radix 16)) |
40785 | 181 |
182 (defun calc-leading-zeros (n) | |
183 (interactive "P") | |
184 (calc-wrapper | |
185 (if (calc-change-mode 'calc-leading-zeros n t t) | |
186 (message "Zero-padding integers to %d digits (assuming radix %d)." | |
187 (let* ((calc-internal-prec 6)) | |
188 (math-compute-max-digits (math-abs calc-word-size) | |
189 calc-number-radix)) | |
190 calc-number-radix) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
191 (message "Omitting leading zeros on integers.")))) |
40785 | 192 |
193 | |
194 (defvar math-power-of-2-cache (list 1 2 4 8 16 32 64 128 256 512 1024)) | |
195 (defvar math-big-power-of-2-cache nil) | |
196 (defun math-power-of-2 (n) ; [I I] [Public] | |
197 (if (and (natnump n) (<= n 100)) | |
198 (or (nth n math-power-of-2-cache) | |
199 (let* ((i (length math-power-of-2-cache)) | |
200 (val (nth (1- i) math-power-of-2-cache))) | |
201 (while (<= i n) | |
202 (setq val (math-mul val 2) | |
203 math-power-of-2-cache (nconc math-power-of-2-cache | |
204 (list val)) | |
205 i (1+ i))) | |
206 val)) | |
207 (let ((found (assq n math-big-power-of-2-cache))) | |
208 (if found | |
209 (cdr found) | |
210 (let ((po2 (math-ipow 2 n))) | |
211 (setq math-big-power-of-2-cache | |
212 (cons (cons n po2) math-big-power-of-2-cache)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
213 po2))))) |
40785 | 214 |
215 (defun math-integer-log2 (n) ; [I I] [Public] | |
216 (let ((i 0) | |
217 (p math-power-of-2-cache) | |
218 val) | |
219 (while (and p (Math-natnum-lessp (setq val (car p)) n)) | |
220 (setq p (cdr p) | |
221 i (1+ i))) | |
222 (if p | |
223 (and (equal val n) | |
224 i) | |
225 (while (Math-natnum-lessp | |
226 (prog1 | |
227 (setq val (math-mul val 2)) | |
228 (setq math-power-of-2-cache (nconc math-power-of-2-cache | |
229 (list val)))) | |
230 n) | |
231 (setq i (1+ i))) | |
232 (and (equal val n) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
233 i)))) |
40785 | 234 |
235 | |
236 | |
237 | |
238 ;;; Bitwise operations. | |
239 | |
240 (defun calcFunc-and (a b &optional w) ; [I I I] [Public] | |
241 (cond ((Math-messy-integerp w) | |
242 (calcFunc-and a b (math-trunc w))) | |
243 ((and w (not (integerp w))) | |
244 (math-reject-arg w 'fixnump)) | |
245 ((and (integerp a) (integerp b)) | |
246 (math-clip (logand a b) w)) | |
247 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
248 (math-binary-modulo-args 'calcFunc-and a b w)) | |
249 ((not (Math-num-integerp a)) | |
250 (math-reject-arg a 'integerp)) | |
251 ((not (Math-num-integerp b)) | |
252 (math-reject-arg b 'integerp)) | |
253 (t (math-clip (cons 'bigpos | |
254 (math-and-bignum (math-binary-arg a w) | |
255 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
256 w)))) |
40785 | 257 |
258 (defun math-binary-arg (a w) | |
259 (if (not (Math-integerp a)) | |
260 (setq a (math-trunc a))) | |
261 (if (Math-integer-negp a) | |
262 (math-not-bignum (cdr (math-bignum-test (math-sub -1 a))) | |
263 (math-abs (if w (math-trunc w) calc-word-size))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
264 (cdr (Math-bignum-test a)))) |
40785 | 265 |
266 (defun math-binary-modulo-args (f a b w) | |
267 (let (mod) | |
268 (if (eq (car-safe a) 'mod) | |
269 (progn | |
270 (setq mod (nth 2 a) | |
271 a (nth 1 a)) | |
272 (if (eq (car-safe b) 'mod) | |
273 (if (equal mod (nth 2 b)) | |
274 (setq b (nth 1 b)) | |
275 (math-reject-arg b "*Inconsistent modulos")))) | |
276 (setq mod (nth 2 b) | |
277 b (nth 1 b))) | |
278 (if (Math-messy-integerp mod) | |
279 (setq mod (math-trunc mod)) | |
280 (or (Math-integerp mod) | |
281 (math-reject-arg mod 'integerp))) | |
282 (let ((bits (math-integer-log2 mod))) | |
283 (if bits | |
284 (if w | |
285 (if (/= w bits) | |
286 (calc-record-why | |
287 "*Warning: Modulo inconsistent with word size")) | |
288 (setq w bits)) | |
289 (calc-record-why "*Warning: Modulo is not a power of 2")) | |
290 (math-make-mod (if b | |
291 (funcall f a b w) | |
292 (funcall f a w)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
293 mod)))) |
40785 | 294 |
295 (defun math-and-bignum (a b) ; [l l l] | |
296 (and a b | |
297 (let ((qa (math-div-bignum-digit a 512)) | |
298 (qb (math-div-bignum-digit b 512))) | |
299 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa)) | |
300 (math-norm-bignum (car qb))) | |
301 512 | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
302 (logand (cdr qa) (cdr qb)))))) |
40785 | 303 |
304 (defun calcFunc-or (a b &optional w) ; [I I I] [Public] | |
305 (cond ((Math-messy-integerp w) | |
306 (calcFunc-or a b (math-trunc w))) | |
307 ((and w (not (integerp w))) | |
308 (math-reject-arg w 'fixnump)) | |
309 ((and (integerp a) (integerp b)) | |
310 (math-clip (logior a b) w)) | |
311 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
312 (math-binary-modulo-args 'calcFunc-or a b w)) | |
313 ((not (Math-num-integerp a)) | |
314 (math-reject-arg a 'integerp)) | |
315 ((not (Math-num-integerp b)) | |
316 (math-reject-arg b 'integerp)) | |
317 (t (math-clip (cons 'bigpos | |
318 (math-or-bignum (math-binary-arg a w) | |
319 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
320 w)))) |
40785 | 321 |
322 (defun math-or-bignum (a b) ; [l l l] | |
323 (and (or a b) | |
324 (let ((qa (math-div-bignum-digit a 512)) | |
325 (qb (math-div-bignum-digit b 512))) | |
326 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa)) | |
327 (math-norm-bignum (car qb))) | |
328 512 | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
329 (logior (cdr qa) (cdr qb)))))) |
40785 | 330 |
331 (defun calcFunc-xor (a b &optional w) ; [I I I] [Public] | |
332 (cond ((Math-messy-integerp w) | |
333 (calcFunc-xor a b (math-trunc w))) | |
334 ((and w (not (integerp w))) | |
335 (math-reject-arg w 'fixnump)) | |
336 ((and (integerp a) (integerp b)) | |
337 (math-clip (logxor a b) w)) | |
338 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
339 (math-binary-modulo-args 'calcFunc-xor a b w)) | |
340 ((not (Math-num-integerp a)) | |
341 (math-reject-arg a 'integerp)) | |
342 ((not (Math-num-integerp b)) | |
343 (math-reject-arg b 'integerp)) | |
344 (t (math-clip (cons 'bigpos | |
345 (math-xor-bignum (math-binary-arg a w) | |
346 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
347 w)))) |
40785 | 348 |
349 (defun math-xor-bignum (a b) ; [l l l] | |
350 (and (or a b) | |
351 (let ((qa (math-div-bignum-digit a 512)) | |
352 (qb (math-div-bignum-digit b 512))) | |
353 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa)) | |
354 (math-norm-bignum (car qb))) | |
355 512 | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
356 (logxor (cdr qa) (cdr qb)))))) |
40785 | 357 |
358 (defun calcFunc-diff (a b &optional w) ; [I I I] [Public] | |
359 (cond ((Math-messy-integerp w) | |
360 (calcFunc-diff a b (math-trunc w))) | |
361 ((and w (not (integerp w))) | |
362 (math-reject-arg w 'fixnump)) | |
363 ((and (integerp a) (integerp b)) | |
364 (math-clip (logand a (lognot b)) w)) | |
365 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
366 (math-binary-modulo-args 'calcFunc-diff a b w)) | |
367 ((not (Math-num-integerp a)) | |
368 (math-reject-arg a 'integerp)) | |
369 ((not (Math-num-integerp b)) | |
370 (math-reject-arg b 'integerp)) | |
371 (t (math-clip (cons 'bigpos | |
372 (math-diff-bignum (math-binary-arg a w) | |
373 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
374 w)))) |
40785 | 375 |
376 (defun math-diff-bignum (a b) ; [l l l] | |
377 (and a | |
378 (let ((qa (math-div-bignum-digit a 512)) | |
379 (qb (math-div-bignum-digit b 512))) | |
380 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa)) | |
381 (math-norm-bignum (car qb))) | |
382 512 | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
383 (logand (cdr qa) (lognot (cdr qb))))))) |
40785 | 384 |
385 (defun calcFunc-not (a &optional w) ; [I I] [Public] | |
386 (cond ((Math-messy-integerp w) | |
387 (calcFunc-not a (math-trunc w))) | |
388 ((eq (car-safe a) 'mod) | |
389 (math-binary-modulo-args 'calcFunc-not a nil w)) | |
390 ((and w (not (integerp w))) | |
391 (math-reject-arg w 'fixnump)) | |
392 ((not (Math-num-integerp a)) | |
393 (math-reject-arg a 'integerp)) | |
394 ((< (or w (setq w calc-word-size)) 0) | |
395 (math-clip (calcFunc-not a (- w)) w)) | |
396 (t (math-normalize | |
397 (cons 'bigpos | |
398 (math-not-bignum (math-binary-arg a w) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
399 w)))))) |
40785 | 400 |
401 (defun math-not-bignum (a w) ; [l l] | |
402 (let ((q (math-div-bignum-digit a 512))) | |
403 (if (<= w 9) | |
404 (list (logand (lognot (cdr q)) | |
405 (1- (lsh 1 w)))) | |
406 (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q)) | |
407 (- w 9)) | |
408 512 | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
409 (logxor (cdr q) 511))))) |
40785 | 410 |
411 (defun calcFunc-lsh (a &optional n w) ; [I I] [Public] | |
412 (setq a (math-trunc a) | |
413 n (if n (math-trunc n) 1)) | |
414 (if (eq (car-safe a) 'mod) | |
415 (math-binary-modulo-args 'calcFunc-lsh a n w) | |
416 (setq w (if w (math-trunc w) calc-word-size)) | |
417 (or (integerp w) | |
418 (math-reject-arg w 'fixnump)) | |
419 (or (Math-integerp a) | |
420 (math-reject-arg a 'integerp)) | |
421 (or (Math-integerp n) | |
422 (math-reject-arg n 'integerp)) | |
423 (if (< w 0) | |
424 (math-clip (calcFunc-lsh a n (- w)) w) | |
425 (if (Math-integer-negp a) | |
426 (setq a (math-clip a w))) | |
427 (cond ((or (Math-lessp n (- w)) | |
428 (Math-lessp w n)) | |
429 0) | |
430 ((< n 0) | |
431 (math-quotient (math-clip a w) (math-power-of-2 (- n)))) | |
432 (t | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
433 (math-clip (math-mul a (math-power-of-2 n)) w)))))) |
40785 | 434 |
435 (defun calcFunc-rsh (a &optional n w) ; [I I] [Public] | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
436 (calcFunc-lsh a (math-neg (or n 1)) w)) |
40785 | 437 |
438 (defun calcFunc-ash (a &optional n w) ; [I I] [Public] | |
439 (if (or (null n) | |
440 (not (Math-negp n))) | |
441 (calcFunc-lsh a n w) | |
442 (setq a (math-trunc a) | |
443 n (if n (math-trunc n) 1)) | |
444 (if (eq (car-safe a) 'mod) | |
445 (math-binary-modulo-args 'calcFunc-ash a n w) | |
446 (setq w (if w (math-trunc w) calc-word-size)) | |
447 (or (integerp w) | |
448 (math-reject-arg w 'fixnump)) | |
449 (or (Math-integerp a) | |
450 (math-reject-arg a 'integerp)) | |
451 (or (Math-integerp n) | |
452 (math-reject-arg n 'integerp)) | |
453 (if (< w 0) | |
454 (math-clip (calcFunc-ash a n (- w)) w) | |
455 (if (Math-integer-negp a) | |
456 (setq a (math-clip a w))) | |
457 (let ((two-to-sizem1 (math-power-of-2 (1- w))) | |
458 (sh (calcFunc-lsh a n w))) | |
459 (cond ((Math-natnum-lessp a two-to-sizem1) | |
460 sh) | |
461 ((Math-lessp n (- 1 w)) | |
462 (math-add (math-mul two-to-sizem1 2) -1)) | |
463 (t (let ((two-to-n (math-power-of-2 (- n)))) | |
464 (math-add (calcFunc-lsh (math-add two-to-n -1) | |
465 (+ w n) w) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
466 sh))))))))) |
40785 | 467 |
468 (defun calcFunc-rash (a &optional n w) ; [I I] [Public] | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
469 (calcFunc-ash a (math-neg (or n 1)) w)) |
40785 | 470 |
471 (defun calcFunc-rot (a &optional n w) ; [I I] [Public] | |
472 (setq a (math-trunc a) | |
473 n (if n (math-trunc n) 1)) | |
474 (if (eq (car-safe a) 'mod) | |
475 (math-binary-modulo-args 'calcFunc-rot a n w) | |
476 (setq w (if w (math-trunc w) calc-word-size)) | |
477 (or (integerp w) | |
478 (math-reject-arg w 'fixnump)) | |
479 (or (Math-integerp a) | |
480 (math-reject-arg a 'integerp)) | |
481 (or (Math-integerp n) | |
482 (math-reject-arg n 'integerp)) | |
483 (if (< w 0) | |
484 (math-clip (calcFunc-rot a n (- w)) w) | |
485 (if (Math-integer-negp a) | |
486 (setq a (math-clip a w))) | |
487 (cond ((or (Math-integer-negp n) | |
488 (not (Math-natnum-lessp n w))) | |
489 (calcFunc-rot a (math-mod n w) w)) | |
490 (t | |
491 (math-add (calcFunc-lsh a (- n w) w) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
492 (calcFunc-lsh a n w))))))) |
40785 | 493 |
494 (defun math-clip (a &optional w) ; [I I] [Public] | |
495 (cond ((Math-messy-integerp w) | |
496 (math-clip a (math-trunc w))) | |
497 ((eq (car-safe a) 'mod) | |
498 (math-binary-modulo-args 'math-clip a nil w)) | |
499 ((and w (not (integerp w))) | |
500 (math-reject-arg w 'fixnump)) | |
501 ((not (Math-num-integerp a)) | |
502 (math-reject-arg a 'integerp)) | |
503 ((< (or w (setq w calc-word-size)) 0) | |
504 (setq a (math-clip a (- w))) | |
505 (if (Math-natnum-lessp a (math-power-of-2 (- -1 w))) | |
506 a | |
507 (math-sub a (math-power-of-2 (- w))))) | |
508 ((Math-negp a) | |
509 (math-normalize (cons 'bigpos (math-binary-arg a w)))) | |
510 ((and (integerp a) (< a 1000000)) | |
511 (if (>= w 20) | |
512 a | |
513 (logand a (1- (lsh 1 w))))) | |
514 (t | |
515 (math-normalize | |
516 (cons 'bigpos | |
517 (math-clip-bignum (cdr (math-bignum-test (math-trunc a))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
518 w)))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
519 |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
520 (defalias 'calcFunc-clip 'math-clip) |
40785 | 521 |
522 (defun math-clip-bignum (a w) ; [l l] | |
523 (let ((q (math-div-bignum-digit a 512))) | |
524 (if (<= w 9) | |
525 (list (logand (cdr q) | |
526 (1- (lsh 1 w)))) | |
527 (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q)) | |
528 (- w 9)) | |
529 512 | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
530 (cdr q))))) |
40785 | 531 |
532 (defvar math-max-digits-cache nil) | |
533 (defun math-compute-max-digits (w r) | |
534 (let* ((pair (+ (* r 100000) w)) | |
535 (res (assq pair math-max-digits-cache))) | |
536 (if res | |
537 (cdr res) | |
538 (let* ((calc-command-flags nil) | |
539 (digs (math-ceiling (math-div w (math-real-log2 r))))) | |
540 (setq math-max-digits-cache (cons (cons pair digs) | |
541 math-max-digits-cache)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
542 digs)))) |
40785 | 543 |
544 (defvar math-log2-cache (list '(2 . 1) | |
545 '(4 . 2) | |
546 '(8 . 3) | |
547 '(10 . (float 332193 -5)) | |
548 '(16 . 4) | |
549 '(32 . 5))) | |
550 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6 | |
551 (let ((res (assq x math-log2-cache))) | |
552 (if res | |
553 (cdr res) | |
554 (let* ((calc-symbolic-mode nil) | |
555 (calc-display-working-message nil) | |
556 (log (calcFunc-log x 2))) | |
557 (setq math-log2-cache (cons (cons x log) math-log2-cache)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
558 log)))) |
40785 | 559 |
560 (defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" | |
561 "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" | |
562 "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" | |
563 "U" "V" "W" "X" "Y" "Z"]) | |
564 | |
565 (defun math-format-radix (a) ; [X S] | |
566 (if (< a calc-number-radix) | |
567 (if (< a 0) | |
568 (concat "-" (math-format-radix (- a))) | |
569 (math-format-radix-digit a)) | |
570 (let ((s "")) | |
571 (while (> a 0) | |
572 (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s) | |
573 a (/ a calc-number-radix))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
574 s))) |
40785 | 575 |
576 (defconst math-binary-digits ["000" "001" "010" "011" | |
577 "100" "101" "110" "111"]) | |
578 (defun math-format-binary (a) ; [X S] | |
579 (if (< a 8) | |
580 (if (< a 0) | |
581 (concat "-" (math-format-binary (- a))) | |
582 (math-format-radix a)) | |
583 (let ((s "")) | |
584 (while (> a 7) | |
585 (setq s (concat (aref math-binary-digits (% a 8)) s) | |
586 a (/ a 8))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
587 (concat (math-format-radix a) s)))) |
40785 | 588 |
589 (defun math-format-bignum-radix (a) ; [X L] | |
590 (cond ((null a) "0") | |
591 ((and (null (cdr a)) | |
592 (< (car a) calc-number-radix)) | |
593 (math-format-radix-digit (car a))) | |
594 (t | |
595 (let ((q (math-div-bignum-digit a calc-number-radix))) | |
596 (concat (math-format-bignum-radix (math-norm-bignum (car q))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
597 (math-format-radix-digit (cdr q))))))) |
40785 | 598 |
599 (defun math-format-bignum-binary (a) ; [X L] | |
600 (cond ((null a) "0") | |
601 ((null (cdr a)) | |
602 (math-format-binary (car a))) | |
603 (t | |
604 (let ((q (math-div-bignum-digit a 512))) | |
605 (concat (math-format-bignum-binary (math-norm-bignum (car q))) | |
606 (aref math-binary-digits (/ (cdr q) 64)) | |
607 (aref math-binary-digits (% (/ (cdr q) 8) 8)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
608 (aref math-binary-digits (% (cdr q) 8))))))) |
40785 | 609 |
610 (defun math-format-bignum-octal (a) ; [X L] | |
611 (cond ((null a) "0") | |
612 ((null (cdr a)) | |
613 (math-format-radix (car a))) | |
614 (t | |
615 (let ((q (math-div-bignum-digit a 512))) | |
616 (concat (math-format-bignum-octal (math-norm-bignum (car q))) | |
617 (math-format-radix-digit (/ (cdr q) 64)) | |
618 (math-format-radix-digit (% (/ (cdr q) 8) 8)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
619 (math-format-radix-digit (% (cdr q) 8))))))) |
40785 | 620 |
621 (defun math-format-bignum-hex (a) ; [X L] | |
622 (cond ((null a) "0") | |
623 ((null (cdr a)) | |
624 (math-format-radix (car a))) | |
625 (t | |
626 (let ((q (math-div-bignum-digit a 256))) | |
627 (concat (math-format-bignum-hex (math-norm-bignum (car q))) | |
628 (math-format-radix-digit (/ (cdr q) 16)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
629 (math-format-radix-digit (% (cdr q) 16))))))) |
40785 | 630 |
631 ;;; Decompose into integer and fractional parts, without depending | |
632 ;;; on calc-internal-prec. | |
633 (defun math-float-parts (a need-frac) ; returns ( int frac fracdigs ) | |
634 (if (>= (nth 2 a) 0) | |
635 (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0) | |
636 (let* ((d (math-numdigs (nth 1 a))) | |
637 (n (- (nth 2 a)))) | |
638 (if need-frac | |
639 (if (>= n d) | |
640 (list 0 a n) | |
641 (let ((qr (math-idivmod (nth 1 a) (math-scale-int 1 n)))) | |
642 (list (car qr) (math-make-float (cdr qr) (- n)) n))) | |
643 (list (math-scale-rounding (nth 1 a) (nth 2 a)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
644 '(float 0 0) 0))))) |
40785 | 645 |
646 (defun math-format-radix-float (a prec) | |
647 (let ((fmt (car calc-float-format)) | |
648 (figs (nth 1 calc-float-format)) | |
649 (point calc-point-char) | |
650 (str nil)) | |
651 (if (eq fmt 'fix) | |
652 (let* ((afigs (math-abs figs)) | |
653 (fp (math-float-parts a (> afigs 0))) | |
654 (calc-internal-prec (+ 3 (max (nth 2 fp) | |
655 (math-convert-radix-digits | |
656 afigs t)))) | |
657 (int (car fp)) | |
658 (frac (math-round (math-mul (math-normalize (nth 1 fp)) | |
659 (math-radix-float-power afigs))))) | |
660 (if (not (and (math-zerop frac) (math-zerop int) (< figs 0))) | |
661 (let ((math-radix-explicit-format nil)) | |
662 (let ((calc-group-digits nil)) | |
663 (setq str (if (> afigs 0) (math-format-number frac) "")) | |
664 (if (< (length str) afigs) | |
665 (setq str (concat (make-string (- afigs (length str)) ?0) | |
666 str)) | |
667 (if (> (length str) afigs) | |
668 (setq str (substring str 1) | |
669 int (math-add int 1)))) | |
670 (setq str (concat (math-format-number int) point str))) | |
671 (if calc-group-digits | |
672 (setq str (math-group-float str)))) | |
673 (setq figs 0)))) | |
674 (or str | |
675 (let* ((prec calc-internal-prec) | |
676 (afigs (if (> figs 0) | |
677 figs | |
678 (max 1 (+ figs | |
679 (1- (math-convert-radix-digits | |
680 (max prec | |
681 (math-numdigs (nth 1 a))))))))) | |
682 (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t))) | |
683 (explo -1) (vlo (math-radix-float-power explo)) | |
684 (exphi 1) (vhi (math-radix-float-power exphi)) | |
685 expmid vmid eadj) | |
686 (setq a (math-normalize a)) | |
687 (if (Math-zerop a) | |
688 (setq explo 0) | |
689 (if (math-lessp-float '(float 1 0) a) | |
690 (while (not (math-lessp-float a vhi)) | |
691 (setq explo exphi vlo vhi | |
692 exphi (math-mul exphi 2) | |
693 vhi (math-radix-float-power exphi))) | |
694 (while (math-lessp-float a vlo) | |
695 (setq exphi explo vhi vlo | |
696 explo (math-mul explo 2) | |
697 vlo (math-radix-float-power explo)))) | |
698 (while (not (eq (math-sub exphi explo) 1)) | |
699 (setq expmid (math-div2 (math-add explo exphi)) | |
700 vmid (math-radix-float-power expmid)) | |
701 (if (math-lessp-float a vmid) | |
702 (setq exphi expmid vhi vmid) | |
703 (setq explo expmid vlo vmid))) | |
704 (setq a (math-div-float a vlo))) | |
705 (let* ((sc (math-round (math-mul a (math-radix-float-power | |
706 (1- afigs))))) | |
707 (math-radix-explicit-format nil)) | |
708 (let ((calc-group-digits nil)) | |
709 (setq str (math-format-number sc)))) | |
710 (if (> (length str) afigs) | |
711 (setq str (substring str 0 -1) | |
712 explo (1+ explo))) | |
713 (if (and (eq fmt 'float) | |
714 (math-lessp explo (+ (if (= figs 0) | |
715 (1- (math-convert-radix-digits | |
716 prec)) | |
717 afigs) | |
718 calc-display-sci-high 1)) | |
719 (math-lessp calc-display-sci-low explo)) | |
720 (let ((dpos (1+ explo))) | |
721 (cond ((<= dpos 0) | |
722 (setq str (concat "0" point (make-string (- dpos) ?0) | |
723 str))) | |
724 ((> dpos (length str)) | |
725 (setq str (concat str (make-string (- dpos (length str)) | |
726 ?0) point))) | |
727 (t | |
728 (setq str (concat (substring str 0 dpos) point | |
729 (substring str dpos))))) | |
730 (setq explo nil)) | |
731 (setq eadj (if (eq fmt 'eng) | |
732 (min (math-mod explo 3) (length str)) | |
733 0) | |
734 str (concat (substring str 0 (1+ eadj)) point | |
735 (substring str (1+ eadj))))) | |
736 (setq pos (length str)) | |
737 (while (eq (aref str (1- pos)) ?0) (setq pos (1- pos))) | |
738 (and explo (eq (aref str (1- pos)) ?.) (setq pos (1- pos))) | |
739 (setq str (substring str 0 pos)) | |
740 (if calc-group-digits | |
741 (setq str (math-group-float str))) | |
742 (if explo | |
743 (let ((estr (let ((calc-number-radix 10) | |
744 (calc-group-digits nil)) | |
745 (setq estr (math-format-number | |
746 (math-sub explo eadj)))))) | |
747 (setq str (if (or (memq calc-language '(math maple)) | |
748 (> calc-number-radix 14)) | |
749 (format "%s*%d.^%s" str calc-number-radix estr) | |
750 (format "%se%s" str estr))))))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
751 str)) |
40785 | 752 |
753 (defun math-convert-radix-digits (n &optional to-dec) | |
754 (let ((key (cons n (cons to-dec calc-number-radix)))) | |
755 (or (cdr (assoc key math-radix-digits-cache)) | |
756 (let* ((calc-internal-prec 6) | |
757 (log (math-div (math-real-log2 calc-number-radix) | |
758 '(float 332193 -5)))) | |
759 (cdr (car (setq math-radix-digits-cache | |
760 (cons (cons key (math-ceiling (if to-dec | |
761 (math-mul n log) | |
762 (math-div n log)))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
763 math-radix-digits-cache)))))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
764 |
40785 | 765 (setq math-radix-digits-cache nil) |
766 | |
767 (defun math-radix-float-power (n) | |
768 (if (eq n 0) | |
769 '(float 1 0) | |
770 (or (and (eq calc-number-radix (car math-radix-float-cache-tag)) | |
771 (<= calc-internal-prec (cdr math-radix-float-cache-tag))) | |
772 (setq math-radix-float-cache-tag (cons calc-number-radix | |
773 calc-internal-prec) | |
774 math-radix-float-cache nil)) | |
775 (math-normalize | |
776 (or (cdr (assoc n math-radix-float-cache)) | |
777 (cdr (car (setq math-radix-float-cache | |
778 (cons (cons | |
779 n | |
780 (let ((calc-internal-prec | |
781 (cdr math-radix-float-cache-tag))) | |
782 (if (math-negp n) | |
783 (math-div-float '(float 1 0) | |
784 (math-radix-float-power | |
785 (math-neg n))) | |
786 (math-mul-float (math-sqr-float | |
787 (math-radix-float-power | |
788 (math-div2 n))) | |
789 (if (math-evenp n) | |
790 '(float 1 0) | |
791 (math-float | |
792 calc-number-radix)))))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
793 math-radix-float-cache)))))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
794 |
40785 | 795 (setq math-radix-float-cache-tag nil) |
796 | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
797 ;;; calc-bin.el ends here |