1 ;;; calc-bin.el --- binary functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 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/>.
27 ;; This file is autoloaded from calc-ext.el.
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.
48 (calc-enter-result 2 "and"
49 (append '(calcFunc-and)
51 (and n (list (prefix-numeric-value n)))))))
56 (calc-enter-result 2 "or"
57 (append '(calcFunc-or)
59 (and n (list (prefix-numeric-value n)))))))
64 (calc-enter-result 2 "xor"
65 (append '(calcFunc-xor)
67 (and n (list (prefix-numeric-value n)))))))
72 (calc-enter-result 2 "diff"
73 (append '(calcFunc-diff)
75 (and n (list (prefix-numeric-value n)))))))
80 (calc-enter-result 1 "not"
81 (append '(calcFunc-not)
83 (and n (list (prefix-numeric-value n)))))))
85 (defun calc-lshift-binary (n)
88 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
89 (calc-enter-result hyp "lsh"
90 (append '(calcFunc-lsh)
92 (and n (list (prefix-numeric-value n))))))))
94 (defun calc-rshift-binary (n)
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)
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)
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)
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))))))))
133 (calc-enter-result 1 "clip"
134 (append '(calcFunc-clip)
136 (and n (list (prefix-numeric-value n)))))))
138 (defun calc-word-size (n)
141 (or n (setq n (read-string (format "Binary word size: (default %d) "
143 (setq n (if (stringp n)
146 (if (string-match "\\`[-+]?[0-9]+\\'" 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))))
159 (calc-refresh-evaltos)
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): ")
173 (if (and (>= n 2) (<= n 36))
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 ()
189 (defun calc-binary-radix (&optional arg)
193 (defun calc-octal-radix (&optional arg)
197 (defun calc-hex-radix (&optional arg)
201 (defun calc-leading-zeros (n)
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)
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)))
221 (setq val (math-mul val 2)
222 math-power-of-2-cache (nconc math-power-of-2-cache
226 (let ((found (assq n math-big-power-of-2-cache)))
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))
234 (defun math-integer-log2 (n) ; [I I] [Public]
236 (p math-power-of-2-cache)
238 (while (and p (Math-natnum-lessp (setq val (car p)) n))
244 (while (Math-natnum-lessp
246 (setq val (math-mul val 2))
247 (setq math-power-of-2-cache (nconc math-power-of-2-cache
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)))
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)
287 (if (eq (car-safe a) 'mod)
291 (if (eq (car-safe b) 'mod)
292 (if (equal mod (nth 2 b))
294 (math-reject-arg b "*Inconsistent modulus"))))
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)))
306 "*Warning: Modulus inconsistent with word size"))
308 (calc-record-why "*Warning: Modulus is not a power of 2"))
314 (defun math-and-bignum (a b) ; [l l l]
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)))
341 (defun math-or-bignum (a b) ; [l l l]
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)))
368 (defun math-xor-bignum (a b) ; [l l l]
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)))
395 (defun math-diff-bignum (a b) ; [l l l]
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))
417 (math-not-bignum (math-binary-arg a 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))
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
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))
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))
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))
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]
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))
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))
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)
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))
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))
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)))))
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)))))
537 (math-clip-bignum (cdr (math-bignum-test (math-trunc a)))
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)
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
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)))
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))
564 (defvar math-log2-cache (list '(2 . 1)
567 '(10 . (float 332193 -5))
570 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
571 (let ((res (assq x math-log2-cache)))
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))
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)
591 (concat "-" (math-format-radix (- a)))
592 (math-format-radix-digit a))
595 (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s)
596 a (/ a calc-number-radix)))
599 (defconst math-binary-digits ["000" "001" "010" "011"
600 "100" "101" "110" "111"])
601 (defun math-format-binary (a) ; [X S]
604 (concat "-" (math-format-binary (- a)))
605 (math-format-radix a))
608 (setq s (concat (aref math-binary-digits (% a 8)) s)
610 (concat (math-format-radix a) s))))
612 (defun math-format-bignum-radix (a) ; [X L]
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]
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]
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]
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 )
658 (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0)
659 (let* ((d (math-numdigs (nth 1 a)))
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))
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)
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
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)
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))))
699 (let* ((prec calc-internal-prec)
700 (afigs (if (> figs 0)
703 (1- (math-convert-radix-digits
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))
710 (setq a (math-normalize a))
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
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)
737 (if (and (eq fmt 'float)
738 (math-lessp explo (+ (if (= figs 0)
739 (1- (math-convert-radix-digits
742 calc-display-sci-high 1))
743 (math-lessp calc-display-sci-low explo))
744 (let ((dpos (1+ explo)))
746 (setq str (concat "0" point (make-string (- dpos) ?0)
748 ((> dpos (length str))
749 (setq str (concat str (make-string (- dpos (length str))
752 (setq str (concat (substring str 0 dpos) point
753 (substring str dpos)))))
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)))
767 (let ((estr (let ((calc-number-radix 10)
768 (calc-group-digits nil))
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)))))))
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
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)
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
801 math-radix-float-cache nil))
803 (or (cdr (assoc n math-radix-float-cache))
804 (cdr (car (setq math-radix-float-cache
807 (let ((calc-internal-prec
808 (cdr math-radix-float-cache-tag)))
810 (math-div-float '(float 1 0)
811 (math-radix-float-power
813 (math-mul-float (math-sqr-float
814 (math-radix-float-power
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)
832 (and (Math-integer-posp 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)))
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)
846 (setq num (concat (make-string (- digs len) ?0) num))))
847 (when calc-group-digits
848 (setq num (math-group-float num)))
850 (number-to-string calc-number-radix)
856 ;;; calc-bin.el ends here