1 ;;; calc-bin.el --- binary functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2016 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
31 ;;; Some useful numbers
32 (defconst math-bignum-logb-digit-size
33 (logb math-bignum-digit-size)
34 "The logb of the size of a bignum digit.
35 This is the largest value of B such that 2^B is less than
36 the size of a Calc bignum digit.")
38 (defconst math-bignum-digit-power-of-two
39 (expt 2 (logb math-bignum-digit-size))
40 "The largest power of 2 less than the size of a Calc bignum digit.")
42 ;;; b-prefix binary commands.
47 (calc-enter-result 2 "and"
48 (append '(calcFunc-and)
50 (and n (list (prefix-numeric-value n)))))))
55 (calc-enter-result 2 "or"
56 (append '(calcFunc-or)
58 (and n (list (prefix-numeric-value n)))))))
63 (calc-enter-result 2 "xor"
64 (append '(calcFunc-xor)
66 (and n (list (prefix-numeric-value n)))))))
71 (calc-enter-result 2 "diff"
72 (append '(calcFunc-diff)
74 (and n (list (prefix-numeric-value n)))))))
79 (calc-enter-result 1 "not"
80 (append '(calcFunc-not)
82 (and n (list (prefix-numeric-value n)))))))
84 (defun calc-lshift-binary (n)
87 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
88 (calc-enter-result hyp "lsh"
89 (append '(calcFunc-lsh)
91 (and n (list (prefix-numeric-value n))))))))
93 (defun calc-rshift-binary (n)
96 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
97 (calc-enter-result hyp "rsh"
98 (append '(calcFunc-rsh)
100 (and n (list (prefix-numeric-value n))))))))
102 (defun calc-lshift-arith (n)
105 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
106 (calc-enter-result hyp "ash"
107 (append '(calcFunc-ash)
108 (calc-top-list-n hyp)
109 (and n (list (prefix-numeric-value n))))))))
111 (defun calc-rshift-arith (n)
114 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
115 (calc-enter-result hyp "rash"
116 (append '(calcFunc-rash)
117 (calc-top-list-n hyp)
118 (and n (list (prefix-numeric-value n))))))))
120 (defun calc-rotate-binary (n)
123 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
124 (calc-enter-result hyp "rot"
125 (append '(calcFunc-rot)
126 (calc-top-list-n hyp)
127 (and n (list (prefix-numeric-value n))))))))
132 (calc-enter-result 1 "clip"
133 (append '(calcFunc-clip)
135 (and n (list (prefix-numeric-value n)))))))
137 (defun calc-word-size (n)
140 (or n (setq n (read-string (format "Binary word size: (default %d) "
142 (setq n (if (stringp n)
145 (if (string-match "\\`[-+]?[0-9]+\\'" n)
147 (error "Expected an integer")))
148 (prefix-numeric-value n)))
149 (or (= n calc-word-size)
150 (if (> (math-abs n) 100)
151 (calc-change-mode 'calc-word-size n calc-leading-zeros)
152 (calc-change-mode '(calc-word-size calc-previous-modulo)
153 (list n (math-power-of-2 (math-abs n)))
154 calc-leading-zeros)))
155 (setq math-2-word-size (math-power-of-2 (math-abs n)))
156 (setq math-half-2-word-size (math-power-of-2 (1- (math-abs n))))
158 (calc-refresh-evaltos)
160 (message "Binary word size is %d bits (two's complement)" (- n))
161 (message "Binary word size is %d bits" n))))
167 ;;; d-prefix mode commands.
169 (defun calc-radix (n &optional arg)
170 (interactive "NDisplay radix (2-36): ")
172 (if (and (>= n 2) (<= n 36))
175 (list 'calc-number-radix 'calc-twos-complement-mode)
176 (list n (or arg (calc-is-option))) t)
177 ;; also change global value so minibuffer sees it
178 (setq-default calc-number-radix calc-number-radix))
179 (setq n calc-number-radix))
180 (if calc-twos-complement-mode
181 (message "Number radix is %d, two's complement mode is on." n)
182 (message "Number radix is %d" n))))
184 (defun calc-decimal-radix ()
188 (defun calc-binary-radix (&optional arg)
192 (defun calc-octal-radix (&optional arg)
196 (defun calc-hex-radix (&optional arg)
200 (defun calc-leading-zeros (n)
203 (if (calc-change-mode 'calc-leading-zeros n t t)
204 (message "Zero-padding integers to %d digits (assuming radix %d)"
205 (let* ((calc-internal-prec 6))
206 (math-compute-max-digits (math-abs calc-word-size)
209 (message "Omitting leading zeros on integers"))))
212 (defvar math-power-of-2-cache (list 1 2 4 8 16 32 64 128 256 512 1024))
213 (defvar math-big-power-of-2-cache nil)
214 (defun math-power-of-2 (n) ; [I I] [Public]
215 (if (and (natnump n) (<= n 100))
216 (or (nth n math-power-of-2-cache)
217 (let* ((i (length math-power-of-2-cache))
218 (val (nth (1- i) math-power-of-2-cache)))
220 (setq val (math-mul val 2)
221 math-power-of-2-cache (nconc math-power-of-2-cache
225 (let ((found (assq n math-big-power-of-2-cache)))
228 (let ((po2 (math-ipow 2 n)))
229 (setq math-big-power-of-2-cache
230 (cons (cons n po2) math-big-power-of-2-cache))
233 (defun math-integer-log2 (n) ; [I I] [Public]
235 (p math-power-of-2-cache)
237 (while (and p (Math-natnum-lessp (setq val (car p)) n))
243 (while (Math-natnum-lessp
245 (setq val (math-mul val 2))
246 (setq math-power-of-2-cache (nconc math-power-of-2-cache
256 ;;; Bitwise operations.
258 (defun calcFunc-and (a b &optional w) ; [I I I] [Public]
259 (cond ((Math-messy-integerp w)
260 (calcFunc-and a b (math-trunc w)))
261 ((and w (not (integerp w)))
262 (math-reject-arg w 'fixnump))
263 ((and (integerp a) (integerp b))
264 (math-clip (logand a b) w))
265 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
266 (math-binary-modulo-args 'calcFunc-and a b w))
267 ((not (Math-num-integerp a))
268 (math-reject-arg a 'integerp))
269 ((not (Math-num-integerp b))
270 (math-reject-arg b 'integerp))
271 (t (math-clip (cons 'bigpos
272 (math-and-bignum (math-binary-arg a w)
273 (math-binary-arg b w)))
276 (defun math-binary-arg (a w)
277 (if (not (Math-integerp a))
278 (setq a (math-trunc a)))
279 (if (Math-integer-negp a)
280 (math-not-bignum (cdr (math-bignum-test (math-sub -1 a)))
281 (math-abs (if w (math-trunc w) calc-word-size)))
282 (cdr (Math-bignum-test a))))
284 (defun math-binary-modulo-args (f a b w)
286 (if (eq (car-safe a) 'mod)
290 (if (eq (car-safe b) 'mod)
291 (if (equal mod (nth 2 b))
293 (math-reject-arg b "*Inconsistent modulus"))))
296 (if (Math-messy-integerp mod)
297 (setq mod (math-trunc mod))
298 (or (Math-integerp mod)
299 (math-reject-arg mod 'integerp)))
300 (let ((bits (math-integer-log2 mod)))
305 "*Warning: Modulus inconsistent with word size"))
307 (calc-record-why "*Warning: Modulus is not a power of 2"))
313 (defun math-and-bignum (a b) ; [l l l]
315 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
316 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
317 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa))
318 (math-norm-bignum (car qb)))
319 math-bignum-digit-power-of-two
320 (logand (cdr qa) (cdr qb))))))
322 (defun calcFunc-or (a b &optional w) ; [I I I] [Public]
323 (cond ((Math-messy-integerp w)
324 (calcFunc-or a b (math-trunc w)))
325 ((and w (not (integerp w)))
326 (math-reject-arg w 'fixnump))
327 ((and (integerp a) (integerp b))
328 (math-clip (logior a b) w))
329 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
330 (math-binary-modulo-args 'calcFunc-or a b w))
331 ((not (Math-num-integerp a))
332 (math-reject-arg a 'integerp))
333 ((not (Math-num-integerp b))
334 (math-reject-arg b 'integerp))
335 (t (math-clip (cons 'bigpos
336 (math-or-bignum (math-binary-arg a w)
337 (math-binary-arg b w)))
340 (defun math-or-bignum (a b) ; [l l l]
342 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
343 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
344 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa))
345 (math-norm-bignum (car qb)))
346 math-bignum-digit-power-of-two
347 (logior (cdr qa) (cdr qb))))))
349 (defun calcFunc-xor (a b &optional w) ; [I I I] [Public]
350 (cond ((Math-messy-integerp w)
351 (calcFunc-xor a b (math-trunc w)))
352 ((and w (not (integerp w)))
353 (math-reject-arg w 'fixnump))
354 ((and (integerp a) (integerp b))
355 (math-clip (logxor a b) w))
356 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
357 (math-binary-modulo-args 'calcFunc-xor a b w))
358 ((not (Math-num-integerp a))
359 (math-reject-arg a 'integerp))
360 ((not (Math-num-integerp b))
361 (math-reject-arg b 'integerp))
362 (t (math-clip (cons 'bigpos
363 (math-xor-bignum (math-binary-arg a w)
364 (math-binary-arg b w)))
367 (defun math-xor-bignum (a b) ; [l l l]
369 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
370 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
371 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa))
372 (math-norm-bignum (car qb)))
373 math-bignum-digit-power-of-two
374 (logxor (cdr qa) (cdr qb))))))
376 (defun calcFunc-diff (a b &optional w) ; [I I I] [Public]
377 (cond ((Math-messy-integerp w)
378 (calcFunc-diff a b (math-trunc w)))
379 ((and w (not (integerp w)))
380 (math-reject-arg w 'fixnump))
381 ((and (integerp a) (integerp b))
382 (math-clip (logand a (lognot b)) w))
383 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod))
384 (math-binary-modulo-args 'calcFunc-diff a b w))
385 ((not (Math-num-integerp a))
386 (math-reject-arg a 'integerp))
387 ((not (Math-num-integerp b))
388 (math-reject-arg b 'integerp))
389 (t (math-clip (cons 'bigpos
390 (math-diff-bignum (math-binary-arg a w)
391 (math-binary-arg b w)))
394 (defun math-diff-bignum (a b) ; [l l l]
396 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two))
397 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two)))
398 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa))
399 (math-norm-bignum (car qb)))
400 math-bignum-digit-power-of-two
401 (logand (cdr qa) (lognot (cdr qb)))))))
403 (defun calcFunc-not (a &optional w) ; [I I] [Public]
404 (cond ((Math-messy-integerp w)
405 (calcFunc-not a (math-trunc w)))
406 ((eq (car-safe a) 'mod)
407 (math-binary-modulo-args 'calcFunc-not a nil w))
408 ((and w (not (integerp w)))
409 (math-reject-arg w 'fixnump))
410 ((not (Math-num-integerp a))
411 (math-reject-arg a 'integerp))
412 ((< (or w (setq w calc-word-size)) 0)
413 (math-clip (calcFunc-not a (- w)) w))
416 (math-not-bignum (math-binary-arg a w)
419 (defun math-not-bignum (a w) ; [l l]
420 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
421 (if (<= w math-bignum-logb-digit-size)
422 (list (logand (lognot (cdr q))
424 (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q))
425 (- w math-bignum-logb-digit-size))
426 math-bignum-digit-power-of-two
428 (1- math-bignum-digit-power-of-two))))))
430 (defun calcFunc-lsh (a &optional n w) ; [I I] [Public]
431 (setq a (math-trunc a)
432 n (if n (math-trunc n) 1))
433 (if (eq (car-safe a) 'mod)
434 (math-binary-modulo-args 'calcFunc-lsh a n w)
435 (setq w (if w (math-trunc w) calc-word-size))
437 (math-reject-arg w 'fixnump))
438 (or (Math-integerp a)
439 (math-reject-arg a 'integerp))
440 (or (Math-integerp n)
441 (math-reject-arg n 'integerp))
443 (math-clip (calcFunc-lsh a n (- w)) w)
444 (if (Math-integer-negp a)
445 (setq a (math-clip a w)))
446 (cond ((or (Math-lessp n (- w))
450 (math-quotient (math-clip a w) (math-power-of-2 (- n))))
452 (math-clip (math-mul a (math-power-of-2 n)) w))))))
454 (defun calcFunc-rsh (a &optional n w) ; [I I] [Public]
455 (calcFunc-lsh a (math-neg (or n 1)) w))
457 (defun calcFunc-ash (a &optional n w) ; [I I] [Public]
461 (setq a (math-trunc a)
462 n (if n (math-trunc n) 1))
463 (if (eq (car-safe a) 'mod)
464 (math-binary-modulo-args 'calcFunc-ash a n w)
465 (setq w (if w (math-trunc w) calc-word-size))
467 (math-reject-arg w 'fixnump))
468 (or (Math-integerp a)
469 (math-reject-arg a 'integerp))
470 (or (Math-integerp n)
471 (math-reject-arg n 'integerp))
473 (math-clip (calcFunc-ash a n (- w)) w)
474 (if (Math-integer-negp a)
475 (setq a (math-clip a w)))
476 (let ((two-to-sizem1 (math-power-of-2 (1- w)))
477 (sh (calcFunc-lsh a n w)))
478 (cond ((Math-natnum-lessp a two-to-sizem1)
480 ((Math-lessp n (- 1 w))
481 (math-add (math-mul two-to-sizem1 2) -1))
482 (t (let ((two-to-n (math-power-of-2 (- n))))
483 (math-add (calcFunc-lsh (math-add two-to-n -1)
487 (defun calcFunc-rash (a &optional n w) ; [I I] [Public]
488 (calcFunc-ash a (math-neg (or n 1)) w))
490 (defun calcFunc-rot (a &optional n w) ; [I I] [Public]
491 (setq a (math-trunc a)
492 n (if n (math-trunc n) 1))
493 (if (eq (car-safe a) 'mod)
494 (math-binary-modulo-args 'calcFunc-rot a n w)
495 (setq w (if w (math-trunc w) calc-word-size))
497 (math-reject-arg w 'fixnump))
498 (or (Math-integerp a)
499 (math-reject-arg a 'integerp))
500 (or (Math-integerp n)
501 (math-reject-arg n 'integerp))
503 (math-clip (calcFunc-rot a n (- w)) w)
504 (if (Math-integer-negp a)
505 (setq a (math-clip a w)))
506 (cond ((or (Math-integer-negp n)
507 (not (Math-natnum-lessp n w)))
508 (calcFunc-rot a (math-mod n w) w))
510 (math-add (calcFunc-lsh a (- n w) w)
511 (calcFunc-lsh a n w)))))))
513 (defun math-clip (a &optional w) ; [I I] [Public]
514 (cond ((Math-messy-integerp w)
515 (math-clip a (math-trunc w)))
516 ((eq (car-safe a) 'mod)
517 (math-binary-modulo-args 'math-clip a nil w))
518 ((and w (not (integerp w)))
519 (math-reject-arg w 'fixnump))
520 ((not (Math-num-integerp a))
521 (math-reject-arg a 'integerp))
522 ((< (or w (setq w calc-word-size)) 0)
523 (setq a (math-clip a (- w)))
524 (if (Math-natnum-lessp a (math-power-of-2 (- -1 w)))
526 (math-sub a (math-power-of-2 (- w)))))
528 (math-normalize (cons 'bigpos (math-binary-arg a w))))
529 ((and (integerp a) (< a math-small-integer-size))
530 (if (> w (logb math-small-integer-size))
532 (logand a (1- (lsh 1 w)))))
536 (math-clip-bignum (cdr (math-bignum-test (math-trunc a)))
539 (defalias 'calcFunc-clip 'math-clip)
541 (defun math-clip-bignum (a w) ; [l l]
542 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two)))
543 (if (<= w math-bignum-logb-digit-size)
544 (list (logand (cdr q)
546 (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q))
547 (- w math-bignum-logb-digit-size))
548 math-bignum-digit-power-of-two
551 (defvar math-max-digits-cache nil)
552 (defun math-compute-max-digits (w r)
553 (let* ((pair (+ (* r 100000) w))
554 (res (assq pair math-max-digits-cache)))
557 (let* ((calc-command-flags nil)
558 (digs (math-ceiling (math-div w (math-real-log2 r)))))
559 (setq math-max-digits-cache (cons (cons pair digs)
560 math-max-digits-cache))
563 (defvar math-log2-cache (list '(2 . 1)
566 '(10 . (float 332193 -5))
569 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
570 (let ((res (assq x math-log2-cache)))
573 (let* ((calc-symbolic-mode nil)
574 (calc-display-working-message nil)
575 (log (calcFunc-log x 2)))
576 (setq math-log2-cache (cons (cons x log) math-log2-cache))
579 (defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
580 "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
581 "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
582 "U" "V" "W" "X" "Y" "Z"])
584 (defsubst math-format-radix-digit (a) ; [X D]
585 (aref math-radix-digits a))
587 (defun math-format-radix (a) ; [X S]
588 (if (< a calc-number-radix)
590 (concat "-" (math-format-radix (- a)))
591 (math-format-radix-digit a))
594 (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s)
595 a (/ a calc-number-radix)))
598 (defconst math-binary-digits ["000" "001" "010" "011"
599 "100" "101" "110" "111"])
600 (defun math-format-binary (a) ; [X S]
603 (concat "-" (math-format-binary (- a)))
604 (math-format-radix a))
607 (setq s (concat (aref math-binary-digits (% a 8)) s)
609 (concat (math-format-radix a) s))))
611 (defun math-format-bignum-radix (a) ; [X L]
614 (< (car a) calc-number-radix))
615 (math-format-radix-digit (car a)))
617 (let ((q (math-div-bignum-digit a calc-number-radix)))
618 (concat (math-format-bignum-radix (math-norm-bignum (car q)))
619 (math-format-radix-digit (cdr q)))))))
621 (defun math-format-bignum-binary (a) ; [X L]
624 (math-format-binary (car a)))
626 (let ((q (math-div-bignum-digit a 512)))
627 (concat (math-format-bignum-binary (math-norm-bignum (car q)))
628 (aref math-binary-digits (/ (cdr q) 64))
629 (aref math-binary-digits (% (/ (cdr q) 8) 8))
630 (aref math-binary-digits (% (cdr q) 8)))))))
632 (defun math-format-bignum-octal (a) ; [X L]
635 (math-format-radix (car a)))
637 (let ((q (math-div-bignum-digit a 512)))
638 (concat (math-format-bignum-octal (math-norm-bignum (car q)))
639 (math-format-radix-digit (/ (cdr q) 64))
640 (math-format-radix-digit (% (/ (cdr q) 8) 8))
641 (math-format-radix-digit (% (cdr q) 8)))))))
643 (defun math-format-bignum-hex (a) ; [X L]
646 (math-format-radix (car a)))
648 (let ((q (math-div-bignum-digit a 256)))
649 (concat (math-format-bignum-hex (math-norm-bignum (car q)))
650 (math-format-radix-digit (/ (cdr q) 16))
651 (math-format-radix-digit (% (cdr q) 16)))))))
653 ;;; Decompose into integer and fractional parts, without depending
654 ;;; on calc-internal-prec.
655 (defun math-float-parts (a need-frac) ; returns ( int frac fracdigs )
657 (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0)
658 (let* ((d (math-numdigs (nth 1 a)))
663 (let ((qr (math-idivmod (nth 1 a) (math-scale-int 1 n))))
664 (list (car qr) (math-make-float (cdr qr) (- n)) n)))
665 (list (math-scale-rounding (nth 1 a) (nth 2 a))
668 (defun math-format-radix-float (a prec)
669 (let ((fmt (car calc-float-format))
670 (figs (nth 1 calc-float-format))
671 (point calc-point-char)
675 (let* ((afigs (math-abs figs))
676 (fp (math-float-parts a (> afigs 0)))
677 (calc-internal-prec (+ 3 (max (nth 2 fp)
678 (math-convert-radix-digits
681 (frac (math-round (math-mul (math-normalize (nth 1 fp))
682 (math-radix-float-power afigs)))))
683 (if (not (and (math-zerop frac) (math-zerop int) (< figs 0)))
684 (let ((math-radix-explicit-format nil))
685 (let ((calc-group-digits nil))
686 (setq str (if (> afigs 0) (math-format-number frac) ""))
687 (if (< (length str) afigs)
688 (setq str (concat (make-string (- afigs (length str)) ?0)
690 (if (> (length str) afigs)
691 (setq str (substring str 1)
692 int (math-add int 1))))
693 (setq str (concat (math-format-number int) point str)))
694 (when calc-group-digits
695 (setq str (math-group-float str))))
698 (let* ((prec calc-internal-prec)
699 (afigs (if (> figs 0)
702 (1- (math-convert-radix-digits
704 (math-numdigs (nth 1 a)))))))))
705 (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t)))
706 (explo -1) (vlo (math-radix-float-power explo))
707 (exphi 1) (vhi (math-radix-float-power exphi))
709 (setq a (math-normalize a))
712 (if (math-lessp-float '(float 1 0) a)
713 (while (not (math-lessp-float a vhi))
714 (setq explo exphi vlo vhi
715 exphi (math-mul exphi 2)
716 vhi (math-radix-float-power exphi)))
717 (while (math-lessp-float a vlo)
718 (setq exphi explo vhi vlo
719 explo (math-mul explo 2)
720 vlo (math-radix-float-power explo))))
721 (while (not (eq (math-sub exphi explo) 1))
722 (setq expmid (math-div2 (math-add explo exphi))
723 vmid (math-radix-float-power expmid))
724 (if (math-lessp-float a vmid)
725 (setq exphi expmid vhi vmid)
726 (setq explo expmid vlo vmid)))
727 (setq a (math-div-float a vlo)))
728 (let* ((sc (math-round (math-mul a (math-radix-float-power
730 (math-radix-explicit-format nil))
731 (let ((calc-group-digits nil))
732 (setq str (math-format-number sc))))
733 (if (> (length str) afigs)
734 (setq str (substring str 0 -1)
736 (if (and (eq fmt 'float)
737 (math-lessp explo (+ (if (= figs 0)
738 (1- (math-convert-radix-digits
741 calc-display-sci-high 1))
742 (math-lessp calc-display-sci-low explo))
743 (let ((dpos (1+ explo)))
745 (setq str (concat "0" point (make-string (- dpos) ?0)
747 ((> dpos (length str))
748 (setq str (concat str (make-string (- dpos (length str))
751 (setq str (concat (substring str 0 dpos) point
752 (substring str dpos)))))
754 (setq eadj (if (eq fmt 'eng)
755 (min (math-mod explo 3) (length str))
757 str (concat (substring str 0 (1+ eadj)) point
758 (substring str (1+ eadj)))))
759 (setq pos (length str))
760 (while (eq (aref str (1- pos)) ?0) (setq pos (1- pos)))
761 (and explo (eq (aref str (1- pos)) ?.) (setq pos (1- pos)))
762 (setq str (substring str 0 pos))
763 (when calc-group-digits
764 (setq str (math-group-float str)))
766 (let ((estr (let ((calc-number-radix 10)
767 (calc-group-digits nil))
769 (math-sub explo eadj)))))
770 (setq str (if (or (memq calc-language '(math maple))
771 (> calc-number-radix 14))
772 (format "%s*%d.^%s" str calc-number-radix estr)
773 (format "%se%s" str estr)))))))
776 (defvar math-radix-digits-cache nil)
778 (defun math-convert-radix-digits (n &optional to-dec)
779 (let ((key (cons n (cons to-dec calc-number-radix))))
780 (or (cdr (assoc key math-radix-digits-cache))
781 (let* ((calc-internal-prec 6)
782 (log (math-div (math-real-log2 calc-number-radix)
783 '(float 332193 -5))))
784 (cdr (car (setq math-radix-digits-cache
785 (cons (cons key (math-ceiling (if to-dec
788 math-radix-digits-cache))))))))
790 (defvar math-radix-float-cache-tag nil)
791 (defvar math-radix-float-cache)
793 (defun math-radix-float-power (n)
796 (or (and (eq calc-number-radix (car math-radix-float-cache-tag))
797 (<= calc-internal-prec (cdr math-radix-float-cache-tag)))
798 (setq math-radix-float-cache-tag (cons calc-number-radix
800 math-radix-float-cache nil))
802 (or (cdr (assoc n math-radix-float-cache))
803 (cdr (car (setq math-radix-float-cache
806 (let ((calc-internal-prec
807 (cdr math-radix-float-cache-tag)))
809 (math-div-float '(float 1 0)
810 (math-radix-float-power
812 (math-mul-float (math-sqr-float
813 (math-radix-float-power
818 calc-number-radix))))))
819 math-radix-float-cache))))))))
821 ;;; Two's complement mode
823 (defun math-format-twos-complement (a)
824 "Format an integer in two's complement mode."
825 (let* (;(calc-leading-zeros t)
831 (and (Math-integer-posp a)))
833 (math-format-radix a)
834 (math-format-bignum-radix (cdr a))))
835 ((Math-integer-negp a)
836 (let ((newa (math-add a math-2-word-size)))
838 (math-format-radix newa)
839 (math-format-bignum-radix (cdr newa))))))))
840 (let* ((calc-internal-prec 6)
841 (digs (math-compute-max-digits (math-abs calc-word-size)
845 (setq num (concat (make-string (- digs len) ?0) num))))
846 (when calc-group-digits
847 (setq num (math-group-float num)))
849 (number-to-string calc-number-radix)
855 ;;; calc-bin.el ends here