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