Mercurial > emacs
annotate lisp/calc/calc-bin.el @ 81979:733a82d712c8
(vc-find-root): Walk up the tree to find an existing
`file' from which to start the search.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Thu, 19 Jul 2007 18:49:59 +0000 |
parents | 95cbb77aca81 |
children | b98604865ea0 988f1edc9674 |
rev | line source |
---|---|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
1 ;;; calc-bin.el --- binary functions for Calc |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
2 |
64325
1db49616ce05
Update copyright information.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
62033
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004, |
75346 | 4 ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
5 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
6 ;; Author: David Gillespie <daveg@synaptics.com> |
77465
1154f082efd9
Update maintainer's address.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
76595
diff
changeset
|
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com> |
40785 | 8 |
9 ;; This file is part of GNU Emacs. | |
10 | |
76595
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
11 ;; GNU Emacs is free software; you can redistribute it and/or modify |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
12 ;; it under the terms of the GNU General Public License as published by |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
13 ;; the Free Software Foundation; either version 2, or (at your option) |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
14 ;; any later version. |
40785 | 15 |
76595
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
16 ;; GNU Emacs is distributed in the hope that it will be useful, |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
19 ;; GNU General Public License for more details. |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
20 |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
21 ;; You should have received a copy of the GNU General Public License |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
497d17a80bb8
Change form of license text to match rest of Emacs.
Glenn Morris <rgm@gnu.org>
parents:
75346
diff
changeset
|
24 ;; Boston, MA 02110-1301, USA. |
40785 | 25 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
26 ;;; Commentary: |
40785 | 27 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
28 ;;; Code: |
40785 | 29 |
30 ;; This file is autoloaded from calc-ext.el. | |
58648
617cbf064b4b
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58553
diff
changeset
|
31 |
40785 | 32 (require 'calc-ext) |
33 (require 'calc-macs) | |
34 | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
35 ;;; Some useful numbers |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
36 (defconst math-bignum-logb-digit-size |
81750
e84fd23f66ff
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): Evaluate
Jay Belanger <jay.p.belanger@gmail.com>
parents:
81717
diff
changeset
|
37 (eval-when-compile (logb math-bignum-digit-size)) |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
38 "The logb of the size of a bignum digit. |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
39 This is the largest value of B such that 2^B is less than |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
40 the size of a Calc bignum digit.") |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
41 |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
42 (defconst math-bignum-digit-power-of-two |
81752
95cbb77aca81
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two):
Jay Belanger <jay.p.belanger@gmail.com>
parents:
81750
diff
changeset
|
43 (eval-when-compile (expt 2 (logb math-bignum-digit-size))) |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
44 "The largest power of 2 less than the size of a Calc bignum digit.") |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
45 |
40785 | 46 ;;; b-prefix binary commands. |
47 | |
48 (defun calc-and (n) | |
49 (interactive "P") | |
50 (calc-slow-wrapper | |
51 (calc-enter-result 2 "and" | |
52 (append '(calcFunc-and) | |
53 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
54 (and n (list (prefix-numeric-value n))))))) |
40785 | 55 |
56 (defun calc-or (n) | |
57 (interactive "P") | |
58 (calc-slow-wrapper | |
59 (calc-enter-result 2 "or" | |
60 (append '(calcFunc-or) | |
61 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
62 (and n (list (prefix-numeric-value n))))))) |
40785 | 63 |
64 (defun calc-xor (n) | |
65 (interactive "P") | |
66 (calc-slow-wrapper | |
67 (calc-enter-result 2 "xor" | |
68 (append '(calcFunc-xor) | |
69 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
70 (and n (list (prefix-numeric-value n))))))) |
40785 | 71 |
72 (defun calc-diff (n) | |
73 (interactive "P") | |
74 (calc-slow-wrapper | |
75 (calc-enter-result 2 "diff" | |
76 (append '(calcFunc-diff) | |
77 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
78 (and n (list (prefix-numeric-value n))))))) |
40785 | 79 |
80 (defun calc-not (n) | |
81 (interactive "P") | |
82 (calc-slow-wrapper | |
83 (calc-enter-result 1 "not" | |
84 (append '(calcFunc-not) | |
85 (calc-top-list-n 1) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
86 (and n (list (prefix-numeric-value n))))))) |
40785 | 87 |
88 (defun calc-lshift-binary (n) | |
89 (interactive "P") | |
90 (calc-slow-wrapper | |
91 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
92 (calc-enter-result hyp "lsh" | |
93 (append '(calcFunc-lsh) | |
94 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
95 (and n (list (prefix-numeric-value n)))))))) |
40785 | 96 |
97 (defun calc-rshift-binary (n) | |
98 (interactive "P") | |
99 (calc-slow-wrapper | |
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) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
104 (and n (list (prefix-numeric-value n)))))))) |
40785 | 105 |
106 (defun calc-lshift-arith (n) | |
107 (interactive "P") | |
108 (calc-slow-wrapper | |
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) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
113 (and n (list (prefix-numeric-value n)))))))) |
40785 | 114 |
115 (defun calc-rshift-arith (n) | |
116 (interactive "P") | |
117 (calc-slow-wrapper | |
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) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
122 (and n (list (prefix-numeric-value n)))))))) |
40785 | 123 |
124 (defun calc-rotate-binary (n) | |
125 (interactive "P") | |
126 (calc-slow-wrapper | |
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) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
131 (and n (list (prefix-numeric-value n)))))))) |
40785 | 132 |
133 (defun calc-clip (n) | |
134 (interactive "P") | |
135 (calc-slow-wrapper | |
136 (calc-enter-result 1 "clip" | |
137 (append '(calcFunc-clip) | |
138 (calc-top-list-n 1) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
139 (and n (list (prefix-numeric-value n))))))) |
40785 | 140 |
141 (defun calc-word-size (n) | |
142 (interactive "P") | |
143 (calc-wrapper | |
144 (or n (setq n (read-string (format "Binary word size: (default %d) " | |
145 calc-word-size)))) | |
146 (setq n (if (stringp n) | |
147 (if (equal n "") | |
148 calc-word-size | |
149 (if (string-match "\\`[-+]?[0-9]+\\'" n) | |
62033
729b3756f347
(calc-word-size): Replace string-to-int by string-to-number.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58648
diff
changeset
|
150 (string-to-number n) |
40785 | 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))) | |
159 (if (< n 0) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
160 (message "Binary word size is %d bits (2's complement)" (- n)) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
161 (message "Binary word size is %d bits" n)))) |
40785 | 162 |
163 | |
164 | |
165 | |
166 | |
167 ;;; d-prefix mode commands. | |
168 | |
169 (defun calc-radix (n) | |
170 (interactive "NDisplay radix (2-36): ") | |
171 (calc-wrapper | |
172 (if (and (>= n 2) (<= n 36)) | |
173 (progn | |
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)) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
178 (message "Number radix is %d" n))) |
40785 | 179 |
180 (defun calc-decimal-radix () | |
181 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
182 (calc-radix 10)) |
40785 | 183 |
184 (defun calc-binary-radix () | |
185 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
186 (calc-radix 2)) |
40785 | 187 |
188 (defun calc-octal-radix () | |
189 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
190 (calc-radix 8)) |
40785 | 191 |
192 (defun calc-hex-radix () | |
193 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
194 (calc-radix 16)) |
40785 | 195 |
196 (defun calc-leading-zeros (n) | |
197 (interactive "P") | |
198 (calc-wrapper | |
199 (if (calc-change-mode 'calc-leading-zeros n t t) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
200 (message "Zero-padding integers to %d digits (assuming radix %d)" |
40785 | 201 (let* ((calc-internal-prec 6)) |
202 (math-compute-max-digits (math-abs calc-word-size) | |
203 calc-number-radix)) | |
204 calc-number-radix) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
205 (message "Omitting leading zeros on integers")))) |
40785 | 206 |
207 | |
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))) | |
215 (while (<= i n) | |
216 (setq val (math-mul val 2) | |
217 math-power-of-2-cache (nconc math-power-of-2-cache | |
218 (list val)) | |
219 i (1+ i))) | |
220 val)) | |
221 (let ((found (assq n math-big-power-of-2-cache))) | |
222 (if found | |
223 (cdr found) | |
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)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
227 po2))))) |
40785 | 228 |
229 (defun math-integer-log2 (n) ; [I I] [Public] | |
230 (let ((i 0) | |
231 (p math-power-of-2-cache) | |
232 val) | |
233 (while (and p (Math-natnum-lessp (setq val (car p)) n)) | |
234 (setq p (cdr p) | |
235 i (1+ i))) | |
236 (if p | |
237 (and (equal val n) | |
238 i) | |
239 (while (Math-natnum-lessp | |
240 (prog1 | |
241 (setq val (math-mul val 2)) | |
242 (setq math-power-of-2-cache (nconc math-power-of-2-cache | |
243 (list val)))) | |
244 n) | |
245 (setq i (1+ i))) | |
246 (and (equal val n) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
247 i)))) |
40785 | 248 |
249 | |
250 | |
251 | |
252 ;;; Bitwise operations. | |
253 | |
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))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
270 w)))) |
40785 | 271 |
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))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
278 (cdr (Math-bignum-test a)))) |
40785 | 279 |
280 (defun math-binary-modulo-args (f a b w) | |
281 (let (mod) | |
282 (if (eq (car-safe a) 'mod) | |
283 (progn | |
284 (setq mod (nth 2 a) | |
285 a (nth 1 a)) | |
286 (if (eq (car-safe b) 'mod) | |
287 (if (equal mod (nth 2 b)) | |
288 (setq b (nth 1 b)) | |
289 (math-reject-arg b "*Inconsistent modulos")))) | |
290 (setq mod (nth 2 b) | |
291 b (nth 1 b))) | |
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))) | |
297 (if bits | |
298 (if w | |
299 (if (/= w bits) | |
300 (calc-record-why | |
301 "*Warning: Modulo inconsistent with word size")) | |
302 (setq w bits)) | |
303 (calc-record-why "*Warning: Modulo is not a power of 2")) | |
304 (math-make-mod (if b | |
305 (funcall f a b w) | |
306 (funcall f a w)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
307 mod)))) |
40785 | 308 |
309 (defun math-and-bignum (a b) ; [l l l] | |
310 (and a b | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
311 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two)) |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
312 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 313 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa)) |
314 (math-norm-bignum (car qb))) | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
315 math-bignum-digit-power-of-two |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
316 (logand (cdr qa) (cdr qb)))))) |
40785 | 317 |
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))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
334 w)))) |
40785 | 335 |
336 (defun math-or-bignum (a b) ; [l l l] | |
337 (and (or a b) | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
338 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two)) |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
339 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 340 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa)) |
341 (math-norm-bignum (car qb))) | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
342 math-bignum-digit-power-of-two |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
343 (logior (cdr qa) (cdr qb)))))) |
40785 | 344 |
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))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
361 w)))) |
40785 | 362 |
363 (defun math-xor-bignum (a b) ; [l l l] | |
364 (and (or a b) | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
365 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two)) |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
366 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 367 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa)) |
368 (math-norm-bignum (car qb))) | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
369 math-bignum-digit-power-of-two |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
370 (logxor (cdr qa) (cdr qb)))))) |
40785 | 371 |
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))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
388 w)))) |
40785 | 389 |
390 (defun math-diff-bignum (a b) ; [l l l] | |
391 (and a | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
392 (let ((qa (math-div-bignum-digit a math-bignum-digit-power-of-two)) |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
393 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 394 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa)) |
395 (math-norm-bignum (car qb))) | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
396 math-bignum-digit-power-of-two |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
397 (logand (cdr qa) (lognot (cdr qb))))))) |
40785 | 398 |
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)) | |
410 (t (math-normalize | |
411 (cons 'bigpos | |
412 (math-not-bignum (math-binary-arg a w) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
413 w)))))) |
40785 | 414 |
415 (defun math-not-bignum (a w) ; [l l] | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
416 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two))) |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
417 (if (<= w math-bignum-logb-digit-size) |
40785 | 418 (list (logand (lognot (cdr q)) |
419 (1- (lsh 1 w)))) | |
420 (math-mul-bignum-digit (math-not-bignum (math-norm-bignum (car q)) | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
421 (- w math-bignum-logb-digit-size)) |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
422 math-bignum-digit-power-of-two |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
423 (logxor (cdr q) |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
424 (1- math-bignum-digit-power-of-two)))))) |
40785 | 425 |
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)) | |
432 (or (integerp w) | |
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)) | |
438 (if (< w 0) | |
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)) | |
443 (Math-lessp w n)) | |
444 0) | |
445 ((< n 0) | |
446 (math-quotient (math-clip a w) (math-power-of-2 (- n)))) | |
447 (t | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
448 (math-clip (math-mul a (math-power-of-2 n)) w)))))) |
40785 | 449 |
450 (defun calcFunc-rsh (a &optional n w) ; [I I] [Public] | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
451 (calcFunc-lsh a (math-neg (or n 1)) w)) |
40785 | 452 |
453 (defun calcFunc-ash (a &optional n w) ; [I I] [Public] | |
454 (if (or (null n) | |
455 (not (Math-negp n))) | |
456 (calcFunc-lsh a n w) | |
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)) | |
462 (or (integerp w) | |
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)) | |
468 (if (< w 0) | |
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) | |
475 sh) | |
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) | |
480 (+ w n) w) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
481 sh))))))))) |
40785 | 482 |
483 (defun calcFunc-rash (a &optional n w) ; [I I] [Public] | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
484 (calcFunc-ash a (math-neg (or n 1)) w)) |
40785 | 485 |
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)) | |
492 (or (integerp w) | |
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)) | |
498 (if (< w 0) | |
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)) | |
505 (t | |
506 (math-add (calcFunc-lsh a (- n w) w) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
507 (calcFunc-lsh a n w))))))) |
40785 | 508 |
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))) | |
521 a | |
522 (math-sub a (math-power-of-2 (- w))))) | |
523 ((Math-negp a) | |
524 (math-normalize (cons 'bigpos (math-binary-arg a w)))) | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
525 ((and (integerp a) (< a math-small-integer-size)) |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
526 (if (> w (logb math-small-integer-size)) |
40785 | 527 a |
528 (logand a (1- (lsh 1 w))))) | |
529 (t | |
530 (math-normalize | |
531 (cons 'bigpos | |
532 (math-clip-bignum (cdr (math-bignum-test (math-trunc a))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
533 w)))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
534 |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
535 (defalias 'calcFunc-clip 'math-clip) |
40785 | 536 |
537 (defun math-clip-bignum (a w) ; [l l] | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
538 (let ((q (math-div-bignum-digit a math-bignum-digit-power-of-two))) |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
539 (if (<= w math-bignum-logb-digit-size) |
40785 | 540 (list (logand (cdr q) |
541 (1- (lsh 1 w)))) | |
542 (math-mul-bignum-digit (math-clip-bignum (math-norm-bignum (car q)) | |
81717
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
543 (- w math-bignum-logb-digit-size)) |
f81d25630552
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two): New
Jay Belanger <jay.p.belanger@gmail.com>
parents:
77465
diff
changeset
|
544 math-bignum-digit-power-of-two |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
545 (cdr q))))) |
40785 | 546 |
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))) | |
551 (if res | |
552 (cdr res) | |
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)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
557 digs)))) |
40785 | 558 |
559 (defvar math-log2-cache (list '(2 . 1) | |
560 '(4 . 2) | |
561 '(8 . 3) | |
562 '(10 . (float 332193 -5)) | |
563 '(16 . 4) | |
564 '(32 . 5))) | |
565 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6 | |
566 (let ((res (assq x math-log2-cache))) | |
567 (if res | |
568 (cdr res) | |
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)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
573 log)))) |
40785 | 574 |
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"]) | |
579 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
580 (defsubst math-format-radix-digit (a) ; [X D] |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
581 (aref math-radix-digits a)) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
582 |
40785 | 583 (defun math-format-radix (a) ; [X S] |
584 (if (< a calc-number-radix) | |
585 (if (< a 0) | |
586 (concat "-" (math-format-radix (- a))) | |
587 (math-format-radix-digit a)) | |
588 (let ((s "")) | |
589 (while (> a 0) | |
590 (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s) | |
591 a (/ a calc-number-radix))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
592 s))) |
40785 | 593 |
594 (defconst math-binary-digits ["000" "001" "010" "011" | |
595 "100" "101" "110" "111"]) | |
596 (defun math-format-binary (a) ; [X S] | |
597 (if (< a 8) | |
598 (if (< a 0) | |
599 (concat "-" (math-format-binary (- a))) | |
600 (math-format-radix a)) | |
601 (let ((s "")) | |
602 (while (> a 7) | |
603 (setq s (concat (aref math-binary-digits (% a 8)) s) | |
604 a (/ a 8))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
605 (concat (math-format-radix a) s)))) |
40785 | 606 |
607 (defun math-format-bignum-radix (a) ; [X L] | |
608 (cond ((null a) "0") | |
609 ((and (null (cdr a)) | |
610 (< (car a) calc-number-radix)) | |
611 (math-format-radix-digit (car a))) | |
612 (t | |
613 (let ((q (math-div-bignum-digit a calc-number-radix))) | |
614 (concat (math-format-bignum-radix (math-norm-bignum (car q))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
615 (math-format-radix-digit (cdr q))))))) |
40785 | 616 |
617 (defun math-format-bignum-binary (a) ; [X L] | |
618 (cond ((null a) "0") | |
619 ((null (cdr a)) | |
620 (math-format-binary (car a))) | |
621 (t | |
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)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
626 (aref math-binary-digits (% (cdr q) 8))))))) |
40785 | 627 |
628 (defun math-format-bignum-octal (a) ; [X L] | |
629 (cond ((null a) "0") | |
630 ((null (cdr a)) | |
631 (math-format-radix (car a))) | |
632 (t | |
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)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
637 (math-format-radix-digit (% (cdr q) 8))))))) |
40785 | 638 |
639 (defun math-format-bignum-hex (a) ; [X L] | |
640 (cond ((null a) "0") | |
641 ((null (cdr a)) | |
642 (math-format-radix (car a))) | |
643 (t | |
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)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
647 (math-format-radix-digit (% (cdr q) 16))))))) |
40785 | 648 |
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 ) | |
652 (if (>= (nth 2 a) 0) | |
653 (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0) | |
654 (let* ((d (math-numdigs (nth 1 a))) | |
655 (n (- (nth 2 a)))) | |
656 (if need-frac | |
657 (if (>= n d) | |
658 (list 0 a n) | |
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)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
662 '(float 0 0) 0))))) |
40785 | 663 |
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) | |
58553
1c7dfa5d5967
(math-format-radix-float): Make pos a local variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
668 (str nil) |
1c7dfa5d5967
(math-format-radix-float): Make pos a local variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
669 pos) |
40785 | 670 (if (eq fmt 'fix) |
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 | |
675 afigs t)))) | |
676 (int (car fp)) | |
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) | |
685 str)) | |
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))) | |
44572
9e404d7dbe1d
(math-format-radix-float): Load `calc-ext' before we call `math-group-float'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
690 (when calc-group-digits |
9e404d7dbe1d
(math-format-radix-float): Load `calc-ext' before we call `math-group-float'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
691 (setq str (math-group-float str)))) |
40785 | 692 (setq figs 0)))) |
693 (or str | |
694 (let* ((prec calc-internal-prec) | |
695 (afigs (if (> figs 0) | |
696 figs | |
697 (max 1 (+ figs | |
698 (1- (math-convert-radix-digits | |
699 (max prec | |
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)) | |
704 expmid vmid eadj) | |
705 (setq a (math-normalize a)) | |
706 (if (Math-zerop a) | |
707 (setq explo 0) | |
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 | |
725 (1- afigs))))) | |
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) | |
731 explo (1+ explo))) | |
732 (if (and (eq fmt 'float) | |
733 (math-lessp explo (+ (if (= figs 0) | |
734 (1- (math-convert-radix-digits | |
735 prec)) | |
736 afigs) | |
737 calc-display-sci-high 1)) | |
738 (math-lessp calc-display-sci-low explo)) | |
739 (let ((dpos (1+ explo))) | |
740 (cond ((<= dpos 0) | |
741 (setq str (concat "0" point (make-string (- dpos) ?0) | |
742 str))) | |
743 ((> dpos (length str)) | |
744 (setq str (concat str (make-string (- dpos (length str)) | |
745 ?0) point))) | |
746 (t | |
747 (setq str (concat (substring str 0 dpos) point | |
748 (substring str dpos))))) | |
749 (setq explo nil)) | |
750 (setq eadj (if (eq fmt 'eng) | |
751 (min (math-mod explo 3) (length str)) | |
752 0) | |
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)) | |
44572
9e404d7dbe1d
(math-format-radix-float): Load `calc-ext' before we call `math-group-float'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
759 (when calc-group-digits |
9e404d7dbe1d
(math-format-radix-float): Load `calc-ext' before we call `math-group-float'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
760 (setq str (math-group-float str))) |
40785 | 761 (if explo |
762 (let ((estr (let ((calc-number-radix 10) | |
763 (calc-group-digits nil)) | |
58553
1c7dfa5d5967
(math-format-radix-float): Make pos a local variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
764 (math-format-number |
1c7dfa5d5967
(math-format-radix-float): Make pos a local variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
765 (math-sub explo eadj))))) |
40785 | 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))))))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
770 str)) |
40785 | 771 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
772 (defvar math-radix-digits-cache nil) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
773 |
40785 | 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 | |
782 (math-mul n log) | |
783 (math-div n log)))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
784 math-radix-digits-cache)))))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
785 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
786 (defvar math-radix-float-cache-tag nil) |
58553
1c7dfa5d5967
(math-format-radix-float): Make pos a local variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
787 (defvar math-radix-float-cache) |
40785 | 788 |
789 (defun math-radix-float-power (n) | |
790 (if (eq n 0) | |
791 '(float 1 0) | |
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 | |
795 calc-internal-prec) | |
796 math-radix-float-cache nil)) | |
797 (math-normalize | |
798 (or (cdr (assoc n math-radix-float-cache)) | |
799 (cdr (car (setq math-radix-float-cache | |
800 (cons (cons | |
801 n | |
802 (let ((calc-internal-prec | |
803 (cdr math-radix-float-cache-tag))) | |
804 (if (math-negp n) | |
805 (math-div-float '(float 1 0) | |
806 (math-radix-float-power | |
807 (math-neg n))) | |
808 (math-mul-float (math-sqr-float | |
809 (math-radix-float-power | |
810 (math-div2 n))) | |
811 (if (math-evenp n) | |
812 '(float 1 0) | |
813 (math-float | |
814 calc-number-radix)))))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
815 math-radix-float-cache)))))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
816 |
58648
617cbf064b4b
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58553
diff
changeset
|
817 (provide 'calc-bin) |
40785 | 818 |
52401 | 819 ;;; arch-tag: f6dba7bc-53b2-41ae-919c-c266ab0ca8b3 |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
820 ;;; calc-bin.el ends here |