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/>.
28 ;; This file is autoloaded from calc-ext.el.
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.
49 (calc-enter-result 2 "and"
50 (append '(calcFunc-and)
52 (and n
(list (prefix-numeric-value n
)))))))
57 (calc-enter-result 2 "or"
58 (append '(calcFunc-or)
60 (and n
(list (prefix-numeric-value n
)))))))
65 (calc-enter-result 2 "xor"
66 (append '(calcFunc-xor)
68 (and n
(list (prefix-numeric-value n
)))))))
73 (calc-enter-result 2 "diff"
74 (append '(calcFunc-diff)
76 (and n
(list (prefix-numeric-value n
)))))))
81 (calc-enter-result 1 "not"
82 (append '(calcFunc-not)
84 (and n
(list (prefix-numeric-value n
)))))))
86 (defun calc-lshift-binary (n)
89 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
90 (calc-enter-result hyp
"lsh"
91 (append '(calcFunc-lsh)
93 (and n
(list (prefix-numeric-value n
))))))))
95 (defun calc-rshift-binary (n)
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)
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)
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)
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
))))))))
134 (calc-enter-result 1 "clip"
135 (append '(calcFunc-clip)
137 (and n
(list (prefix-numeric-value n
)))))))
139 (defun calc-word-size (n)
142 (or n
(setq n
(read-string (format "Binary word size: (default %d) "
144 (setq n
(if (stringp n
)
147 (if (string-match "\\`[-+]?[0-9]+\\'" 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
))))
160 (calc-refresh-evaltos)
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): ")
174 (if (and (>= n
2) (<= n
36))
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 ()
190 (defun calc-binary-radix (&optional arg
)
194 (defun calc-octal-radix (&optional arg
)
198 (defun calc-hex-radix (&optional arg
)
202 (defun calc-leading-zeros (n)
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
)
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
)))
222 (setq val
(math-mul val
2)
223 math-power-of-2-cache
(nconc math-power-of-2-cache
227 (let ((found (assq n math-big-power-of-2-cache
)))
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
))
235 (defun math-integer-log2 (n) ; [I I] [Public]
237 (p math-power-of-2-cache
)
239 (while (and p
(Math-natnum-lessp (setq val
(car p
)) n
))
245 (while (Math-natnum-lessp
247 (setq val
(math-mul val
2))
248 (setq math-power-of-2-cache
(nconc math-power-of-2-cache
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
)))
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
)
288 (if (eq (car-safe a
) 'mod
)
292 (if (eq (car-safe b
) 'mod
)
293 (if (equal mod
(nth 2 b
))
295 (math-reject-arg b
"*Inconsistent modulos"))))
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
)))
307 "*Warning: Modulo inconsistent with word size"))
309 (calc-record-why "*Warning: Modulo is not a power of 2"))
315 (defun math-and-bignum (a b
) ; [l l l]
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
)))
342 (defun math-or-bignum (a b
) ; [l l l]
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
)))
369 (defun math-xor-bignum (a b
) ; [l l l]
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
)))
396 (defun math-diff-bignum (a b
) ; [l l l]
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
))
418 (math-not-bignum (math-binary-arg a 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
))
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
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
))
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
))
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
))
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]
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
))
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
))
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)
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
))
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
))
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
)))))
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
)))))
538 (math-clip-bignum (cdr (math-bignum-test (math-trunc a
)))
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
)
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
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
)))
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
))
565 (defvar math-log2-cache
(list '(2 .
1)
568 '(10 .
(float 332193 -
5))
571 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
572 (let ((res (assq x math-log2-cache
)))
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
))
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
)
592 (concat "-" (math-format-radix (- a
)))
593 (math-format-radix-digit a
))
596 (setq s
(concat (math-format-radix-digit (% a calc-number-radix
)) s
)
597 a
(/ a calc-number-radix
)))
600 (defconst math-binary-digits
["000" "001" "010" "011"
601 "100" "101" "110" "111"])
602 (defun math-format-binary (a) ; [X S]
605 (concat "-" (math-format-binary (- a
)))
606 (math-format-radix a
))
609 (setq s
(concat (aref math-binary-digits
(% a
8)) s
)
611 (concat (math-format-radix a
) s
))))
613 (defun math-format-bignum-radix (a) ; [X L]
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]
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]
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]
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 )
659 (list (math-scale-rounding (nth 1 a
) (nth 2 a
)) '(float 0 0) 0)
660 (let* ((d (math-numdigs (nth 1 a
)))
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
))
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
)
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
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)
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
))))
700 (let* ((prec calc-internal-prec
)
701 (afigs (if (> figs
0)
704 (1- (math-convert-radix-digits
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
))
711 (setq a
(math-normalize a
))
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
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)
738 (if (and (eq fmt
'float
)
739 (math-lessp explo
(+ (if (= figs
0)
740 (1- (math-convert-radix-digits
743 calc-display-sci-high
1))
744 (math-lessp calc-display-sci-low explo
))
745 (let ((dpos (1+ explo
)))
747 (setq str
(concat "0" point
(make-string (- dpos
) ?
0)
749 ((> dpos
(length str
))
750 (setq str
(concat str
(make-string (- dpos
(length str
))
753 (setq str
(concat (substring str
0 dpos
) point
754 (substring str dpos
)))))
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
)))
768 (let ((estr (let ((calc-number-radix 10)
769 (calc-group-digits nil
))
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
)))))))
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
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)
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
802 math-radix-float-cache nil
))
804 (or (cdr (assoc n math-radix-float-cache
))
805 (cdr (car (setq math-radix-float-cache
808 (let ((calc-internal-prec
809 (cdr math-radix-float-cache-tag
)))
811 (math-div-float '(float 1 0)
812 (math-radix-float-power
814 (math-mul-float (math-sqr-float
815 (math-radix-float-power
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)
833 (and (Math-integer-posp 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
)))
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
)
847 (setq num
(concat (make-string (- digs len
) ?
0) num
))))
849 (number-to-string calc-number-radix
)
855 ;; arch-tag: f6dba7bc-53b2-41ae-919c-c266ab0ca8b3
856 ;;; calc-bin.el ends here