1 ;;; calc-bin.el --- binary functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-Need-calc-bin () nil
)
38 ;;; b-prefix binary commands.
43 (calc-enter-result 2 "and"
44 (append '(calcFunc-and)
46 (and n
(list (prefix-numeric-value n
)))))))
51 (calc-enter-result 2 "or"
52 (append '(calcFunc-or)
54 (and n
(list (prefix-numeric-value n
)))))))
59 (calc-enter-result 2 "xor"
60 (append '(calcFunc-xor)
62 (and n
(list (prefix-numeric-value n
)))))))
67 (calc-enter-result 2 "diff"
68 (append '(calcFunc-diff)
70 (and n
(list (prefix-numeric-value n
)))))))
75 (calc-enter-result 1 "not"
76 (append '(calcFunc-not)
78 (and n
(list (prefix-numeric-value n
)))))))
80 (defun calc-lshift-binary (n)
83 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
84 (calc-enter-result hyp
"lsh"
85 (append '(calcFunc-lsh)
87 (and n
(list (prefix-numeric-value n
))))))))
89 (defun calc-rshift-binary (n)
92 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
93 (calc-enter-result hyp
"rsh"
94 (append '(calcFunc-rsh)
96 (and n
(list (prefix-numeric-value n
))))))))
98 (defun calc-lshift-arith (n)
101 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
102 (calc-enter-result hyp
"ash"
103 (append '(calcFunc-ash)
104 (calc-top-list-n hyp
)
105 (and n
(list (prefix-numeric-value n
))))))))
107 (defun calc-rshift-arith (n)
110 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
111 (calc-enter-result hyp
"rash"
112 (append '(calcFunc-rash)
113 (calc-top-list-n hyp
)
114 (and n
(list (prefix-numeric-value n
))))))))
116 (defun calc-rotate-binary (n)
119 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
120 (calc-enter-result hyp
"rot"
121 (append '(calcFunc-rot)
122 (calc-top-list-n hyp
)
123 (and n
(list (prefix-numeric-value n
))))))))
128 (calc-enter-result 1 "clip"
129 (append '(calcFunc-clip)
131 (and n
(list (prefix-numeric-value n
)))))))
133 (defun calc-word-size (n)
136 (or n
(setq n
(read-string (format "Binary word size: (default %d) "
138 (setq n
(if (stringp n
)
141 (if (string-match "\\`[-+]?[0-9]+\\'" n
)
143 (error "Expected an integer")))
144 (prefix-numeric-value n
)))
145 (or (= n calc-word-size
)
146 (if (> (math-abs n
) 100)
147 (calc-change-mode 'calc-word-size n calc-leading-zeros
)
148 (calc-change-mode '(calc-word-size calc-previous-modulo
)
149 (list n
(math-power-of-2 (math-abs n
)))
150 calc-leading-zeros
)))
152 (message "Binary word size is %d bits (2's complement)" (- n
))
153 (message "Binary word size is %d bits" n
))))
159 ;;; d-prefix mode commands.
161 (defun calc-radix (n)
162 (interactive "NDisplay radix (2-36): ")
164 (if (and (>= n
2) (<= n
36))
166 (calc-change-mode 'calc-number-radix n t
)
167 ;; also change global value so minibuffer sees it
168 (setq-default calc-number-radix calc-number-radix
))
169 (setq n calc-number-radix
))
170 (message "Number radix is %d" n
)))
172 (defun calc-decimal-radix ()
176 (defun calc-binary-radix ()
180 (defun calc-octal-radix ()
184 (defun calc-hex-radix ()
188 (defun calc-leading-zeros (n)
191 (if (calc-change-mode 'calc-leading-zeros n t t
)
192 (message "Zero-padding integers to %d digits (assuming radix %d)"
193 (let* ((calc-internal-prec 6))
194 (math-compute-max-digits (math-abs calc-word-size
)
197 (message "Omitting leading zeros on integers"))))
200 (defvar math-power-of-2-cache
(list 1 2 4 8 16 32 64 128 256 512 1024))
201 (defvar math-big-power-of-2-cache nil
)
202 (defun math-power-of-2 (n) ; [I I] [Public]
203 (if (and (natnump n
) (<= n
100))
204 (or (nth n math-power-of-2-cache
)
205 (let* ((i (length math-power-of-2-cache
))
206 (val (nth (1- i
) math-power-of-2-cache
)))
208 (setq val
(math-mul val
2)
209 math-power-of-2-cache
(nconc math-power-of-2-cache
213 (let ((found (assq n math-big-power-of-2-cache
)))
216 (let ((po2 (math-ipow 2 n
)))
217 (setq math-big-power-of-2-cache
218 (cons (cons n po2
) math-big-power-of-2-cache
))
221 (defun math-integer-log2 (n) ; [I I] [Public]
223 (p math-power-of-2-cache
)
225 (while (and p
(Math-natnum-lessp (setq val
(car p
)) n
))
231 (while (Math-natnum-lessp
233 (setq val
(math-mul val
2))
234 (setq math-power-of-2-cache
(nconc math-power-of-2-cache
244 ;;; Bitwise operations.
246 (defun calcFunc-and (a b
&optional w
) ; [I I I] [Public]
247 (cond ((Math-messy-integerp w
)
248 (calcFunc-and a b
(math-trunc w
)))
249 ((and w
(not (integerp w
)))
250 (math-reject-arg w
'fixnump
))
251 ((and (integerp a
) (integerp b
))
252 (math-clip (logand a b
) w
))
253 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
254 (math-binary-modulo-args 'calcFunc-and a b w
))
255 ((not (Math-num-integerp a
))
256 (math-reject-arg a
'integerp
))
257 ((not (Math-num-integerp b
))
258 (math-reject-arg b
'integerp
))
259 (t (math-clip (cons 'bigpos
260 (math-and-bignum (math-binary-arg a w
)
261 (math-binary-arg b w
)))
264 (defun math-binary-arg (a w
)
265 (if (not (Math-integerp a
))
266 (setq a
(math-trunc a
)))
267 (if (Math-integer-negp a
)
268 (math-not-bignum (cdr (math-bignum-test (math-sub -
1 a
)))
269 (math-abs (if w
(math-trunc w
) calc-word-size
)))
270 (cdr (Math-bignum-test a
))))
272 (defun math-binary-modulo-args (f a b w
)
274 (if (eq (car-safe a
) 'mod
)
278 (if (eq (car-safe b
) 'mod
)
279 (if (equal mod
(nth 2 b
))
281 (math-reject-arg b
"*Inconsistent modulos"))))
284 (if (Math-messy-integerp mod
)
285 (setq mod
(math-trunc mod
))
286 (or (Math-integerp mod
)
287 (math-reject-arg mod
'integerp
)))
288 (let ((bits (math-integer-log2 mod
)))
293 "*Warning: Modulo inconsistent with word size"))
295 (calc-record-why "*Warning: Modulo is not a power of 2"))
301 (defun math-and-bignum (a b
) ; [l l l]
303 (let ((qa (math-div-bignum-digit a
512))
304 (qb (math-div-bignum-digit b
512)))
305 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa
))
306 (math-norm-bignum (car qb
)))
308 (logand (cdr qa
) (cdr qb
))))))
310 (defun calcFunc-or (a b
&optional w
) ; [I I I] [Public]
311 (cond ((Math-messy-integerp w
)
312 (calcFunc-or a b
(math-trunc w
)))
313 ((and w
(not (integerp w
)))
314 (math-reject-arg w
'fixnump
))
315 ((and (integerp a
) (integerp b
))
316 (math-clip (logior a b
) w
))
317 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
318 (math-binary-modulo-args 'calcFunc-or a b w
))
319 ((not (Math-num-integerp a
))
320 (math-reject-arg a
'integerp
))
321 ((not (Math-num-integerp b
))
322 (math-reject-arg b
'integerp
))
323 (t (math-clip (cons 'bigpos
324 (math-or-bignum (math-binary-arg a w
)
325 (math-binary-arg b w
)))
328 (defun math-or-bignum (a b
) ; [l l l]
330 (let ((qa (math-div-bignum-digit a
512))
331 (qb (math-div-bignum-digit b
512)))
332 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa
))
333 (math-norm-bignum (car qb
)))
335 (logior (cdr qa
) (cdr qb
))))))
337 (defun calcFunc-xor (a b
&optional w
) ; [I I I] [Public]
338 (cond ((Math-messy-integerp w
)
339 (calcFunc-xor a b
(math-trunc w
)))
340 ((and w
(not (integerp w
)))
341 (math-reject-arg w
'fixnump
))
342 ((and (integerp a
) (integerp b
))
343 (math-clip (logxor a b
) w
))
344 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
345 (math-binary-modulo-args 'calcFunc-xor a b w
))
346 ((not (Math-num-integerp a
))
347 (math-reject-arg a
'integerp
))
348 ((not (Math-num-integerp b
))
349 (math-reject-arg b
'integerp
))
350 (t (math-clip (cons 'bigpos
351 (math-xor-bignum (math-binary-arg a w
)
352 (math-binary-arg b w
)))
355 (defun math-xor-bignum (a b
) ; [l l l]
357 (let ((qa (math-div-bignum-digit a
512))
358 (qb (math-div-bignum-digit b
512)))
359 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa
))
360 (math-norm-bignum (car qb
)))
362 (logxor (cdr qa
) (cdr qb
))))))
364 (defun calcFunc-diff (a b
&optional w
) ; [I I I] [Public]
365 (cond ((Math-messy-integerp w
)
366 (calcFunc-diff a b
(math-trunc w
)))
367 ((and w
(not (integerp w
)))
368 (math-reject-arg w
'fixnump
))
369 ((and (integerp a
) (integerp b
))
370 (math-clip (logand a
(lognot b
)) w
))
371 ((or (eq (car-safe a
) 'mod
) (eq (car-safe b
) 'mod
))
372 (math-binary-modulo-args 'calcFunc-diff a b w
))
373 ((not (Math-num-integerp a
))
374 (math-reject-arg a
'integerp
))
375 ((not (Math-num-integerp b
))
376 (math-reject-arg b
'integerp
))
377 (t (math-clip (cons 'bigpos
378 (math-diff-bignum (math-binary-arg a w
)
379 (math-binary-arg b w
)))
382 (defun math-diff-bignum (a b
) ; [l l l]
384 (let ((qa (math-div-bignum-digit a
512))
385 (qb (math-div-bignum-digit b
512)))
386 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa
))
387 (math-norm-bignum (car qb
)))
389 (logand (cdr qa
) (lognot (cdr qb
)))))))
391 (defun calcFunc-not (a &optional w
) ; [I I] [Public]
392 (cond ((Math-messy-integerp w
)
393 (calcFunc-not a
(math-trunc w
)))
394 ((eq (car-safe a
) 'mod
)
395 (math-binary-modulo-args 'calcFunc-not a nil w
))
396 ((and w
(not (integerp w
)))
397 (math-reject-arg w
'fixnump
))
398 ((not (Math-num-integerp a
))
399 (math-reject-arg a
'integerp
))
400 ((< (or w
(setq w calc-word-size
)) 0)
401 (math-clip (calcFunc-not a
(- w
)) w
))
404 (math-not-bignum (math-binary-arg a w
)
407 (defun math-not-bignum (a w
) ; [l l]
408 (let ((q (math-div-bignum-digit a
512)))
410 (list (logand (lognot (cdr q
))
412 (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q
))
415 (logxor (cdr q
) 511)))))
417 (defun calcFunc-lsh (a &optional n w
) ; [I I] [Public]
418 (setq a
(math-trunc a
)
419 n
(if n
(math-trunc n
) 1))
420 (if (eq (car-safe a
) 'mod
)
421 (math-binary-modulo-args 'calcFunc-lsh a n w
)
422 (setq w
(if w
(math-trunc w
) calc-word-size
))
424 (math-reject-arg w
'fixnump
))
425 (or (Math-integerp a
)
426 (math-reject-arg a
'integerp
))
427 (or (Math-integerp n
)
428 (math-reject-arg n
'integerp
))
430 (math-clip (calcFunc-lsh a n
(- w
)) w
)
431 (if (Math-integer-negp a
)
432 (setq a
(math-clip a w
)))
433 (cond ((or (Math-lessp n
(- w
))
437 (math-quotient (math-clip a w
) (math-power-of-2 (- n
))))
439 (math-clip (math-mul a
(math-power-of-2 n
)) w
))))))
441 (defun calcFunc-rsh (a &optional n w
) ; [I I] [Public]
442 (calcFunc-lsh a
(math-neg (or n
1)) w
))
444 (defun calcFunc-ash (a &optional n w
) ; [I I] [Public]
448 (setq a
(math-trunc a
)
449 n
(if n
(math-trunc n
) 1))
450 (if (eq (car-safe a
) 'mod
)
451 (math-binary-modulo-args 'calcFunc-ash a n w
)
452 (setq w
(if w
(math-trunc w
) calc-word-size
))
454 (math-reject-arg w
'fixnump
))
455 (or (Math-integerp a
)
456 (math-reject-arg a
'integerp
))
457 (or (Math-integerp n
)
458 (math-reject-arg n
'integerp
))
460 (math-clip (calcFunc-ash a n
(- w
)) w
)
461 (if (Math-integer-negp a
)
462 (setq a
(math-clip a w
)))
463 (let ((two-to-sizem1 (math-power-of-2 (1- w
)))
464 (sh (calcFunc-lsh a n w
)))
465 (cond ((Math-natnum-lessp a two-to-sizem1
)
467 ((Math-lessp n
(- 1 w
))
468 (math-add (math-mul two-to-sizem1
2) -
1))
469 (t (let ((two-to-n (math-power-of-2 (- n
))))
470 (math-add (calcFunc-lsh (math-add two-to-n -
1)
474 (defun calcFunc-rash (a &optional n w
) ; [I I] [Public]
475 (calcFunc-ash a
(math-neg (or n
1)) w
))
477 (defun calcFunc-rot (a &optional n w
) ; [I I] [Public]
478 (setq a
(math-trunc a
)
479 n
(if n
(math-trunc n
) 1))
480 (if (eq (car-safe a
) 'mod
)
481 (math-binary-modulo-args 'calcFunc-rot a n w
)
482 (setq w
(if w
(math-trunc w
) calc-word-size
))
484 (math-reject-arg w
'fixnump
))
485 (or (Math-integerp a
)
486 (math-reject-arg a
'integerp
))
487 (or (Math-integerp n
)
488 (math-reject-arg n
'integerp
))
490 (math-clip (calcFunc-rot a n
(- w
)) w
)
491 (if (Math-integer-negp a
)
492 (setq a
(math-clip a w
)))
493 (cond ((or (Math-integer-negp n
)
494 (not (Math-natnum-lessp n w
)))
495 (calcFunc-rot a
(math-mod n w
) w
))
497 (math-add (calcFunc-lsh a
(- n w
) w
)
498 (calcFunc-lsh a n w
)))))))
500 (defun math-clip (a &optional w
) ; [I I] [Public]
501 (cond ((Math-messy-integerp w
)
502 (math-clip a
(math-trunc w
)))
503 ((eq (car-safe a
) 'mod
)
504 (math-binary-modulo-args 'math-clip a nil w
))
505 ((and w
(not (integerp w
)))
506 (math-reject-arg w
'fixnump
))
507 ((not (Math-num-integerp a
))
508 (math-reject-arg a
'integerp
))
509 ((< (or w
(setq w calc-word-size
)) 0)
510 (setq a
(math-clip a
(- w
)))
511 (if (Math-natnum-lessp a
(math-power-of-2 (- -
1 w
)))
513 (math-sub a
(math-power-of-2 (- w
)))))
515 (math-normalize (cons 'bigpos
(math-binary-arg a w
))))
516 ((and (integerp a
) (< a
1000000))
519 (logand a
(1- (lsh 1 w
)))))
523 (math-clip-bignum (cdr (math-bignum-test (math-trunc a
)))
526 (defalias 'calcFunc-clip
'math-clip
)
528 (defun math-clip-bignum (a w
) ; [l l]
529 (let ((q (math-div-bignum-digit a
512)))
531 (list (logand (cdr q
)
533 (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q
))
538 (defvar math-max-digits-cache nil
)
539 (defun math-compute-max-digits (w r
)
540 (let* ((pair (+ (* r
100000) w
))
541 (res (assq pair math-max-digits-cache
)))
544 (let* ((calc-command-flags nil
)
545 (digs (math-ceiling (math-div w
(math-real-log2 r
)))))
546 (setq math-max-digits-cache
(cons (cons pair digs
)
547 math-max-digits-cache
))
550 (defvar math-log2-cache
(list '(2 .
1)
553 '(10 .
(float 332193 -
5))
556 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
557 (let ((res (assq x math-log2-cache
)))
560 (let* ((calc-symbolic-mode nil
)
561 (calc-display-working-message nil
)
562 (log (calcFunc-log x
2)))
563 (setq math-log2-cache
(cons (cons x log
) math-log2-cache
))
566 (defconst math-radix-digits
["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
567 "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
568 "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T"
569 "U" "V" "W" "X" "Y" "Z"])
571 (defsubst math-format-radix-digit
(a) ; [X D]
572 (aref math-radix-digits a
))
574 (defun math-format-radix (a) ; [X S]
575 (if (< a calc-number-radix
)
577 (concat "-" (math-format-radix (- a
)))
578 (math-format-radix-digit a
))
581 (setq s
(concat (math-format-radix-digit (% a calc-number-radix
)) s
)
582 a
(/ a calc-number-radix
)))
585 (defconst math-binary-digits
["000" "001" "010" "011"
586 "100" "101" "110" "111"])
587 (defun math-format-binary (a) ; [X S]
590 (concat "-" (math-format-binary (- a
)))
591 (math-format-radix a
))
594 (setq s
(concat (aref math-binary-digits
(% a
8)) s
)
596 (concat (math-format-radix a
) s
))))
598 (defun math-format-bignum-radix (a) ; [X L]
601 (< (car a
) calc-number-radix
))
602 (math-format-radix-digit (car a
)))
604 (let ((q (math-div-bignum-digit a calc-number-radix
)))
605 (concat (math-format-bignum-radix (math-norm-bignum (car q
)))
606 (math-format-radix-digit (cdr q
)))))))
608 (defun math-format-bignum-binary (a) ; [X L]
611 (math-format-binary (car a
)))
613 (let ((q (math-div-bignum-digit a
512)))
614 (concat (math-format-bignum-binary (math-norm-bignum (car q
)))
615 (aref math-binary-digits
(/ (cdr q
) 64))
616 (aref math-binary-digits
(%
(/ (cdr q
) 8) 8))
617 (aref math-binary-digits
(%
(cdr q
) 8)))))))
619 (defun math-format-bignum-octal (a) ; [X L]
622 (math-format-radix (car a
)))
624 (let ((q (math-div-bignum-digit a
512)))
625 (concat (math-format-bignum-octal (math-norm-bignum (car q
)))
626 (math-format-radix-digit (/ (cdr q
) 64))
627 (math-format-radix-digit (%
(/ (cdr q
) 8) 8))
628 (math-format-radix-digit (%
(cdr q
) 8)))))))
630 (defun math-format-bignum-hex (a) ; [X L]
633 (math-format-radix (car a
)))
635 (let ((q (math-div-bignum-digit a
256)))
636 (concat (math-format-bignum-hex (math-norm-bignum (car q
)))
637 (math-format-radix-digit (/ (cdr q
) 16))
638 (math-format-radix-digit (%
(cdr q
) 16)))))))
640 ;;; Decompose into integer and fractional parts, without depending
641 ;;; on calc-internal-prec.
642 (defun math-float-parts (a need-frac
) ; returns ( int frac fracdigs )
644 (list (math-scale-rounding (nth 1 a
) (nth 2 a
)) '(float 0 0) 0)
645 (let* ((d (math-numdigs (nth 1 a
)))
650 (let ((qr (math-idivmod (nth 1 a
) (math-scale-int 1 n
))))
651 (list (car qr
) (math-make-float (cdr qr
) (- n
)) n
)))
652 (list (math-scale-rounding (nth 1 a
) (nth 2 a
))
655 (defun math-format-radix-float (a prec
)
656 (let ((fmt (car calc-float-format
))
657 (figs (nth 1 calc-float-format
))
658 (point calc-point-char
)
661 (let* ((afigs (math-abs figs
))
662 (fp (math-float-parts a
(> afigs
0)))
663 (calc-internal-prec (+ 3 (max (nth 2 fp
)
664 (math-convert-radix-digits
667 (frac (math-round (math-mul (math-normalize (nth 1 fp
))
668 (math-radix-float-power afigs
)))))
669 (if (not (and (math-zerop frac
) (math-zerop int
) (< figs
0)))
670 (let ((math-radix-explicit-format nil
))
671 (let ((calc-group-digits nil
))
672 (setq str
(if (> afigs
0) (math-format-number frac
) ""))
673 (if (< (length str
) afigs
)
674 (setq str
(concat (make-string (- afigs
(length str
)) ?
0)
676 (if (> (length str
) afigs
)
677 (setq str
(substring str
1)
678 int
(math-add int
1))))
679 (setq str
(concat (math-format-number int
) point str
)))
680 (when calc-group-digits
681 (setq str
(math-group-float str
))))
684 (let* ((prec calc-internal-prec
)
685 (afigs (if (> figs
0)
688 (1- (math-convert-radix-digits
690 (math-numdigs (nth 1 a
)))))))))
691 (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t
)))
692 (explo -
1) (vlo (math-radix-float-power explo
))
693 (exphi 1) (vhi (math-radix-float-power exphi
))
695 (setq a
(math-normalize a
))
698 (if (math-lessp-float '(float 1 0) a
)
699 (while (not (math-lessp-float a vhi
))
700 (setq explo exphi vlo vhi
701 exphi
(math-mul exphi
2)
702 vhi
(math-radix-float-power exphi
)))
703 (while (math-lessp-float a vlo
)
704 (setq exphi explo vhi vlo
705 explo
(math-mul explo
2)
706 vlo
(math-radix-float-power explo
))))
707 (while (not (eq (math-sub exphi explo
) 1))
708 (setq expmid
(math-div2 (math-add explo exphi
))
709 vmid
(math-radix-float-power expmid
))
710 (if (math-lessp-float a vmid
)
711 (setq exphi expmid vhi vmid
)
712 (setq explo expmid vlo vmid
)))
713 (setq a
(math-div-float a vlo
)))
714 (let* ((sc (math-round (math-mul a
(math-radix-float-power
716 (math-radix-explicit-format nil
))
717 (let ((calc-group-digits nil
))
718 (setq str
(math-format-number sc
))))
719 (if (> (length str
) afigs
)
720 (setq str
(substring str
0 -
1)
722 (if (and (eq fmt
'float
)
723 (math-lessp explo
(+ (if (= figs
0)
724 (1- (math-convert-radix-digits
727 calc-display-sci-high
1))
728 (math-lessp calc-display-sci-low explo
))
729 (let ((dpos (1+ explo
)))
731 (setq str
(concat "0" point
(make-string (- dpos
) ?
0)
733 ((> dpos
(length str
))
734 (setq str
(concat str
(make-string (- dpos
(length str
))
737 (setq str
(concat (substring str
0 dpos
) point
738 (substring str dpos
)))))
740 (setq eadj
(if (eq fmt
'eng
)
741 (min (math-mod explo
3) (length str
))
743 str
(concat (substring str
0 (1+ eadj
)) point
744 (substring str
(1+ eadj
)))))
745 (setq pos
(length str
))
746 (while (eq (aref str
(1- pos
)) ?
0) (setq pos
(1- pos
)))
747 (and explo
(eq (aref str
(1- pos
)) ?.
) (setq pos
(1- pos
)))
748 (setq str
(substring str
0 pos
))
749 (when calc-group-digits
750 (setq str
(math-group-float str
)))
752 (let ((estr (let ((calc-number-radix 10)
753 (calc-group-digits nil
))
754 (setq estr
(math-format-number
755 (math-sub explo eadj
))))))
756 (setq str
(if (or (memq calc-language
'(math maple
))
757 (> calc-number-radix
14))
758 (format "%s*%d.^%s" str calc-number-radix estr
)
759 (format "%se%s" str estr
)))))))
762 (defvar math-radix-digits-cache nil
)
764 (defun math-convert-radix-digits (n &optional to-dec
)
765 (let ((key (cons n
(cons to-dec calc-number-radix
))))
766 (or (cdr (assoc key math-radix-digits-cache
))
767 (let* ((calc-internal-prec 6)
768 (log (math-div (math-real-log2 calc-number-radix
)
769 '(float 332193 -
5))))
770 (cdr (car (setq math-radix-digits-cache
771 (cons (cons key
(math-ceiling (if to-dec
774 math-radix-digits-cache
))))))))
776 (defvar math-radix-float-cache-tag nil
)
778 (defun math-radix-float-power (n)
781 (or (and (eq calc-number-radix
(car math-radix-float-cache-tag
))
782 (<= calc-internal-prec
(cdr math-radix-float-cache-tag
)))
783 (setq math-radix-float-cache-tag
(cons calc-number-radix
785 math-radix-float-cache nil
))
787 (or (cdr (assoc n math-radix-float-cache
))
788 (cdr (car (setq math-radix-float-cache
791 (let ((calc-internal-prec
792 (cdr math-radix-float-cache-tag
)))
794 (math-div-float '(float 1 0)
795 (math-radix-float-power
797 (math-mul-float (math-sqr-float
798 (math-radix-float-power
803 calc-number-radix
))))))
804 math-radix-float-cache
))))))))
807 ;;; arch-tag: f6dba7bc-53b2-41ae-919c-c266ab0ca8b3
808 ;;; calc-bin.el ends here