]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-bin.el
Update copyright year to 2016
[gnu-emacs] / lisp / calc / calc-bin.el
1 ;;; calc-bin.el --- binary functions for Calc
2
3 ;; Copyright (C) 1990-1993, 2001-2016 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 ;; This file is autoloaded from calc-ext.el.
27
28 (require 'calc-ext)
29 (require 'calc-macs)
30
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.")
37
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.")
41
42 ;;; b-prefix binary commands.
43
44 (defun calc-and (n)
45 (interactive "P")
46 (calc-slow-wrapper
47 (calc-enter-result 2 "and"
48 (append '(calcFunc-and)
49 (calc-top-list-n 2)
50 (and n (list (prefix-numeric-value n)))))))
51
52 (defun calc-or (n)
53 (interactive "P")
54 (calc-slow-wrapper
55 (calc-enter-result 2 "or"
56 (append '(calcFunc-or)
57 (calc-top-list-n 2)
58 (and n (list (prefix-numeric-value n)))))))
59
60 (defun calc-xor (n)
61 (interactive "P")
62 (calc-slow-wrapper
63 (calc-enter-result 2 "xor"
64 (append '(calcFunc-xor)
65 (calc-top-list-n 2)
66 (and n (list (prefix-numeric-value n)))))))
67
68 (defun calc-diff (n)
69 (interactive "P")
70 (calc-slow-wrapper
71 (calc-enter-result 2 "diff"
72 (append '(calcFunc-diff)
73 (calc-top-list-n 2)
74 (and n (list (prefix-numeric-value n)))))))
75
76 (defun calc-not (n)
77 (interactive "P")
78 (calc-slow-wrapper
79 (calc-enter-result 1 "not"
80 (append '(calcFunc-not)
81 (calc-top-list-n 1)
82 (and n (list (prefix-numeric-value n)))))))
83
84 (defun calc-lshift-binary (n)
85 (interactive "P")
86 (calc-slow-wrapper
87 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
88 (calc-enter-result hyp "lsh"
89 (append '(calcFunc-lsh)
90 (calc-top-list-n hyp)
91 (and n (list (prefix-numeric-value n))))))))
92
93 (defun calc-rshift-binary (n)
94 (interactive "P")
95 (calc-slow-wrapper
96 (let ((hyp (if (calc-is-hyperbolic) 2 1)))
97 (calc-enter-result hyp "rsh"
98 (append '(calcFunc-rsh)
99 (calc-top-list-n hyp)
100 (and n (list (prefix-numeric-value n))))))))
101
102 (defun calc-lshift-arith (n)
103 (interactive "P")
104 (calc-slow-wrapper
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))))))))
110
111 (defun calc-rshift-arith (n)
112 (interactive "P")
113 (calc-slow-wrapper
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))))))))
119
120 (defun calc-rotate-binary (n)
121 (interactive "P")
122 (calc-slow-wrapper
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))))))))
128
129 (defun calc-clip (n)
130 (interactive "P")
131 (calc-slow-wrapper
132 (calc-enter-result 1 "clip"
133 (append '(calcFunc-clip)
134 (calc-top-list-n 1)
135 (and n (list (prefix-numeric-value n)))))))
136
137 (defun calc-word-size (n)
138 (interactive "P")
139 (calc-wrapper
140 (or n (setq n (read-string (format "Binary word size: (default %d) "
141 calc-word-size))))
142 (setq n (if (stringp n)
143 (if (equal n "")
144 calc-word-size
145 (if (string-match "\\`[-+]?[0-9]+\\'" n)
146 (string-to-number 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))))
157 (calc-do-refresh)
158 (calc-refresh-evaltos)
159 (if (< n 0)
160 (message "Binary word size is %d bits (two's complement)" (- n))
161 (message "Binary word size is %d bits" n))))
162
163
164
165
166
167 ;;; d-prefix mode commands.
168
169 (defun calc-radix (n &optional arg)
170 (interactive "NDisplay radix (2-36): ")
171 (calc-wrapper
172 (if (and (>= n 2) (<= n 36))
173 (progn
174 (calc-change-mode
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))))
183
184 (defun calc-decimal-radix ()
185 (interactive)
186 (calc-radix 10))
187
188 (defun calc-binary-radix (&optional arg)
189 (interactive "P")
190 (calc-radix 2 arg))
191
192 (defun calc-octal-radix (&optional arg)
193 (interactive "P")
194 (calc-radix 8 arg))
195
196 (defun calc-hex-radix (&optional arg)
197 (interactive "P")
198 (calc-radix 16 arg))
199
200 (defun calc-leading-zeros (n)
201 (interactive "P")
202 (calc-wrapper
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)
207 calc-number-radix))
208 calc-number-radix)
209 (message "Omitting leading zeros on integers"))))
210
211
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)))
219 (while (<= i n)
220 (setq val (math-mul val 2)
221 math-power-of-2-cache (nconc math-power-of-2-cache
222 (list val))
223 i (1+ i)))
224 val))
225 (let ((found (assq n math-big-power-of-2-cache)))
226 (if found
227 (cdr found)
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))
231 po2)))))
232
233 (defun math-integer-log2 (n) ; [I I] [Public]
234 (let ((i 0)
235 (p math-power-of-2-cache)
236 val)
237 (while (and p (Math-natnum-lessp (setq val (car p)) n))
238 (setq p (cdr p)
239 i (1+ i)))
240 (if p
241 (and (equal val n)
242 i)
243 (while (Math-natnum-lessp
244 (prog1
245 (setq val (math-mul val 2))
246 (setq math-power-of-2-cache (nconc math-power-of-2-cache
247 (list val))))
248 n)
249 (setq i (1+ i)))
250 (and (equal val n)
251 i))))
252
253
254
255
256 ;;; Bitwise operations.
257
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)))
274 w))))
275
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))))
283
284 (defun math-binary-modulo-args (f a b w)
285 (let (mod)
286 (if (eq (car-safe a) 'mod)
287 (progn
288 (setq mod (nth 2 a)
289 a (nth 1 a))
290 (if (eq (car-safe b) 'mod)
291 (if (equal mod (nth 2 b))
292 (setq b (nth 1 b))
293 (math-reject-arg b "*Inconsistent modulus"))))
294 (setq mod (nth 2 b)
295 b (nth 1 b)))
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)))
301 (if bits
302 (if w
303 (if (/= w bits)
304 (calc-record-why
305 "*Warning: Modulus inconsistent with word size"))
306 (setq w bits))
307 (calc-record-why "*Warning: Modulus is not a power of 2"))
308 (math-make-mod (if b
309 (funcall f a b w)
310 (funcall f a w))
311 mod))))
312
313 (defun math-and-bignum (a b) ; [l l l]
314 (and a b
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))))))
321
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)))
338 w))))
339
340 (defun math-or-bignum (a b) ; [l l l]
341 (and (or a b)
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))))))
348
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)))
365 w))))
366
367 (defun math-xor-bignum (a b) ; [l l l]
368 (and (or a b)
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))))))
375
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)))
392 w))))
393
394 (defun math-diff-bignum (a b) ; [l l l]
395 (and a
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)))))))
402
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))
414 (t (math-normalize
415 (cons 'bigpos
416 (math-not-bignum (math-binary-arg a w)
417 w))))))
418
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))
423 (1- (lsh 1 w))))
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
427 (logxor (cdr q)
428 (1- math-bignum-digit-power-of-two))))))
429
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))
436 (or (integerp w)
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))
442 (if (< w 0)
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))
447 (Math-lessp w n))
448 0)
449 ((< n 0)
450 (math-quotient (math-clip a w) (math-power-of-2 (- n))))
451 (t
452 (math-clip (math-mul a (math-power-of-2 n)) w))))))
453
454 (defun calcFunc-rsh (a &optional n w) ; [I I] [Public]
455 (calcFunc-lsh a (math-neg (or n 1)) w))
456
457 (defun calcFunc-ash (a &optional n w) ; [I I] [Public]
458 (if (or (null n)
459 (not (Math-negp n)))
460 (calcFunc-lsh a n w)
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))
466 (or (integerp w)
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))
472 (if (< w 0)
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)
479 sh)
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)
484 (+ w n) w)
485 sh)))))))))
486
487 (defun calcFunc-rash (a &optional n w) ; [I I] [Public]
488 (calcFunc-ash a (math-neg (or n 1)) w))
489
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))
496 (or (integerp w)
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))
502 (if (< w 0)
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))
509 (t
510 (math-add (calcFunc-lsh a (- n w) w)
511 (calcFunc-lsh a n w)))))))
512
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)))
525 a
526 (math-sub a (math-power-of-2 (- w)))))
527 ((Math-negp a)
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))
531 a
532 (logand a (1- (lsh 1 w)))))
533 (t
534 (math-normalize
535 (cons 'bigpos
536 (math-clip-bignum (cdr (math-bignum-test (math-trunc a)))
537 w))))))
538
539 (defalias 'calcFunc-clip 'math-clip)
540
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)
545 (1- (lsh 1 w))))
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
549 (cdr q)))))
550
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)))
555 (if res
556 (cdr res)
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))
561 digs))))
562
563 (defvar math-log2-cache (list '(2 . 1)
564 '(4 . 2)
565 '(8 . 3)
566 '(10 . (float 332193 -5))
567 '(16 . 4)
568 '(32 . 5)))
569 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6
570 (let ((res (assq x math-log2-cache)))
571 (if res
572 (cdr res)
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))
577 log))))
578
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"])
583
584 (defsubst math-format-radix-digit (a) ; [X D]
585 (aref math-radix-digits a))
586
587 (defun math-format-radix (a) ; [X S]
588 (if (< a calc-number-radix)
589 (if (< a 0)
590 (concat "-" (math-format-radix (- a)))
591 (math-format-radix-digit a))
592 (let ((s ""))
593 (while (> a 0)
594 (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s)
595 a (/ a calc-number-radix)))
596 s)))
597
598 (defconst math-binary-digits ["000" "001" "010" "011"
599 "100" "101" "110" "111"])
600 (defun math-format-binary (a) ; [X S]
601 (if (< a 8)
602 (if (< a 0)
603 (concat "-" (math-format-binary (- a)))
604 (math-format-radix a))
605 (let ((s ""))
606 (while (> a 7)
607 (setq s (concat (aref math-binary-digits (% a 8)) s)
608 a (/ a 8)))
609 (concat (math-format-radix a) s))))
610
611 (defun math-format-bignum-radix (a) ; [X L]
612 (cond ((null a) "0")
613 ((and (null (cdr a))
614 (< (car a) calc-number-radix))
615 (math-format-radix-digit (car a)))
616 (t
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)))))))
620
621 (defun math-format-bignum-binary (a) ; [X L]
622 (cond ((null a) "0")
623 ((null (cdr a))
624 (math-format-binary (car a)))
625 (t
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)))))))
631
632 (defun math-format-bignum-octal (a) ; [X L]
633 (cond ((null a) "0")
634 ((null (cdr a))
635 (math-format-radix (car a)))
636 (t
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)))))))
642
643 (defun math-format-bignum-hex (a) ; [X L]
644 (cond ((null a) "0")
645 ((null (cdr a))
646 (math-format-radix (car a)))
647 (t
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)))))))
652
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 )
656 (if (>= (nth 2 a) 0)
657 (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0)
658 (let* ((d (math-numdigs (nth 1 a)))
659 (n (- (nth 2 a))))
660 (if need-frac
661 (if (>= n d)
662 (list 0 a n)
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))
666 '(float 0 0) 0)))))
667
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)
672 (str nil)
673 pos)
674 (if (eq fmt 'fix)
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
679 afigs t))))
680 (int (car fp))
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)
689 str))
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))))
696 (setq figs 0))))
697 (or str
698 (let* ((prec calc-internal-prec)
699 (afigs (if (> figs 0)
700 figs
701 (max 1 (+ figs
702 (1- (math-convert-radix-digits
703 (max prec
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))
708 expmid vmid eadj)
709 (setq a (math-normalize a))
710 (if (Math-zerop a)
711 (setq explo 0)
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
729 (1- afigs)))))
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)
735 explo (1+ explo)))
736 (if (and (eq fmt 'float)
737 (math-lessp explo (+ (if (= figs 0)
738 (1- (math-convert-radix-digits
739 prec))
740 afigs)
741 calc-display-sci-high 1))
742 (math-lessp calc-display-sci-low explo))
743 (let ((dpos (1+ explo)))
744 (cond ((<= dpos 0)
745 (setq str (concat "0" point (make-string (- dpos) ?0)
746 str)))
747 ((> dpos (length str))
748 (setq str (concat str (make-string (- dpos (length str))
749 ?0) point)))
750 (t
751 (setq str (concat (substring str 0 dpos) point
752 (substring str dpos)))))
753 (setq explo nil))
754 (setq eadj (if (eq fmt 'eng)
755 (min (math-mod explo 3) (length str))
756 0)
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)))
765 (if explo
766 (let ((estr (let ((calc-number-radix 10)
767 (calc-group-digits nil))
768 (math-format-number
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)))))))
774 str))
775
776 (defvar math-radix-digits-cache nil)
777
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
786 (math-mul n log)
787 (math-div n log))))
788 math-radix-digits-cache))))))))
789
790 (defvar math-radix-float-cache-tag nil)
791 (defvar math-radix-float-cache)
792
793 (defun math-radix-float-power (n)
794 (if (eq n 0)
795 '(float 1 0)
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
799 calc-internal-prec)
800 math-radix-float-cache nil))
801 (math-normalize
802 (or (cdr (assoc n math-radix-float-cache))
803 (cdr (car (setq math-radix-float-cache
804 (cons (cons
805 n
806 (let ((calc-internal-prec
807 (cdr math-radix-float-cache-tag)))
808 (if (math-negp n)
809 (math-div-float '(float 1 0)
810 (math-radix-float-power
811 (math-neg n)))
812 (math-mul-float (math-sqr-float
813 (math-radix-float-power
814 (math-div2 n)))
815 (if (math-evenp n)
816 '(float 1 0)
817 (math-float
818 calc-number-radix))))))
819 math-radix-float-cache))))))))
820
821 ;;; Two's complement mode
822
823 (defun math-format-twos-complement (a)
824 "Format an integer in two's complement mode."
825 (let* (;(calc-leading-zeros t)
826 (overflow nil)
827 (negative nil)
828 (num
829 (cond
830 ((or (eq a 0)
831 (and (Math-integer-posp a)))
832 (if (integerp 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)))
837 (if (integerp newa)
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)
842 calc-number-radix))
843 (len (length num)))
844 (if (< len digs)
845 (setq num (concat (make-string (- digs len) ?0) num))))
846 (when calc-group-digits
847 (setq num (math-group-float num)))
848 (concat
849 (number-to-string calc-number-radix)
850 "##"
851 num)))
852
853 (provide 'calc-bin)
854
855 ;;; calc-bin.el ends here