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 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
)))
158 (message "Binary word size is %d bits (2's complement)" (- n
))
159 (message "Binary word size is %d bits" n
))))
165 ;;; d-prefix mode commands.
167 (defun calc-radix (n)
168 (interactive "NDisplay radix (2-36): ")
170 (if (and (>= n
2) (<= n
36))
172 (calc-change-mode 'calc-number-radix n t
)
173 ;; also change global value so minibuffer sees it
174 (setq-default calc-number-radix calc-number-radix
))
175 (setq n calc-number-radix
))
176 (message "Number radix is %d" n
)))
178 (defun calc-decimal-radix ()
182 (defun calc-binary-radix ()
186 (defun calc-octal-radix ()
190 (defun calc-hex-radix ()
194 (defun calc-leading-zeros (n)
197 (if (calc-change-mode 'calc-leading-zeros n t t
)
198 (message "Zero-padding integers to %d digits (assuming radix %d)"
199 (let* ((calc-internal-prec 6))
200 (math-compute-max-digits (math-abs calc-word-size
)
203 (message "Omitting leading zeros on integers"))))
206 (defvar math-power-of-2-cache
(list 1 2 4 8 16 32 64 128 256 512 1024))
207 (defvar math-big-power-of-2-cache nil
)
208 (defun math-power-of-2 (n) ; [I I] [Public]
209 (if (and (natnump n
) (<= n
100))
210 (or (nth n math-power-of-2-cache
)
211 (let* ((i (length math-power-of-2-cache
))
212 (val (nth (1- i
) math-power-of-2-cache
)))
214 (setq val
(math-mul val
2)
215 math-power-of-2-cache
(nconc math-power-of-2-cache
219 (let ((found (assq n math-big-power-of-2-cache
)))
222 (let ((po2 (math-ipow 2 n
)))
223 (setq math-big-power-of-2-cache
224 (cons (cons n po2
) math-big-power-of-2-cache
))
227 (defun math-integer-log2 (n) ; [I I] [Public]
229 (p math-power-of-2-cache
)
231 (while (and p
(Math-natnum-lessp (setq val
(car p
)) n
))
237 (while (Math-natnum-lessp
239 (setq val
(math-mul val
2))
240 (setq math-power-of-2-cache
(nconc math-power-of-2-cache
250 ;;; Bitwise operations.
252 (defun calcFunc-and (a b
&optional w
) ; [I I I] [Public]
253 (cond ((Math-messy-integerp w
)
254 (calcFunc-and a b
(math-trunc w
)))
255 ((and w
(not (integerp w
)))
256 (math-reject-arg w
'fixnump
))
257 ((and (integerp a
) (integerp b
))
258 (math-clip (logand a b
) w
))
259 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
260 (math-binary-modulo-args 'calcFunc-and a b w
))
261 ((not (Math-num-integerp a
))
262 (math-reject-arg a
'integerp
))
263 ((not (Math-num-integerp b
))
264 (math-reject-arg b
'integerp
))
265 (t (math-clip (cons 'bigpos
266 (math-and-bignum (math-binary-arg a w
)
267 (math-binary-arg b w
)))
270 (defun math-binary-arg (a w
)
271 (if (not (Math-integerp a
))
272 (setq a
(math-trunc a
)))
273 (if (Math-integer-negp a
)
274 (math-not-bignum (cdr (math-bignum-test (math-sub -
1 a
)))
275 (math-abs (if w
(math-trunc w
) calc-word-size
)))
276 (cdr (Math-bignum-test a
))))
278 (defun math-binary-modulo-args (f a b w
)
280 (if (eq (car-safe a
) 'mod
)
284 (if (eq (car-safe b
) 'mod
)
285 (if (equal mod
(nth 2 b
))
287 (math-reject-arg b
"*Inconsistent modulos"))))
290 (if (Math-messy-integerp mod
)
291 (setq mod
(math-trunc mod
))
292 (or (Math-integerp mod
)
293 (math-reject-arg mod
'integerp
)))
294 (let ((bits (math-integer-log2 mod
)))
299 "*Warning: Modulo inconsistent with word size"))
301 (calc-record-why "*Warning: Modulo is not a power of 2"))
307 (defun math-and-bignum (a b
) ; [l l l]
309 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two
))
310 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two
)))
311 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa
))
312 (math-norm-bignum (car qb
)))
313 math-bignum-digit-power-of-two
314 (logand (cdr qa
) (cdr qb
))))))
316 (defun calcFunc-or (a b
&optional w
) ; [I I I] [Public]
317 (cond ((Math-messy-integerp w
)
318 (calcFunc-or a b
(math-trunc w
)))
319 ((and w
(not (integerp w
)))
320 (math-reject-arg w
'fixnump
))
321 ((and (integerp a
) (integerp b
))
322 (math-clip (logior a b
) w
))
323 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
324 (math-binary-modulo-args 'calcFunc-or a b w
))
325 ((not (Math-num-integerp a
))
326 (math-reject-arg a
'integerp
))
327 ((not (Math-num-integerp b
))
328 (math-reject-arg b
'integerp
))
329 (t (math-clip (cons 'bigpos
330 (math-or-bignum (math-binary-arg a w
)
331 (math-binary-arg b w
)))
334 (defun math-or-bignum (a b
) ; [l l l]
336 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two
))
337 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two
)))
338 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa
))
339 (math-norm-bignum (car qb
)))
340 math-bignum-digit-power-of-two
341 (logior (cdr qa
) (cdr qb
))))))
343 (defun calcFunc-xor (a b
&optional w
) ; [I I I] [Public]
344 (cond ((Math-messy-integerp w
)
345 (calcFunc-xor a b
(math-trunc w
)))
346 ((and w
(not (integerp w
)))
347 (math-reject-arg w
'fixnump
))
348 ((and (integerp a
) (integerp b
))
349 (math-clip (logxor a b
) w
))
350 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
351 (math-binary-modulo-args 'calcFunc-xor a b w
))
352 ((not (Math-num-integerp a
))
353 (math-reject-arg a
'integerp
))
354 ((not (Math-num-integerp b
))
355 (math-reject-arg b
'integerp
))
356 (t (math-clip (cons 'bigpos
357 (math-xor-bignum (math-binary-arg a w
)
358 (math-binary-arg b w
)))
361 (defun math-xor-bignum (a b
) ; [l l l]
363 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two
))
364 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two
)))
365 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa
))
366 (math-norm-bignum (car qb
)))
367 math-bignum-digit-power-of-two
368 (logxor (cdr qa
) (cdr qb
))))))
370 (defun calcFunc-diff (a b
&optional w
) ; [I I I] [Public]
371 (cond ((Math-messy-integerp w
)
372 (calcFunc-diff a b
(math-trunc w
)))
373 ((and w
(not (integerp w
)))
374 (math-reject-arg w
'fixnump
))
375 ((and (integerp a
) (integerp b
))
376 (math-clip (logand a
(lognot b
)) w
))
377 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
378 (math-binary-modulo-args 'calcFunc-diff a b w
))
379 ((not (Math-num-integerp a
))
380 (math-reject-arg a
'integerp
))
381 ((not (Math-num-integerp b
))
382 (math-reject-arg b
'integerp
))
383 (t (math-clip (cons 'bigpos
384 (math-diff-bignum (math-binary-arg a w
)
385 (math-binary-arg b w
)))
388 (defun math-diff-bignum (a b
) ; [l l l]
390 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two
))
391 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two
)))
392 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa
))
393 (math-norm-bignum (car qb
)))
394 math-bignum-digit-power-of-two
395 (logand (cdr qa
) (lognot (cdr qb
)))))))
397 (defun calcFunc-not (a &optional w
) ; [I I] [Public]
398 (cond ((Math-messy-integerp w
)
399 (calcFunc-not a
(math-trunc w
)))
400 ((eq (car-safe a
) 'mod
)
401 (math-binary-modulo-args 'calcFunc-not a nil w
))
402 ((and w
(not (integerp w
)))
403 (math-reject-arg w
'fixnump
))
404 ((not (Math-num-integerp a
))
405 (math-reject-arg a
'integerp
))
406 ((< (or w
(setq w calc-word-size
)) 0)
407 (math-clip (calcFunc-not a
(- w
)) w
))
410 (math-not-bignum (math-binary-arg a w
)
413 (defun math-not-bignum (a w
) ; [l l]
414 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two
)))
415 (if (<= w math-bignum-logb-digit-size
)
416 (list (logand (lognot (cdr q
))
418 (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q
))
419 (- w math-bignum-logb-digit-size
))
420 math-bignum-digit-power-of-two
422 (1- math-bignum-digit-power-of-two
))))))
424 (defun calcFunc-lsh (a &optional n w
) ; [I I] [Public]
425 (setq a
(math-trunc a
)
426 n
(if n
(math-trunc n
) 1))
427 (if (eq (car-safe a
) 'mod
)
428 (math-binary-modulo-args 'calcFunc-lsh a n w
)
429 (setq w
(if w
(math-trunc w
) calc-word-size
))
431 (math-reject-arg w
'fixnump
))
432 (or (Math-integerp a
)
433 (math-reject-arg a
'integerp
))
434 (or (Math-integerp n
)
435 (math-reject-arg n
'integerp
))
437 (math-clip (calcFunc-lsh a n
(- w
)) w
)
438 (if (Math-integer-negp a
)
439 (setq a
(math-clip a w
)))
440 (cond ((or (Math-lessp n
(- w
))
444 (math-quotient (math-clip a w
) (math-power-of-2 (- n
))))
446 (math-clip (math-mul a
(math-power-of-2 n
)) w
))))))
448 (defun calcFunc-rsh (a &optional n w
) ; [I I] [Public]
449 (calcFunc-lsh a
(math-neg (or n
1)) w
))
451 (defun calcFunc-ash (a &optional n w
) ; [I I] [Public]
455 (setq a
(math-trunc a
)
456 n
(if n
(math-trunc n
) 1))
457 (if (eq (car-safe a
) 'mod
)
458 (math-binary-modulo-args 'calcFunc-ash a n w
)
459 (setq w
(if w
(math-trunc w
) calc-word-size
))
461 (math-reject-arg w
'fixnump
))
462 (or (Math-integerp a
)
463 (math-reject-arg a
'integerp
))
464 (or (Math-integerp n
)
465 (math-reject-arg n
'integerp
))
467 (math-clip (calcFunc-ash a n
(- w
)) w
)
468 (if (Math-integer-negp a
)
469 (setq a
(math-clip a w
)))
470 (let ((two-to-sizem1 (math-power-of-2 (1- w
)))
471 (sh (calcFunc-lsh a n w
)))
472 (cond ((Math-natnum-lessp a two-to-sizem1
)
474 ((Math-lessp n
(- 1 w
))
475 (math-add (math-mul two-to-sizem1
2) -
1))
476 (t (let ((two-to-n (math-power-of-2 (- n
))))
477 (math-add (calcFunc-lsh (math-add two-to-n -
1)
481 (defun calcFunc-rash (a &optional n w
) ; [I I] [Public]
482 (calcFunc-ash a
(math-neg (or n
1)) w
))
484 (defun calcFunc-rot (a &optional n w
) ; [I I] [Public]
485 (setq a
(math-trunc a
)
486 n
(if n
(math-trunc n
) 1))
487 (if (eq (car-safe a
) 'mod
)
488 (math-binary-modulo-args 'calcFunc-rot a n w
)
489 (setq w
(if w
(math-trunc w
) calc-word-size
))
491 (math-reject-arg w
'fixnump
))
492 (or (Math-integerp a
)
493 (math-reject-arg a
'integerp
))
494 (or (Math-integerp n
)
495 (math-reject-arg n
'integerp
))
497 (math-clip (calcFunc-rot a n
(- w
)) w
)
498 (if (Math-integer-negp a
)
499 (setq a
(math-clip a w
)))
500 (cond ((or (Math-integer-negp n
)
501 (not (Math-natnum-lessp n w
)))
502 (calcFunc-rot a
(math-mod n w
) w
))
504 (math-add (calcFunc-lsh a
(- n w
) w
)
505 (calcFunc-lsh a n w
)))))))
507 (defun math-clip (a &optional w
) ; [I I] [Public]
508 (cond ((Math-messy-integerp w
)
509 (math-clip a
(math-trunc w
)))
510 ((eq (car-safe a
) 'mod
)
511 (math-binary-modulo-args 'math-clip a nil w
))
512 ((and w
(not (integerp w
)))
513 (math-reject-arg w
'fixnump
))
514 ((not (Math-num-integerp a
))
515 (math-reject-arg a
'integerp
))
516 ((< (or w
(setq w calc-word-size
)) 0)
517 (setq a
(math-clip a
(- w
)))
518 (if (Math-natnum-lessp a
(math-power-of-2 (- -
1 w
)))
520 (math-sub a
(math-power-of-2 (- w
)))))
522 (math-normalize (cons 'bigpos
(math-binary-arg a w
))))
523 ((and (integerp a
) (< a math-small-integer-size
))
524 (if (> w
(logb math-small-integer-size
))
526 (logand a
(1- (lsh 1 w
)))))
530 (math-clip-bignum (cdr (math-bignum-test (math-trunc a
)))
533 (defalias 'calcFunc-clip
'math-clip
)
535 (defun math-clip-bignum (a w
) ; [l l]
536 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two
)))
537 (if (<= w math-bignum-logb-digit-size
)
538 (list (logand (cdr q
)
540 (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q
))
541 (- w math-bignum-logb-digit-size
))
542 math-bignum-digit-power-of-two
545 (defvar math-max-digits-cache nil
)
546 (defun math-compute-max-digits (w r
)
547 (let* ((pair (+ (* r
100000) w
))
548 (res (assq pair math-max-digits-cache
)))
551 (let* ((calc-command-flags nil
)
552 (digs (math-ceiling (math-div w
(math-real-log2 r
)))))
553 (setq math-max-digits-cache
(cons (cons pair digs
)
554 math-max-digits-cache
))
557 (defvar math-log2-cache
(list '(2 .
1)
560 '(10 .
(float 332193 -
5))
563 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
564 (let ((res (assq x math-log2-cache
)))
567 (let* ((calc-symbolic-mode nil
)
568 (calc-display-working-message nil
)
569 (log (calcFunc-log x
2)))
570 (setq math-log2-cache
(cons (cons x log
) math-log2-cache
))
573 (defconst math-radix-digits
["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
574 "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
575 "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
576 "U" "V" "W" "X" "Y" "Z"])
578 (defsubst math-format-radix-digit
(a) ; [X D]
579 (aref math-radix-digits a
))
581 (defun math-format-radix (a) ; [X S]
582 (if (< a calc-number-radix
)
584 (concat "-" (math-format-radix (- a
)))
585 (math-format-radix-digit a
))
588 (setq s
(concat (math-format-radix-digit (% a calc-number-radix
)) s
)
589 a
(/ a calc-number-radix
)))
592 (defconst math-binary-digits
["000" "001" "010" "011"
593 "100" "101" "110" "111"])
594 (defun math-format-binary (a) ; [X S]
597 (concat "-" (math-format-binary (- a
)))
598 (math-format-radix a
))
601 (setq s
(concat (aref math-binary-digits
(% a
8)) s
)
603 (concat (math-format-radix a
) s
))))
605 (defun math-format-bignum-radix (a) ; [X L]
608 (< (car a
) calc-number-radix
))
609 (math-format-radix-digit (car a
)))
611 (let ((q (math-div-bignum-digit a calc-number-radix
)))
612 (concat (math-format-bignum-radix (math-norm-bignum (car q
)))
613 (math-format-radix-digit (cdr q
)))))))
615 (defun math-format-bignum-binary (a) ; [X L]
618 (math-format-binary (car a
)))
620 (let ((q (math-div-bignum-digit a
512)))
621 (concat (math-format-bignum-binary (math-norm-bignum (car q
)))
622 (aref math-binary-digits
(/ (cdr q
) 64))
623 (aref math-binary-digits
(%
(/ (cdr q
) 8) 8))
624 (aref math-binary-digits
(%
(cdr q
) 8)))))))
626 (defun math-format-bignum-octal (a) ; [X L]
629 (math-format-radix (car a
)))
631 (let ((q (math-div-bignum-digit a
512)))
632 (concat (math-format-bignum-octal (math-norm-bignum (car q
)))
633 (math-format-radix-digit (/ (cdr q
) 64))
634 (math-format-radix-digit (%
(/ (cdr q
) 8) 8))
635 (math-format-radix-digit (%
(cdr q
) 8)))))))
637 (defun math-format-bignum-hex (a) ; [X L]
640 (math-format-radix (car a
)))
642 (let ((q (math-div-bignum-digit a
256)))
643 (concat (math-format-bignum-hex (math-norm-bignum (car q
)))
644 (math-format-radix-digit (/ (cdr q
) 16))
645 (math-format-radix-digit (%
(cdr q
) 16)))))))
647 ;;; Decompose into integer and fractional parts, without depending
648 ;;; on calc-internal-prec.
649 (defun math-float-parts (a need-frac
) ; returns ( int frac fracdigs )
651 (list (math-scale-rounding (nth 1 a
) (nth 2 a
)) '(float 0 0) 0)
652 (let* ((d (math-numdigs (nth 1 a
)))
657 (let ((qr (math-idivmod (nth 1 a
) (math-scale-int 1 n
))))
658 (list (car qr
) (math-make-float (cdr qr
) (- n
)) n
)))
659 (list (math-scale-rounding (nth 1 a
) (nth 2 a
))
662 (defun math-format-radix-float (a prec
)
663 (let ((fmt (car calc-float-format
))
664 (figs (nth 1 calc-float-format
))
665 (point calc-point-char
)
669 (let* ((afigs (math-abs figs
))
670 (fp (math-float-parts a
(> afigs
0)))
671 (calc-internal-prec (+ 3 (max (nth 2 fp
)
672 (math-convert-radix-digits
675 (frac (math-round (math-mul (math-normalize (nth 1 fp
))
676 (math-radix-float-power afigs
)))))
677 (if (not (and (math-zerop frac
) (math-zerop int
) (< figs
0)))
678 (let ((math-radix-explicit-format nil
))
679 (let ((calc-group-digits nil
))
680 (setq str
(if (> afigs
0) (math-format-number frac
) ""))
681 (if (< (length str
) afigs
)
682 (setq str
(concat (make-string (- afigs
(length str
)) ?
0)
684 (if (> (length str
) afigs
)
685 (setq str
(substring str
1)
686 int
(math-add int
1))))
687 (setq str
(concat (math-format-number int
) point str
)))
688 (when calc-group-digits
689 (setq str
(math-group-float str
))))
692 (let* ((prec calc-internal-prec
)
693 (afigs (if (> figs
0)
696 (1- (math-convert-radix-digits
698 (math-numdigs (nth 1 a
)))))))))
699 (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t
)))
700 (explo -
1) (vlo (math-radix-float-power explo
))
701 (exphi 1) (vhi (math-radix-float-power exphi
))
703 (setq a
(math-normalize a
))
706 (if (math-lessp-float '(float 1 0) a
)
707 (while (not (math-lessp-float a vhi
))
708 (setq explo exphi vlo vhi
709 exphi
(math-mul exphi
2)
710 vhi
(math-radix-float-power exphi
)))
711 (while (math-lessp-float a vlo
)
712 (setq exphi explo vhi vlo
713 explo
(math-mul explo
2)
714 vlo
(math-radix-float-power explo
))))
715 (while (not (eq (math-sub exphi explo
) 1))
716 (setq expmid
(math-div2 (math-add explo exphi
))
717 vmid
(math-radix-float-power expmid
))
718 (if (math-lessp-float a vmid
)
719 (setq exphi expmid vhi vmid
)
720 (setq explo expmid vlo vmid
)))
721 (setq a
(math-div-float a vlo
)))
722 (let* ((sc (math-round (math-mul a
(math-radix-float-power
724 (math-radix-explicit-format nil
))
725 (let ((calc-group-digits nil
))
726 (setq str
(math-format-number sc
))))
727 (if (> (length str
) afigs
)
728 (setq str
(substring str
0 -
1)
730 (if (and (eq fmt
'float
)
731 (math-lessp explo
(+ (if (= figs
0)
732 (1- (math-convert-radix-digits
735 calc-display-sci-high
1))
736 (math-lessp calc-display-sci-low explo
))
737 (let ((dpos (1+ explo
)))
739 (setq str
(concat "0" point
(make-string (- dpos
) ?
0)
741 ((> dpos
(length str
))
742 (setq str
(concat str
(make-string (- dpos
(length str
))
745 (setq str
(concat (substring str
0 dpos
) point
746 (substring str dpos
)))))
748 (setq eadj
(if (eq fmt
'eng
)
749 (min (math-mod explo
3) (length str
))
751 str
(concat (substring str
0 (1+ eadj
)) point
752 (substring str
(1+ eadj
)))))
753 (setq pos
(length str
))
754 (while (eq (aref str
(1- pos
)) ?
0) (setq pos
(1- pos
)))
755 (and explo
(eq (aref str
(1- pos
)) ?.
) (setq pos
(1- pos
)))
756 (setq str
(substring str
0 pos
))
757 (when calc-group-digits
758 (setq str
(math-group-float str
)))
760 (let ((estr (let ((calc-number-radix 10)
761 (calc-group-digits nil
))
763 (math-sub explo eadj
)))))
764 (setq str
(if (or (memq calc-language
'(math maple
))
765 (> calc-number-radix
14))
766 (format "%s*%d.^%s" str calc-number-radix estr
)
767 (format "%se%s" str estr
)))))))
770 (defvar math-radix-digits-cache nil
)
772 (defun math-convert-radix-digits (n &optional to-dec
)
773 (let ((key (cons n
(cons to-dec calc-number-radix
))))
774 (or (cdr (assoc key math-radix-digits-cache
))
775 (let* ((calc-internal-prec 6)
776 (log (math-div (math-real-log2 calc-number-radix
)
777 '(float 332193 -
5))))
778 (cdr (car (setq math-radix-digits-cache
779 (cons (cons key
(math-ceiling (if to-dec
782 math-radix-digits-cache
))))))))
784 (defvar math-radix-float-cache-tag nil
)
785 (defvar math-radix-float-cache
)
787 (defun math-radix-float-power (n)
790 (or (and (eq calc-number-radix
(car math-radix-float-cache-tag
))
791 (<= calc-internal-prec
(cdr math-radix-float-cache-tag
)))
792 (setq math-radix-float-cache-tag
(cons calc-number-radix
794 math-radix-float-cache nil
))
796 (or (cdr (assoc n math-radix-float-cache
))
797 (cdr (car (setq math-radix-float-cache
800 (let ((calc-internal-prec
801 (cdr math-radix-float-cache-tag
)))
803 (math-div-float '(float 1 0)
804 (math-radix-float-power
806 (math-mul-float (math-sqr-float
807 (math-radix-float-power
812 calc-number-radix
))))))
813 math-radix-float-cache
))))))))
817 ;; arch-tag: f6dba7bc-53b2-41ae-919c-c266ab0ca8b3
818 ;;; calc-bin.el ends here