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