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