1 ;;; calc-bin.el --- binary functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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, or (at your option)
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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc-ext.el.
35 ;;; Some useful numbers
36 (defconst math-bignum-logb-digit-size
37 (logb math-bignum-digit-size
)
38 "The logb of the size of a bignum digit.
39 This is the largest value of B such that 2^B is less than
40 the size of a Calc bignum digit.")
42 (defconst math-bignum-digit-power-of-two
43 (expt 2 (logb math-bignum-digit-size
))
44 "The largest power of 2 less than the size of a Calc bignum digit.")
46 ;;; b-prefix binary commands.
51 (calc-enter-result 2 "and"
52 (append '(calcFunc-and)
54 (and n
(list (prefix-numeric-value n
)))))))
59 (calc-enter-result 2 "or"
60 (append '(calcFunc-or)
62 (and n
(list (prefix-numeric-value n
)))))))
67 (calc-enter-result 2 "xor"
68 (append '(calcFunc-xor)
70 (and n
(list (prefix-numeric-value n
)))))))
75 (calc-enter-result 2 "diff"
76 (append '(calcFunc-diff)
78 (and n
(list (prefix-numeric-value n
)))))))
83 (calc-enter-result 1 "not"
84 (append '(calcFunc-not)
86 (and n
(list (prefix-numeric-value n
)))))))
88 (defun calc-lshift-binary (n)
91 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
92 (calc-enter-result hyp
"lsh"
93 (append '(calcFunc-lsh)
95 (and n
(list (prefix-numeric-value n
))))))))
97 (defun calc-rshift-binary (n)
100 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
101 (calc-enter-result hyp
"rsh"
102 (append '(calcFunc-rsh)
103 (calc-top-list-n hyp
)
104 (and n
(list (prefix-numeric-value n
))))))))
106 (defun calc-lshift-arith (n)
109 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
110 (calc-enter-result hyp
"ash"
111 (append '(calcFunc-ash)
112 (calc-top-list-n hyp
)
113 (and n
(list (prefix-numeric-value n
))))))))
115 (defun calc-rshift-arith (n)
118 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
119 (calc-enter-result hyp
"rash"
120 (append '(calcFunc-rash)
121 (calc-top-list-n hyp
)
122 (and n
(list (prefix-numeric-value n
))))))))
124 (defun calc-rotate-binary (n)
127 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
128 (calc-enter-result hyp
"rot"
129 (append '(calcFunc-rot)
130 (calc-top-list-n hyp
)
131 (and n
(list (prefix-numeric-value n
))))))))
136 (calc-enter-result 1 "clip"
137 (append '(calcFunc-clip)
139 (and n
(list (prefix-numeric-value n
)))))))
141 (defun calc-word-size (n)
144 (or n
(setq n
(read-string (format "Binary word size: (default %d) "
146 (setq n
(if (stringp n
)
149 (if (string-match "\\`[-+]?[0-9]+\\'" n
)
151 (error "Expected an integer")))
152 (prefix-numeric-value n
)))
153 (or (= n calc-word-size
)
154 (if (> (math-abs n
) 100)
155 (calc-change-mode 'calc-word-size n calc-leading-zeros
)
156 (calc-change-mode '(calc-word-size calc-previous-modulo
)
157 (list n
(math-power-of-2 (math-abs n
)))
158 calc-leading-zeros
)))
160 (message "Binary word size is %d bits (2's complement)" (- n
))
161 (message "Binary word size is %d bits" n
))))
167 ;;; d-prefix mode commands.
169 (defun calc-radix (n)
170 (interactive "NDisplay radix (2-36): ")
172 (if (and (>= n
2) (<= n
36))
174 (calc-change-mode 'calc-number-radix n t
)
175 ;; also change global value so minibuffer sees it
176 (setq-default calc-number-radix calc-number-radix
))
177 (setq n calc-number-radix
))
178 (message "Number radix is %d" n
)))
180 (defun calc-decimal-radix ()
184 (defun calc-binary-radix ()
188 (defun calc-octal-radix ()
192 (defun calc-hex-radix ()
196 (defun calc-leading-zeros (n)
199 (if (calc-change-mode 'calc-leading-zeros n t t
)
200 (message "Zero-padding integers to %d digits (assuming radix %d)"
201 (let* ((calc-internal-prec 6))
202 (math-compute-max-digits (math-abs calc-word-size
)
205 (message "Omitting leading zeros on integers"))))
208 (defvar math-power-of-2-cache
(list 1 2 4 8 16 32 64 128 256 512 1024))
209 (defvar math-big-power-of-2-cache nil
)
210 (defun math-power-of-2 (n) ; [I I] [Public]
211 (if (and (natnump n
) (<= n
100))
212 (or (nth n math-power-of-2-cache
)
213 (let* ((i (length math-power-of-2-cache
))
214 (val (nth (1- i
) math-power-of-2-cache
)))
216 (setq val
(math-mul val
2)
217 math-power-of-2-cache
(nconc math-power-of-2-cache
221 (let ((found (assq n math-big-power-of-2-cache
)))
224 (let ((po2 (math-ipow 2 n
)))
225 (setq math-big-power-of-2-cache
226 (cons (cons n po2
) math-big-power-of-2-cache
))
229 (defun math-integer-log2 (n) ; [I I] [Public]
231 (p math-power-of-2-cache
)
233 (while (and p
(Math-natnum-lessp (setq val
(car p
)) n
))
239 (while (Math-natnum-lessp
241 (setq val
(math-mul val
2))
242 (setq math-power-of-2-cache
(nconc math-power-of-2-cache
252 ;;; Bitwise operations.
254 (defun calcFunc-and (a b
&optional w
) ; [I I I] [Public]
255 (cond ((Math-messy-integerp w
)
256 (calcFunc-and a b
(math-trunc w
)))
257 ((and w
(not (integerp w
)))
258 (math-reject-arg w
'fixnump
))
259 ((and (integerp a
) (integerp b
))
260 (math-clip (logand a b
) w
))
261 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
262 (math-binary-modulo-args 'calcFunc-and a b w
))
263 ((not (Math-num-integerp a
))
264 (math-reject-arg a
'integerp
))
265 ((not (Math-num-integerp b
))
266 (math-reject-arg b
'integerp
))
267 (t (math-clip (cons 'bigpos
268 (math-and-bignum (math-binary-arg a w
)
269 (math-binary-arg b w
)))
272 (defun math-binary-arg (a w
)
273 (if (not (Math-integerp a
))
274 (setq a
(math-trunc a
)))
275 (if (Math-integer-negp a
)
276 (math-not-bignum (cdr (math-bignum-test (math-sub -
1 a
)))
277 (math-abs (if w
(math-trunc w
) calc-word-size
)))
278 (cdr (Math-bignum-test a
))))
280 (defun math-binary-modulo-args (f a b w
)
282 (if (eq (car-safe a
) 'mod
)
286 (if (eq (car-safe b
) 'mod
)
287 (if (equal mod
(nth 2 b
))
289 (math-reject-arg b
"*Inconsistent modulos"))))
292 (if (Math-messy-integerp mod
)
293 (setq mod
(math-trunc mod
))
294 (or (Math-integerp mod
)
295 (math-reject-arg mod
'integerp
)))
296 (let ((bits (math-integer-log2 mod
)))
301 "*Warning: Modulo inconsistent with word size"))
303 (calc-record-why "*Warning: Modulo is not a power of 2"))
309 (defun math-and-bignum (a b
) ; [l l l]
311 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two
))
312 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two
)))
313 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa
))
314 (math-norm-bignum (car qb
)))
315 math-bignum-digit-power-of-two
316 (logand (cdr qa
) (cdr qb
))))))
318 (defun calcFunc-or (a b
&optional w
) ; [I I I] [Public]
319 (cond ((Math-messy-integerp w
)
320 (calcFunc-or a b
(math-trunc w
)))
321 ((and w
(not (integerp w
)))
322 (math-reject-arg w
'fixnump
))
323 ((and (integerp a
) (integerp b
))
324 (math-clip (logior a b
) w
))
325 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
326 (math-binary-modulo-args 'calcFunc-or a b w
))
327 ((not (Math-num-integerp a
))
328 (math-reject-arg a
'integerp
))
329 ((not (Math-num-integerp b
))
330 (math-reject-arg b
'integerp
))
331 (t (math-clip (cons 'bigpos
332 (math-or-bignum (math-binary-arg a w
)
333 (math-binary-arg b w
)))
336 (defun math-or-bignum (a b
) ; [l l l]
338 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two
))
339 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two
)))
340 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa
))
341 (math-norm-bignum (car qb
)))
342 math-bignum-digit-power-of-two
343 (logior (cdr qa
) (cdr qb
))))))
345 (defun calcFunc-xor (a b
&optional w
) ; [I I I] [Public]
346 (cond ((Math-messy-integerp w
)
347 (calcFunc-xor a b
(math-trunc w
)))
348 ((and w
(not (integerp w
)))
349 (math-reject-arg w
'fixnump
))
350 ((and (integerp a
) (integerp b
))
351 (math-clip (logxor a b
) w
))
352 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
353 (math-binary-modulo-args 'calcFunc-xor a b w
))
354 ((not (Math-num-integerp a
))
355 (math-reject-arg a
'integerp
))
356 ((not (Math-num-integerp b
))
357 (math-reject-arg b
'integerp
))
358 (t (math-clip (cons 'bigpos
359 (math-xor-bignum (math-binary-arg a w
)
360 (math-binary-arg b w
)))
363 (defun math-xor-bignum (a b
) ; [l l l]
365 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two
))
366 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two
)))
367 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa
))
368 (math-norm-bignum (car qb
)))
369 math-bignum-digit-power-of-two
370 (logxor (cdr qa
) (cdr qb
))))))
372 (defun calcFunc-diff (a b
&optional w
) ; [I I I] [Public]
373 (cond ((Math-messy-integerp w
)
374 (calcFunc-diff a b
(math-trunc w
)))
375 ((and w
(not (integerp w
)))
376 (math-reject-arg w
'fixnump
))
377 ((and (integerp a
) (integerp b
))
378 (math-clip (logand a
(lognot b
)) w
))
379 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
380 (math-binary-modulo-args 'calcFunc-diff a b w
))
381 ((not (Math-num-integerp a
))
382 (math-reject-arg a
'integerp
))
383 ((not (Math-num-integerp b
))
384 (math-reject-arg b
'integerp
))
385 (t (math-clip (cons 'bigpos
386 (math-diff-bignum (math-binary-arg a w
)
387 (math-binary-arg b w
)))
390 (defun math-diff-bignum (a b
) ; [l l l]
392 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two
))
393 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two
)))
394 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa
))
395 (math-norm-bignum (car qb
)))
396 math-bignum-digit-power-of-two
397 (logand (cdr qa
) (lognot (cdr qb
)))))))
399 (defun calcFunc-not (a &optional w
) ; [I I] [Public]
400 (cond ((Math-messy-integerp w
)
401 (calcFunc-not a
(math-trunc w
)))
402 ((eq (car-safe a
) 'mod
)
403 (math-binary-modulo-args 'calcFunc-not a nil w
))
404 ((and w
(not (integerp w
)))
405 (math-reject-arg w
'fixnump
))
406 ((not (Math-num-integerp a
))
407 (math-reject-arg a
'integerp
))
408 ((< (or w
(setq w calc-word-size
)) 0)
409 (math-clip (calcFunc-not a
(- w
)) w
))
412 (math-not-bignum (math-binary-arg a w
)
415 (defun math-not-bignum (a w
) ; [l l]
416 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two
)))
417 (if (<= w math-bignum-logb-digit-size
)
418 (list (logand (lognot (cdr q
))
420 (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q
))
421 (- w math-bignum-logb-digit-size
))
422 math-bignum-digit-power-of-two
424 (1- math-bignum-digit-power-of-two
))))))
426 (defun calcFunc-lsh (a &optional n w
) ; [I I] [Public]
427 (setq a
(math-trunc a
)
428 n
(if n
(math-trunc n
) 1))
429 (if (eq (car-safe a
) 'mod
)
430 (math-binary-modulo-args 'calcFunc-lsh a n w
)
431 (setq w
(if w
(math-trunc w
) calc-word-size
))
433 (math-reject-arg w
'fixnump
))
434 (or (Math-integerp a
)
435 (math-reject-arg a
'integerp
))
436 (or (Math-integerp n
)
437 (math-reject-arg n
'integerp
))
439 (math-clip (calcFunc-lsh a n
(- w
)) w
)
440 (if (Math-integer-negp a
)
441 (setq a
(math-clip a w
)))
442 (cond ((or (Math-lessp n
(- w
))
446 (math-quotient (math-clip a w
) (math-power-of-2 (- n
))))
448 (math-clip (math-mul a
(math-power-of-2 n
)) w
))))))
450 (defun calcFunc-rsh (a &optional n w
) ; [I I] [Public]
451 (calcFunc-lsh a
(math-neg (or n
1)) w
))
453 (defun calcFunc-ash (a &optional n w
) ; [I I] [Public]
457 (setq a
(math-trunc a
)
458 n
(if n
(math-trunc n
) 1))
459 (if (eq (car-safe a
) 'mod
)
460 (math-binary-modulo-args 'calcFunc-ash a n w
)
461 (setq w
(if w
(math-trunc w
) calc-word-size
))
463 (math-reject-arg w
'fixnump
))
464 (or (Math-integerp a
)
465 (math-reject-arg a
'integerp
))
466 (or (Math-integerp n
)
467 (math-reject-arg n
'integerp
))
469 (math-clip (calcFunc-ash a n
(- w
)) w
)
470 (if (Math-integer-negp a
)
471 (setq a
(math-clip a w
)))
472 (let ((two-to-sizem1 (math-power-of-2 (1- w
)))
473 (sh (calcFunc-lsh a n w
)))
474 (cond ((Math-natnum-lessp a two-to-sizem1
)
476 ((Math-lessp n
(- 1 w
))
477 (math-add (math-mul two-to-sizem1
2) -
1))
478 (t (let ((two-to-n (math-power-of-2 (- n
))))
479 (math-add (calcFunc-lsh (math-add two-to-n -
1)
483 (defun calcFunc-rash (a &optional n w
) ; [I I] [Public]
484 (calcFunc-ash a
(math-neg (or n
1)) w
))
486 (defun calcFunc-rot (a &optional n w
) ; [I I] [Public]
487 (setq a
(math-trunc a
)
488 n
(if n
(math-trunc n
) 1))
489 (if (eq (car-safe a
) 'mod
)
490 (math-binary-modulo-args 'calcFunc-rot a n w
)
491 (setq w
(if w
(math-trunc w
) calc-word-size
))
493 (math-reject-arg w
'fixnump
))
494 (or (Math-integerp a
)
495 (math-reject-arg a
'integerp
))
496 (or (Math-integerp n
)
497 (math-reject-arg n
'integerp
))
499 (math-clip (calcFunc-rot a n
(- w
)) w
)
500 (if (Math-integer-negp a
)
501 (setq a
(math-clip a w
)))
502 (cond ((or (Math-integer-negp n
)
503 (not (Math-natnum-lessp n w
)))
504 (calcFunc-rot a
(math-mod n w
) w
))
506 (math-add (calcFunc-lsh a
(- n w
) w
)
507 (calcFunc-lsh a n w
)))))))
509 (defun math-clip (a &optional w
) ; [I I] [Public]
510 (cond ((Math-messy-integerp w
)
511 (math-clip a
(math-trunc w
)))
512 ((eq (car-safe a
) 'mod
)
513 (math-binary-modulo-args 'math-clip a nil w
))
514 ((and w
(not (integerp w
)))
515 (math-reject-arg w
'fixnump
))
516 ((not (Math-num-integerp a
))
517 (math-reject-arg a
'integerp
))
518 ((< (or w
(setq w calc-word-size
)) 0)
519 (setq a
(math-clip a
(- w
)))
520 (if (Math-natnum-lessp a
(math-power-of-2 (- -
1 w
)))
522 (math-sub a
(math-power-of-2 (- w
)))))
524 (math-normalize (cons 'bigpos
(math-binary-arg a w
))))
525 ((and (integerp a
) (< a math-small-integer-size
))
526 (if (> w
(logb math-small-integer-size
))
528 (logand a
(1- (lsh 1 w
)))))
532 (math-clip-bignum (cdr (math-bignum-test (math-trunc a
)))
535 (defalias 'calcFunc-clip
'math-clip
)
537 (defun math-clip-bignum (a w
) ; [l l]
538 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two
)))
539 (if (<= w math-bignum-logb-digit-size
)
540 (list (logand (cdr q
)
542 (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q
))
543 (- w math-bignum-logb-digit-size
))
544 math-bignum-digit-power-of-two
547 (defvar math-max-digits-cache nil
)
548 (defun math-compute-max-digits (w r
)
549 (let* ((pair (+ (* r
100000) w
))
550 (res (assq pair math-max-digits-cache
)))
553 (let* ((calc-command-flags nil
)
554 (digs (math-ceiling (math-div w
(math-real-log2 r
)))))
555 (setq math-max-digits-cache
(cons (cons pair digs
)
556 math-max-digits-cache
))
559 (defvar math-log2-cache
(list '(2 .
1)
562 '(10 .
(float 332193 -
5))
565 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
566 (let ((res (assq x math-log2-cache
)))
569 (let* ((calc-symbolic-mode nil
)
570 (calc-display-working-message nil
)
571 (log (calcFunc-log x
2)))
572 (setq math-log2-cache
(cons (cons x log
) math-log2-cache
))
575 (defconst math-radix-digits
["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
576 "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
577 "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
578 "U" "V" "W" "X" "Y" "Z"])
580 (defsubst math-format-radix-digit
(a) ; [X D]
581 (aref math-radix-digits a
))
583 (defun math-format-radix (a) ; [X S]
584 (if (< a calc-number-radix
)
586 (concat "-" (math-format-radix (- a
)))
587 (math-format-radix-digit a
))
590 (setq s
(concat (math-format-radix-digit (% a calc-number-radix
)) s
)
591 a
(/ a calc-number-radix
)))
594 (defconst math-binary-digits
["000" "001" "010" "011"
595 "100" "101" "110" "111"])
596 (defun math-format-binary (a) ; [X S]
599 (concat "-" (math-format-binary (- a
)))
600 (math-format-radix a
))
603 (setq s
(concat (aref math-binary-digits
(% a
8)) s
)
605 (concat (math-format-radix a
) s
))))
607 (defun math-format-bignum-radix (a) ; [X L]
610 (< (car a
) calc-number-radix
))
611 (math-format-radix-digit (car a
)))
613 (let ((q (math-div-bignum-digit a calc-number-radix
)))
614 (concat (math-format-bignum-radix (math-norm-bignum (car q
)))
615 (math-format-radix-digit (cdr q
)))))))
617 (defun math-format-bignum-binary (a) ; [X L]
620 (math-format-binary (car a
)))
622 (let ((q (math-div-bignum-digit a
512)))
623 (concat (math-format-bignum-binary (math-norm-bignum (car q
)))
624 (aref math-binary-digits
(/ (cdr q
) 64))
625 (aref math-binary-digits
(%
(/ (cdr q
) 8) 8))
626 (aref math-binary-digits
(%
(cdr q
) 8)))))))
628 (defun math-format-bignum-octal (a) ; [X L]
631 (math-format-radix (car a
)))
633 (let ((q (math-div-bignum-digit a
512)))
634 (concat (math-format-bignum-octal (math-norm-bignum (car q
)))
635 (math-format-radix-digit (/ (cdr q
) 64))
636 (math-format-radix-digit (%
(/ (cdr q
) 8) 8))
637 (math-format-radix-digit (%
(cdr q
) 8)))))))
639 (defun math-format-bignum-hex (a) ; [X L]
642 (math-format-radix (car a
)))
644 (let ((q (math-div-bignum-digit a
256)))
645 (concat (math-format-bignum-hex (math-norm-bignum (car q
)))
646 (math-format-radix-digit (/ (cdr q
) 16))
647 (math-format-radix-digit (%
(cdr q
) 16)))))))
649 ;;; Decompose into integer and fractional parts, without depending
650 ;;; on calc-internal-prec.
651 (defun math-float-parts (a need-frac
) ; returns ( int frac fracdigs )
653 (list (math-scale-rounding (nth 1 a
) (nth 2 a
)) '(float 0 0) 0)
654 (let* ((d (math-numdigs (nth 1 a
)))
659 (let ((qr (math-idivmod (nth 1 a
) (math-scale-int 1 n
))))
660 (list (car qr
) (math-make-float (cdr qr
) (- n
)) n
)))
661 (list (math-scale-rounding (nth 1 a
) (nth 2 a
))
664 (defun math-format-radix-float (a prec
)
665 (let ((fmt (car calc-float-format
))
666 (figs (nth 1 calc-float-format
))
667 (point calc-point-char
)
671 (let* ((afigs (math-abs figs
))
672 (fp (math-float-parts a
(> afigs
0)))
673 (calc-internal-prec (+ 3 (max (nth 2 fp
)
674 (math-convert-radix-digits
677 (frac (math-round (math-mul (math-normalize (nth 1 fp
))
678 (math-radix-float-power afigs
)))))
679 (if (not (and (math-zerop frac
) (math-zerop int
) (< figs
0)))
680 (let ((math-radix-explicit-format nil
))
681 (let ((calc-group-digits nil
))
682 (setq str
(if (> afigs
0) (math-format-number frac
) ""))
683 (if (< (length str
) afigs
)
684 (setq str
(concat (make-string (- afigs
(length str
)) ?
0)
686 (if (> (length str
) afigs
)
687 (setq str
(substring str
1)
688 int
(math-add int
1))))
689 (setq str
(concat (math-format-number int
) point str
)))
690 (when calc-group-digits
691 (setq str
(math-group-float str
))))
694 (let* ((prec calc-internal-prec
)
695 (afigs (if (> figs
0)
698 (1- (math-convert-radix-digits
700 (math-numdigs (nth 1 a
)))))))))
701 (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t
)))
702 (explo -
1) (vlo (math-radix-float-power explo
))
703 (exphi 1) (vhi (math-radix-float-power exphi
))
705 (setq a
(math-normalize a
))
708 (if (math-lessp-float '(float 1 0) a
)
709 (while (not (math-lessp-float a vhi
))
710 (setq explo exphi vlo vhi
711 exphi
(math-mul exphi
2)
712 vhi
(math-radix-float-power exphi
)))
713 (while (math-lessp-float a vlo
)
714 (setq exphi explo vhi vlo
715 explo
(math-mul explo
2)
716 vlo
(math-radix-float-power explo
))))
717 (while (not (eq (math-sub exphi explo
) 1))
718 (setq expmid
(math-div2 (math-add explo exphi
))
719 vmid
(math-radix-float-power expmid
))
720 (if (math-lessp-float a vmid
)
721 (setq exphi expmid vhi vmid
)
722 (setq explo expmid vlo vmid
)))
723 (setq a
(math-div-float a vlo
)))
724 (let* ((sc (math-round (math-mul a
(math-radix-float-power
726 (math-radix-explicit-format nil
))
727 (let ((calc-group-digits nil
))
728 (setq str
(math-format-number sc
))))
729 (if (> (length str
) afigs
)
730 (setq str
(substring str
0 -
1)
732 (if (and (eq fmt
'float
)
733 (math-lessp explo
(+ (if (= figs
0)
734 (1- (math-convert-radix-digits
737 calc-display-sci-high
1))
738 (math-lessp calc-display-sci-low explo
))
739 (let ((dpos (1+ explo
)))
741 (setq str
(concat "0" point
(make-string (- dpos
) ?
0)
743 ((> dpos
(length str
))
744 (setq str
(concat str
(make-string (- dpos
(length str
))
747 (setq str
(concat (substring str
0 dpos
) point
748 (substring str dpos
)))))
750 (setq eadj
(if (eq fmt
'eng
)
751 (min (math-mod explo
3) (length str
))
753 str
(concat (substring str
0 (1+ eadj
)) point
754 (substring str
(1+ eadj
)))))
755 (setq pos
(length str
))
756 (while (eq (aref str
(1- pos
)) ?
0) (setq pos
(1- pos
)))
757 (and explo
(eq (aref str
(1- pos
)) ?.
) (setq pos
(1- pos
)))
758 (setq str
(substring str
0 pos
))
759 (when calc-group-digits
760 (setq str
(math-group-float str
)))
762 (let ((estr (let ((calc-number-radix 10)
763 (calc-group-digits nil
))
765 (math-sub explo eadj
)))))
766 (setq str
(if (or (memq calc-language
'(math maple
))
767 (> calc-number-radix
14))
768 (format "%s*%d.^%s" str calc-number-radix estr
)
769 (format "%se%s" str estr
)))))))
772 (defvar math-radix-digits-cache nil
)
774 (defun math-convert-radix-digits (n &optional to-dec
)
775 (let ((key (cons n
(cons to-dec calc-number-radix
))))
776 (or (cdr (assoc key math-radix-digits-cache
))
777 (let* ((calc-internal-prec 6)
778 (log (math-div (math-real-log2 calc-number-radix
)
779 '(float 332193 -
5))))
780 (cdr (car (setq math-radix-digits-cache
781 (cons (cons key
(math-ceiling (if to-dec
784 math-radix-digits-cache
))))))))
786 (defvar math-radix-float-cache-tag nil
)
787 (defvar math-radix-float-cache
)
789 (defun math-radix-float-power (n)
792 (or (and (eq calc-number-radix
(car math-radix-float-cache-tag
))
793 (<= calc-internal-prec
(cdr math-radix-float-cache-tag
)))
794 (setq math-radix-float-cache-tag
(cons calc-number-radix
796 math-radix-float-cache nil
))
798 (or (cdr (assoc n math-radix-float-cache
))
799 (cdr (car (setq math-radix-float-cache
802 (let ((calc-internal-prec
803 (cdr math-radix-float-cache-tag
)))
805 (math-div-float '(float 1 0)
806 (math-radix-float-power
808 (math-mul-float (math-sqr-float
809 (math-radix-float-power
814 calc-number-radix
))))))
815 math-radix-float-cache
))))))))
819 ;;; arch-tag: f6dba7bc-53b2-41ae-919c-c266ab0ca8b3
820 ;;; calc-bin.el ends here