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