Define with-new-thread macro.
[emacs.git] / lisp / calc / calc-bin.el
blob7af60e921407df91f50ec17df0f26507f7999cbf
1 ;;; calc-bin.el --- binary functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 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 (setq math-2-word-size (math-power-of-2 (math-abs n)))
158 (setq math-half-2-word-size (math-power-of-2 (1- (math-abs n))))
159 (calc-do-refresh)
160 (calc-refresh-evaltos)
161 (if (< n 0)
162 (message "Binary word size is %d bits (two's complement)" (- n))
163 (message "Binary word size is %d bits" n))))
169 ;;; d-prefix mode commands.
171 (defun calc-radix (n &optional arg)
172 (interactive "NDisplay radix (2-36): ")
173 (calc-wrapper
174 (if (and (>= n 2) (<= n 36))
175 (progn
176 (calc-change-mode
177 (list 'calc-number-radix 'calc-twos-complement-mode)
178 (list n (and (or (= n 2) (= n 8) (= n 16)) arg)) t)
179 ;; also change global value so minibuffer sees it
180 (setq-default calc-number-radix calc-number-radix))
181 (setq n calc-number-radix))
182 (if calc-twos-complement-mode
183 (message "Number radix is %d, two's complement mode is on." n)
184 (message "Number radix is %d" n))))
186 (defun calc-decimal-radix ()
187 (interactive)
188 (calc-radix 10))
190 (defun calc-binary-radix (&optional arg)
191 (interactive "P")
192 (calc-radix 2 arg))
194 (defun calc-octal-radix (&optional arg)
195 (interactive "P")
196 (calc-radix 8 arg))
198 (defun calc-hex-radix (&optional arg)
199 (interactive "P")
200 (calc-radix 16 arg))
202 (defun calc-leading-zeros (n)
203 (interactive "P")
204 (calc-wrapper
205 (if (calc-change-mode 'calc-leading-zeros n t t)
206 (message "Zero-padding integers to %d digits (assuming radix %d)"
207 (let* ((calc-internal-prec 6))
208 (math-compute-max-digits (math-abs calc-word-size)
209 calc-number-radix))
210 calc-number-radix)
211 (message "Omitting leading zeros on integers"))))
214 (defvar math-power-of-2-cache (list 1 2 4 8 16 32 64 128 256 512 1024))
215 (defvar math-big-power-of-2-cache nil)
216 (defun math-power-of-2 (n) ; [I I] [Public]
217 (if (and (natnump n) (<= n 100))
218 (or (nth n math-power-of-2-cache)
219 (let* ((i (length math-power-of-2-cache))
220 (val (nth (1- i) math-power-of-2-cache)))
221 (while (<= i n)
222 (setq val (math-mul val 2)
223 math-power-of-2-cache (nconc math-power-of-2-cache
224 (list val))
225 i (1+ i)))
226 val))
227 (let ((found (assq n math-big-power-of-2-cache)))
228 (if found
229 (cdr found)
230 (let ((po2 (math-ipow 2 n)))
231 (setq math-big-power-of-2-cache
232 (cons (cons n po2) math-big-power-of-2-cache))
233 po2)))))
235 (defun math-integer-log2 (n) ; [I I] [Public]
236 (let ((i 0)
237 (p math-power-of-2-cache)
238 val)
239 (while (and p (Math-natnum-lessp (setq val (car p)) n))
240 (setq p (cdr p)
241 i (1+ i)))
242 (if p
243 (and (equal val n)
245 (while (Math-natnum-lessp
246 (prog1
247 (setq val (math-mul val 2))
248 (setq math-power-of-2-cache (nconc math-power-of-2-cache
249 (list val))))
251 (setq i (1+ i)))
252 (and (equal val n)
253 i))))
258 ;;; Bitwise operations.
260 (defun calcFunc-and (a b &optional w) ; [I I I] [Public]
261 (cond ((Math-messy-integerp w)
262 (calcFunc-and a b (math-trunc w)))
263 ((and w (not (integerp w)))
264 (math-reject-arg w 'fixnump))
265 ((and (integerp a) (integerp b))
266 (math-clip (logand a b) w))
267 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
268 (math-binary-modulo-args 'calcFunc-and a b w))
269 ((not (Math-num-integerp a))
270 (math-reject-arg a 'integerp))
271 ((not (Math-num-integerp b))
272 (math-reject-arg b 'integerp))
273 (t (math-clip (cons 'bigpos
274 (math-and-bignum (math-binary-arg a w)
275 (math-binary-arg b w)))
276 w))))
278 (defun math-binary-arg (a w)
279 (if (not (Math-integerp a))
280 (setq a (math-trunc a)))
281 (if (Math-integer-negp a)
282 (math-not-bignum (cdr (math-bignum-test (math-sub -1 a)))
283 (math-abs (if w (math-trunc w) calc-word-size)))
284 (cdr (Math-bignum-test a))))
286 (defun math-binary-modulo-args (f a b w)
287 (let (mod)
288 (if (eq (car-safe a) 'mod)
289 (progn
290 (setq mod (nth 2 a)
291 a (nth 1 a))
292 (if (eq (car-safe b) 'mod)
293 (if (equal mod (nth 2 b))
294 (setq b (nth 1 b))
295 (math-reject-arg b "*Inconsistent modulos"))))
296 (setq mod (nth 2 b)
297 b (nth 1 b)))
298 (if (Math-messy-integerp mod)
299 (setq mod (math-trunc mod))
300 (or (Math-integerp mod)
301 (math-reject-arg mod 'integerp)))
302 (let ((bits (math-integer-log2 mod)))
303 (if bits
304 (if w
305 (if (/= w bits)
306 (calc-record-why
307 "*Warning: Modulo inconsistent with word size"))
308 (setq w bits))
309 (calc-record-why "*Warning: Modulo is not a power of 2"))
310 (math-make-mod (if b
311 (funcall f a b w)
312 (funcall f a w))
313 mod))))
315 (defun math-and-bignum (a b) ; [l l l]
316 (and a b
317 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
318 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
319 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa))
320 (math-norm-bignum (car qb)))
321 math-bignum-digit-power-of-two
322 (logand (cdr qa) (cdr qb))))))
324 (defun calcFunc-or (a b &optional w) ; [I I I] [Public]
325 (cond ((Math-messy-integerp w)
326 (calcFunc-or a b (math-trunc w)))
327 ((and w (not (integerp w)))
328 (math-reject-arg w 'fixnump))
329 ((and (integerp a) (integerp b))
330 (math-clip (logior a b) w))
331 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
332 (math-binary-modulo-args 'calcFunc-or a b w))
333 ((not (Math-num-integerp a))
334 (math-reject-arg a 'integerp))
335 ((not (Math-num-integerp b))
336 (math-reject-arg b 'integerp))
337 (t (math-clip (cons 'bigpos
338 (math-or-bignum (math-binary-arg a w)
339 (math-binary-arg b w)))
340 w))))
342 (defun math-or-bignum (a b) ; [l l l]
343 (and (or a b)
344 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
345 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
346 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa))
347 (math-norm-bignum (car qb)))
348 math-bignum-digit-power-of-two
349 (logior (cdr qa) (cdr qb))))))
351 (defun calcFunc-xor (a b &optional w) ; [I I I] [Public]
352 (cond ((Math-messy-integerp w)
353 (calcFunc-xor a b (math-trunc w)))
354 ((and w (not (integerp w)))
355 (math-reject-arg w 'fixnump))
356 ((and (integerp a) (integerp b))
357 (math-clip (logxor a b) w))
358 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
359 (math-binary-modulo-args 'calcFunc-xor a b w))
360 ((not (Math-num-integerp a))
361 (math-reject-arg a 'integerp))
362 ((not (Math-num-integerp b))
363 (math-reject-arg b 'integerp))
364 (t (math-clip (cons 'bigpos
365 (math-xor-bignum (math-binary-arg a w)
366 (math-binary-arg b w)))
367 w))))
369 (defun math-xor-bignum (a b) ; [l l l]
370 (and (or a b)
371 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
372 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
373 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa))
374 (math-norm-bignum (car qb)))
375 math-bignum-digit-power-of-two
376 (logxor (cdr qa) (cdr qb))))))
378 (defun calcFunc-diff (a b &optional w) ; [I I I] [Public]
379 (cond ((Math-messy-integerp w)
380 (calcFunc-diff a b (math-trunc w)))
381 ((and w (not (integerp w)))
382 (math-reject-arg w 'fixnump))
383 ((and (integerp a) (integerp b))
384 (math-clip (logand a (lognot b)) w))
385 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
386 (math-binary-modulo-args 'calcFunc-diff a b w))
387 ((not (Math-num-integerp a))
388 (math-reject-arg a 'integerp))
389 ((not (Math-num-integerp b))
390 (math-reject-arg b 'integerp))
391 (t (math-clip (cons 'bigpos
392 (math-diff-bignum (math-binary-arg a w)
393 (math-binary-arg b w)))
394 w))))
396 (defun math-diff-bignum (a b) ; [l l l]
397 (and a
398 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
399 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
400 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa))
401 (math-norm-bignum (car qb)))
402 math-bignum-digit-power-of-two
403 (logand (cdr qa) (lognot (cdr qb)))))))
405 (defun calcFunc-not (a &optional w) ; [I I] [Public]
406 (cond ((Math-messy-integerp w)
407 (calcFunc-not a (math-trunc w)))
408 ((eq (car-safe a) 'mod)
409 (math-binary-modulo-args 'calcFunc-not a nil w))
410 ((and w (not (integerp w)))
411 (math-reject-arg w 'fixnump))
412 ((not (Math-num-integerp a))
413 (math-reject-arg a 'integerp))
414 ((< (or w (setq w calc-word-size)) 0)
415 (math-clip (calcFunc-not a (- w)) w))
416 (t (math-normalize
417 (cons 'bigpos
418 (math-not-bignum (math-binary-arg a w)
419 w))))))
421 (defun math-not-bignum (a w) ; [l l]
422 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
423 (if (<= w math-bignum-logb-digit-size)
424 (list (logand (lognot (cdr q))
425 (1- (lsh 1 w))))
426 (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q))
427 (- w math-bignum-logb-digit-size))
428 math-bignum-digit-power-of-two
429 (logxor (cdr q)
430 (1- math-bignum-digit-power-of-two))))))
432 (defun calcFunc-lsh (a &optional n w) ; [I I] [Public]
433 (setq a (math-trunc a)
434 n (if n (math-trunc n) 1))
435 (if (eq (car-safe a) 'mod)
436 (math-binary-modulo-args 'calcFunc-lsh a n w)
437 (setq w (if w (math-trunc w) calc-word-size))
438 (or (integerp w)
439 (math-reject-arg w 'fixnump))
440 (or (Math-integerp a)
441 (math-reject-arg a 'integerp))
442 (or (Math-integerp n)
443 (math-reject-arg n 'integerp))
444 (if (< w 0)
445 (math-clip (calcFunc-lsh a n (- w)) w)
446 (if (Math-integer-negp a)
447 (setq a (math-clip a w)))
448 (cond ((or (Math-lessp n (- w))
449 (Math-lessp w n))
451 ((< n 0)
452 (math-quotient (math-clip a w) (math-power-of-2 (- n))))
454 (math-clip (math-mul a (math-power-of-2 n)) w))))))
456 (defun calcFunc-rsh (a &optional n w) ; [I I] [Public]
457 (calcFunc-lsh a (math-neg (or n 1)) w))
459 (defun calcFunc-ash (a &optional n w) ; [I I] [Public]
460 (if (or (null n)
461 (not (Math-negp n)))
462 (calcFunc-lsh a n w)
463 (setq a (math-trunc a)
464 n (if n (math-trunc n) 1))
465 (if (eq (car-safe a) 'mod)
466 (math-binary-modulo-args 'calcFunc-ash a n w)
467 (setq w (if w (math-trunc w) calc-word-size))
468 (or (integerp w)
469 (math-reject-arg w 'fixnump))
470 (or (Math-integerp a)
471 (math-reject-arg a 'integerp))
472 (or (Math-integerp n)
473 (math-reject-arg n 'integerp))
474 (if (< w 0)
475 (math-clip (calcFunc-ash a n (- w)) w)
476 (if (Math-integer-negp a)
477 (setq a (math-clip a w)))
478 (let ((two-to-sizem1 (math-power-of-2 (1- w)))
479 (sh (calcFunc-lsh a n w)))
480 (cond ((Math-natnum-lessp a two-to-sizem1)
482 ((Math-lessp n (- 1 w))
483 (math-add (math-mul two-to-sizem1 2) -1))
484 (t (let ((two-to-n (math-power-of-2 (- n))))
485 (math-add (calcFunc-lsh (math-add two-to-n -1)
486 (+ w n) w)
487 sh)))))))))
489 (defun calcFunc-rash (a &optional n w) ; [I I] [Public]
490 (calcFunc-ash a (math-neg (or n 1)) w))
492 (defun calcFunc-rot (a &optional n w) ; [I I] [Public]
493 (setq a (math-trunc a)
494 n (if n (math-trunc n) 1))
495 (if (eq (car-safe a) 'mod)
496 (math-binary-modulo-args 'calcFunc-rot a n w)
497 (setq w (if w (math-trunc w) calc-word-size))
498 (or (integerp w)
499 (math-reject-arg w 'fixnump))
500 (or (Math-integerp a)
501 (math-reject-arg a 'integerp))
502 (or (Math-integerp n)
503 (math-reject-arg n 'integerp))
504 (if (< w 0)
505 (math-clip (calcFunc-rot a n (- w)) w)
506 (if (Math-integer-negp a)
507 (setq a (math-clip a w)))
508 (cond ((or (Math-integer-negp n)
509 (not (Math-natnum-lessp n w)))
510 (calcFunc-rot a (math-mod n w) w))
512 (math-add (calcFunc-lsh a (- n w) w)
513 (calcFunc-lsh a n w)))))))
515 (defun math-clip (a &optional w) ; [I I] [Public]
516 (cond ((Math-messy-integerp w)
517 (math-clip a (math-trunc w)))
518 ((eq (car-safe a) 'mod)
519 (math-binary-modulo-args 'math-clip a nil w))
520 ((and w (not (integerp w)))
521 (math-reject-arg w 'fixnump))
522 ((not (Math-num-integerp a))
523 (math-reject-arg a 'integerp))
524 ((< (or w (setq w calc-word-size)) 0)
525 (setq a (math-clip a (- w)))
526 (if (Math-natnum-lessp a (math-power-of-2 (- -1 w)))
528 (math-sub a (math-power-of-2 (- w)))))
529 ((Math-negp a)
530 (math-normalize (cons 'bigpos (math-binary-arg a w))))
531 ((and (integerp a) (< a math-small-integer-size))
532 (if (> w (logb math-small-integer-size))
534 (logand a (1- (lsh 1 w)))))
536 (math-normalize
537 (cons 'bigpos
538 (math-clip-bignum (cdr (math-bignum-test (math-trunc a)))
539 w))))))
541 (defalias 'calcFunc-clip 'math-clip)
543 (defun math-clip-bignum (a w) ; [l l]
544 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
545 (if (<= w math-bignum-logb-digit-size)
546 (list (logand (cdr q)
547 (1- (lsh 1 w))))
548 (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q))
549 (- w math-bignum-logb-digit-size))
550 math-bignum-digit-power-of-two
551 (cdr q)))))
553 (defvar math-max-digits-cache nil)
554 (defun math-compute-max-digits (w r)
555 (let* ((pair (+ (* r 100000) w))
556 (res (assq pair math-max-digits-cache)))
557 (if res
558 (cdr res)
559 (let* ((calc-command-flags nil)
560 (digs (math-ceiling (math-div w (math-real-log2 r)))))
561 (setq math-max-digits-cache (cons (cons pair digs)
562 math-max-digits-cache))
563 digs))))
565 (defvar math-log2-cache (list '(2 . 1)
566 '(4 . 2)
567 '(8 . 3)
568 '(10 . (float 332193 -5))
569 '(16 . 4)
570 '(32 . 5)))
571 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
572 (let ((res (assq x math-log2-cache)))
573 (if res
574 (cdr res)
575 (let* ((calc-symbolic-mode nil)
576 (calc-display-working-message nil)
577 (log (calcFunc-log x 2)))
578 (setq math-log2-cache (cons (cons x log) math-log2-cache))
579 log))))
581 (defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
582 "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
583 "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
584 "U" "V" "W" "X" "Y" "Z"])
586 (defsubst math-format-radix-digit (a) ; [X D]
587 (aref math-radix-digits a))
589 (defun math-format-radix (a) ; [X S]
590 (if (< a calc-number-radix)
591 (if (< a 0)
592 (concat "-" (math-format-radix (- a)))
593 (math-format-radix-digit a))
594 (let ((s ""))
595 (while (> a 0)
596 (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s)
597 a (/ a calc-number-radix)))
598 s)))
600 (defconst math-binary-digits ["000" "001" "010" "011"
601 "100" "101" "110" "111"])
602 (defun math-format-binary (a) ; [X S]
603 (if (< a 8)
604 (if (< a 0)
605 (concat "-" (math-format-binary (- a)))
606 (math-format-radix a))
607 (let ((s ""))
608 (while (> a 7)
609 (setq s (concat (aref math-binary-digits (% a 8)) s)
610 a (/ a 8)))
611 (concat (math-format-radix a) s))))
613 (defun math-format-bignum-radix (a) ; [X L]
614 (cond ((null a) "0")
615 ((and (null (cdr a))
616 (< (car a) calc-number-radix))
617 (math-format-radix-digit (car a)))
619 (let ((q (math-div-bignum-digit a calc-number-radix)))
620 (concat (math-format-bignum-radix (math-norm-bignum (car q)))
621 (math-format-radix-digit (cdr q)))))))
623 (defun math-format-bignum-binary (a) ; [X L]
624 (cond ((null a) "0")
625 ((null (cdr a))
626 (math-format-binary (car a)))
628 (let ((q (math-div-bignum-digit a 512)))
629 (concat (math-format-bignum-binary (math-norm-bignum (car q)))
630 (aref math-binary-digits (/ (cdr q) 64))
631 (aref math-binary-digits (% (/ (cdr q) 8) 8))
632 (aref math-binary-digits (% (cdr q) 8)))))))
634 (defun math-format-bignum-octal (a) ; [X L]
635 (cond ((null a) "0")
636 ((null (cdr a))
637 (math-format-radix (car a)))
639 (let ((q (math-div-bignum-digit a 512)))
640 (concat (math-format-bignum-octal (math-norm-bignum (car q)))
641 (math-format-radix-digit (/ (cdr q) 64))
642 (math-format-radix-digit (% (/ (cdr q) 8) 8))
643 (math-format-radix-digit (% (cdr q) 8)))))))
645 (defun math-format-bignum-hex (a) ; [X L]
646 (cond ((null a) "0")
647 ((null (cdr a))
648 (math-format-radix (car a)))
650 (let ((q (math-div-bignum-digit a 256)))
651 (concat (math-format-bignum-hex (math-norm-bignum (car q)))
652 (math-format-radix-digit (/ (cdr q) 16))
653 (math-format-radix-digit (% (cdr q) 16)))))))
655 ;;; Decompose into integer and fractional parts, without depending
656 ;;; on calc-internal-prec.
657 (defun math-float-parts (a need-frac) ; returns ( int frac fracdigs )
658 (if (>= (nth 2 a) 0)
659 (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0)
660 (let* ((d (math-numdigs (nth 1 a)))
661 (n (- (nth 2 a))))
662 (if need-frac
663 (if (>= n d)
664 (list 0 a n)
665 (let ((qr (math-idivmod (nth 1 a) (math-scale-int 1 n))))
666 (list (car qr) (math-make-float (cdr qr) (- n)) n)))
667 (list (math-scale-rounding (nth 1 a) (nth 2 a))
668 '(float 0 0) 0)))))
670 (defun math-format-radix-float (a prec)
671 (let ((fmt (car calc-float-format))
672 (figs (nth 1 calc-float-format))
673 (point calc-point-char)
674 (str nil)
675 pos)
676 (if (eq fmt 'fix)
677 (let* ((afigs (math-abs figs))
678 (fp (math-float-parts a (> afigs 0)))
679 (calc-internal-prec (+ 3 (max (nth 2 fp)
680 (math-convert-radix-digits
681 afigs t))))
682 (int (car fp))
683 (frac (math-round (math-mul (math-normalize (nth 1 fp))
684 (math-radix-float-power afigs)))))
685 (if (not (and (math-zerop frac) (math-zerop int) (< figs 0)))
686 (let ((math-radix-explicit-format nil))
687 (let ((calc-group-digits nil))
688 (setq str (if (> afigs 0) (math-format-number frac) ""))
689 (if (< (length str) afigs)
690 (setq str (concat (make-string (- afigs (length str)) ?0)
691 str))
692 (if (> (length str) afigs)
693 (setq str (substring str 1)
694 int (math-add int 1))))
695 (setq str (concat (math-format-number int) point str)))
696 (when calc-group-digits
697 (setq str (math-group-float str))))
698 (setq figs 0))))
699 (or str
700 (let* ((prec calc-internal-prec)
701 (afigs (if (> figs 0)
702 figs
703 (max 1 (+ figs
704 (1- (math-convert-radix-digits
705 (max prec
706 (math-numdigs (nth 1 a)))))))))
707 (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t)))
708 (explo -1) (vlo (math-radix-float-power explo))
709 (exphi 1) (vhi (math-radix-float-power exphi))
710 expmid vmid eadj)
711 (setq a (math-normalize a))
712 (if (Math-zerop a)
713 (setq explo 0)
714 (if (math-lessp-float '(float 1 0) a)
715 (while (not (math-lessp-float a vhi))
716 (setq explo exphi vlo vhi
717 exphi (math-mul exphi 2)
718 vhi (math-radix-float-power exphi)))
719 (while (math-lessp-float a vlo)
720 (setq exphi explo vhi vlo
721 explo (math-mul explo 2)
722 vlo (math-radix-float-power explo))))
723 (while (not (eq (math-sub exphi explo) 1))
724 (setq expmid (math-div2 (math-add explo exphi))
725 vmid (math-radix-float-power expmid))
726 (if (math-lessp-float a vmid)
727 (setq exphi expmid vhi vmid)
728 (setq explo expmid vlo vmid)))
729 (setq a (math-div-float a vlo)))
730 (let* ((sc (math-round (math-mul a (math-radix-float-power
731 (1- afigs)))))
732 (math-radix-explicit-format nil))
733 (let ((calc-group-digits nil))
734 (setq str (math-format-number sc))))
735 (if (> (length str) afigs)
736 (setq str (substring str 0 -1)
737 explo (1+ explo)))
738 (if (and (eq fmt 'float)
739 (math-lessp explo (+ (if (= figs 0)
740 (1- (math-convert-radix-digits
741 prec))
742 afigs)
743 calc-display-sci-high 1))
744 (math-lessp calc-display-sci-low explo))
745 (let ((dpos (1+ explo)))
746 (cond ((<= dpos 0)
747 (setq str (concat "0" point (make-string (- dpos) ?0)
748 str)))
749 ((> dpos (length str))
750 (setq str (concat str (make-string (- dpos (length str))
751 ?0) point)))
753 (setq str (concat (substring str 0 dpos) point
754 (substring str dpos)))))
755 (setq explo nil))
756 (setq eadj (if (eq fmt 'eng)
757 (min (math-mod explo 3) (length str))
759 str (concat (substring str 0 (1+ eadj)) point
760 (substring str (1+ eadj)))))
761 (setq pos (length str))
762 (while (eq (aref str (1- pos)) ?0) (setq pos (1- pos)))
763 (and explo (eq (aref str (1- pos)) ?.) (setq pos (1- pos)))
764 (setq str (substring str 0 pos))
765 (when calc-group-digits
766 (setq str (math-group-float str)))
767 (if explo
768 (let ((estr (let ((calc-number-radix 10)
769 (calc-group-digits nil))
770 (math-format-number
771 (math-sub explo eadj)))))
772 (setq str (if (or (memq calc-language '(math maple))
773 (> calc-number-radix 14))
774 (format "%s*%d.^%s" str calc-number-radix estr)
775 (format "%se%s" str estr)))))))
776 str))
778 (defvar math-radix-digits-cache nil)
780 (defun math-convert-radix-digits (n &optional to-dec)
781 (let ((key (cons n (cons to-dec calc-number-radix))))
782 (or (cdr (assoc key math-radix-digits-cache))
783 (let* ((calc-internal-prec 6)
784 (log (math-div (math-real-log2 calc-number-radix)
785 '(float 332193 -5))))
786 (cdr (car (setq math-radix-digits-cache
787 (cons (cons key (math-ceiling (if to-dec
788 (math-mul n log)
789 (math-div n log))))
790 math-radix-digits-cache))))))))
792 (defvar math-radix-float-cache-tag nil)
793 (defvar math-radix-float-cache)
795 (defun math-radix-float-power (n)
796 (if (eq n 0)
797 '(float 1 0)
798 (or (and (eq calc-number-radix (car math-radix-float-cache-tag))
799 (<= calc-internal-prec (cdr math-radix-float-cache-tag)))
800 (setq math-radix-float-cache-tag (cons calc-number-radix
801 calc-internal-prec)
802 math-radix-float-cache nil))
803 (math-normalize
804 (or (cdr (assoc n math-radix-float-cache))
805 (cdr (car (setq math-radix-float-cache
806 (cons (cons
808 (let ((calc-internal-prec
809 (cdr math-radix-float-cache-tag)))
810 (if (math-negp n)
811 (math-div-float '(float 1 0)
812 (math-radix-float-power
813 (math-neg n)))
814 (math-mul-float (math-sqr-float
815 (math-radix-float-power
816 (math-div2 n)))
817 (if (math-evenp n)
818 '(float 1 0)
819 (math-float
820 calc-number-radix))))))
821 math-radix-float-cache))))))))
823 ;;; Two's complement mode
825 (defun math-format-twos-complement (a)
826 "Format an integer in two's complement mode."
827 (let* (;(calc-leading-zeros t)
828 (overflow nil)
829 (negative nil)
830 (num
831 (cond
832 ((or (eq a 0)
833 (and (Math-integer-posp a)))
834 (if (integerp a)
835 (math-format-radix a)
836 (math-format-bignum-radix (cdr a))))
837 ((Math-integer-negp a)
838 (let ((newa (math-add a math-2-word-size)))
839 (if (integerp newa)
840 (math-format-radix newa)
841 (math-format-bignum-radix (cdr newa))))))))
842 (let* ((calc-internal-prec 6)
843 (digs (math-compute-max-digits (math-abs calc-word-size)
844 calc-number-radix))
845 (len (length num)))
846 (if (< len digs)
847 (setq num (concat (make-string (- digs len) ?0) num))))
848 (concat
849 (number-to-string calc-number-radix)
850 "##"
851 num)))
853 (provide 'calc-bin)
855 ;; arch-tag: f6dba7bc-53b2-41ae-919c-c266ab0ca8b3
856 ;;; calc-bin.el ends here