Mercurial > emacs
annotate lisp/calc/calc-bin.el @ 101559:3688f1456c9e
(mh-mm-merge-handles)
(mh-mm-set-handle-multipart-parameter, mh-mm-inline-text-vcard)
(mh-mml-minibuffer-read-disposition, mh-mm-save-part): Update with
code from Gnus 5.11 (closes SF #2235022).
author | Bill Wohler <wohler@newt.com> |
---|---|
date | Tue, 27 Jan 2009 06:34:57 +0000 |
parents | a9dc0e7c3f2b |
children | 9ddf1eafde9a |
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, |
100908 | 4 ;; 2005, 2006, 2007, 2008, 2009 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 | |
94654
6c9af2bfcfee
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
11 ;; GNU Emacs is free software: you can redistribute it and/or modify |
76595
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 |
94654
6c9af2bfcfee
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
13 ;; the Free Software Foundation, either version 3 of the License, or |
6c9af2bfcfee
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
14 ;; (at your option) 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 |
94654
6c9af2bfcfee
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
40785 | 23 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
24 ;;; Commentary: |
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 ;;; Code: |
40785 | 27 |
28 ;; 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
|
29 |
40785 | 30 (require 'calc-ext) |
31 (require 'calc-macs) | |
32 | |
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
|
33 ;;; 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
|
34 (defconst math-bignum-logb-digit-size |
82439
d45f82f9c8f3
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two):
Jay Belanger <jay.p.belanger@gmail.com>
parents:
82140
diff
changeset
|
35 (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
|
36 "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
|
37 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
|
38 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
|
39 |
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 (defconst math-bignum-digit-power-of-two |
82439
d45f82f9c8f3
(math-bignum-logb-digit-size,math-bignum-digit-power-of-two):
Jay Belanger <jay.p.belanger@gmail.com>
parents:
82140
diff
changeset
|
41 (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
|
42 "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
|
43 |
40785 | 44 ;;; b-prefix binary commands. |
45 | |
46 (defun calc-and (n) | |
47 (interactive "P") | |
48 (calc-slow-wrapper | |
49 (calc-enter-result 2 "and" | |
50 (append '(calcFunc-and) | |
51 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
52 (and n (list (prefix-numeric-value n))))))) |
40785 | 53 |
54 (defun calc-or (n) | |
55 (interactive "P") | |
56 (calc-slow-wrapper | |
57 (calc-enter-result 2 "or" | |
58 (append '(calcFunc-or) | |
59 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
60 (and n (list (prefix-numeric-value n))))))) |
40785 | 61 |
62 (defun calc-xor (n) | |
63 (interactive "P") | |
64 (calc-slow-wrapper | |
65 (calc-enter-result 2 "xor" | |
66 (append '(calcFunc-xor) | |
67 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
68 (and n (list (prefix-numeric-value n))))))) |
40785 | 69 |
70 (defun calc-diff (n) | |
71 (interactive "P") | |
72 (calc-slow-wrapper | |
73 (calc-enter-result 2 "diff" | |
74 (append '(calcFunc-diff) | |
75 (calc-top-list-n 2) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
76 (and n (list (prefix-numeric-value n))))))) |
40785 | 77 |
78 (defun calc-not (n) | |
79 (interactive "P") | |
80 (calc-slow-wrapper | |
81 (calc-enter-result 1 "not" | |
82 (append '(calcFunc-not) | |
83 (calc-top-list-n 1) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
84 (and n (list (prefix-numeric-value n))))))) |
40785 | 85 |
86 (defun calc-lshift-binary (n) | |
87 (interactive "P") | |
88 (calc-slow-wrapper | |
89 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
90 (calc-enter-result hyp "lsh" | |
91 (append '(calcFunc-lsh) | |
92 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
93 (and n (list (prefix-numeric-value n)))))))) |
40785 | 94 |
95 (defun calc-rshift-binary (n) | |
96 (interactive "P") | |
97 (calc-slow-wrapper | |
98 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
99 (calc-enter-result hyp "rsh" | |
100 (append '(calcFunc-rsh) | |
101 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
102 (and n (list (prefix-numeric-value n)))))))) |
40785 | 103 |
104 (defun calc-lshift-arith (n) | |
105 (interactive "P") | |
106 (calc-slow-wrapper | |
107 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
108 (calc-enter-result hyp "ash" | |
109 (append '(calcFunc-ash) | |
110 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
111 (and n (list (prefix-numeric-value n)))))))) |
40785 | 112 |
113 (defun calc-rshift-arith (n) | |
114 (interactive "P") | |
115 (calc-slow-wrapper | |
116 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
117 (calc-enter-result hyp "rash" | |
118 (append '(calcFunc-rash) | |
119 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
120 (and n (list (prefix-numeric-value n)))))))) |
40785 | 121 |
122 (defun calc-rotate-binary (n) | |
123 (interactive "P") | |
124 (calc-slow-wrapper | |
125 (let ((hyp (if (calc-is-hyperbolic) 2 1))) | |
126 (calc-enter-result hyp "rot" | |
127 (append '(calcFunc-rot) | |
128 (calc-top-list-n hyp) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
129 (and n (list (prefix-numeric-value n)))))))) |
40785 | 130 |
131 (defun calc-clip (n) | |
132 (interactive "P") | |
133 (calc-slow-wrapper | |
134 (calc-enter-result 1 "clip" | |
135 (append '(calcFunc-clip) | |
136 (calc-top-list-n 1) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
137 (and n (list (prefix-numeric-value n))))))) |
40785 | 138 |
139 (defun calc-word-size (n) | |
140 (interactive "P") | |
141 (calc-wrapper | |
142 (or n (setq n (read-string (format "Binary word size: (default %d) " | |
143 calc-word-size)))) | |
144 (setq n (if (stringp n) | |
145 (if (equal n "") | |
146 calc-word-size | |
147 (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
|
148 (string-to-number n) |
40785 | 149 (error "Expected an integer"))) |
150 (prefix-numeric-value n))) | |
151 (or (= n calc-word-size) | |
152 (if (> (math-abs n) 100) | |
153 (calc-change-mode 'calc-word-size n calc-leading-zeros) | |
154 (calc-change-mode '(calc-word-size calc-previous-modulo) | |
155 (list n (math-power-of-2 (math-abs n))) | |
156 calc-leading-zeros))) | |
157 (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
|
158 (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
|
159 (message "Binary word size is %d bits" n)))) |
40785 | 160 |
161 | |
162 | |
163 | |
164 | |
165 ;;; d-prefix mode commands. | |
166 | |
167 (defun calc-radix (n) | |
168 (interactive "NDisplay radix (2-36): ") | |
169 (calc-wrapper | |
170 (if (and (>= n 2) (<= n 36)) | |
171 (progn | |
172 (calc-change-mode 'calc-number-radix n t) | |
173 ;; also change global value so minibuffer sees it | |
174 (setq-default calc-number-radix calc-number-radix)) | |
175 (setq n calc-number-radix)) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
176 (message "Number radix is %d" n))) |
40785 | 177 |
178 (defun calc-decimal-radix () | |
179 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
180 (calc-radix 10)) |
40785 | 181 |
182 (defun calc-binary-radix () | |
183 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
184 (calc-radix 2)) |
40785 | 185 |
186 (defun calc-octal-radix () | |
187 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
188 (calc-radix 8)) |
40785 | 189 |
190 (defun calc-hex-radix () | |
191 (interactive) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
192 (calc-radix 16)) |
40785 | 193 |
194 (defun calc-leading-zeros (n) | |
195 (interactive "P") | |
196 (calc-wrapper | |
197 (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
|
198 (message "Zero-padding integers to %d digits (assuming radix %d)" |
40785 | 199 (let* ((calc-internal-prec 6)) |
200 (math-compute-max-digits (math-abs calc-word-size) | |
201 calc-number-radix)) | |
202 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
|
203 (message "Omitting leading zeros on integers")))) |
40785 | 204 |
205 | |
206 (defvar math-power-of-2-cache (list 1 2 4 8 16 32 64 128 256 512 1024)) | |
207 (defvar math-big-power-of-2-cache nil) | |
208 (defun math-power-of-2 (n) ; [I I] [Public] | |
209 (if (and (natnump n) (<= n 100)) | |
210 (or (nth n math-power-of-2-cache) | |
211 (let* ((i (length math-power-of-2-cache)) | |
212 (val (nth (1- i) math-power-of-2-cache))) | |
213 (while (<= i n) | |
214 (setq val (math-mul val 2) | |
215 math-power-of-2-cache (nconc math-power-of-2-cache | |
216 (list val)) | |
217 i (1+ i))) | |
218 val)) | |
219 (let ((found (assq n math-big-power-of-2-cache))) | |
220 (if found | |
221 (cdr found) | |
222 (let ((po2 (math-ipow 2 n))) | |
223 (setq math-big-power-of-2-cache | |
224 (cons (cons n po2) math-big-power-of-2-cache)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
225 po2))))) |
40785 | 226 |
227 (defun math-integer-log2 (n) ; [I I] [Public] | |
228 (let ((i 0) | |
229 (p math-power-of-2-cache) | |
230 val) | |
231 (while (and p (Math-natnum-lessp (setq val (car p)) n)) | |
232 (setq p (cdr p) | |
233 i (1+ i))) | |
234 (if p | |
235 (and (equal val n) | |
236 i) | |
237 (while (Math-natnum-lessp | |
238 (prog1 | |
239 (setq val (math-mul val 2)) | |
240 (setq math-power-of-2-cache (nconc math-power-of-2-cache | |
241 (list val)))) | |
242 n) | |
243 (setq i (1+ i))) | |
244 (and (equal val n) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
245 i)))) |
40785 | 246 |
247 | |
248 | |
249 | |
250 ;;; Bitwise operations. | |
251 | |
252 (defun calcFunc-and (a b &optional w) ; [I I I] [Public] | |
253 (cond ((Math-messy-integerp w) | |
254 (calcFunc-and a b (math-trunc w))) | |
255 ((and w (not (integerp w))) | |
256 (math-reject-arg w 'fixnump)) | |
257 ((and (integerp a) (integerp b)) | |
258 (math-clip (logand a b) w)) | |
259 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
260 (math-binary-modulo-args 'calcFunc-and a b w)) | |
261 ((not (Math-num-integerp a)) | |
262 (math-reject-arg a 'integerp)) | |
263 ((not (Math-num-integerp b)) | |
264 (math-reject-arg b 'integerp)) | |
265 (t (math-clip (cons 'bigpos | |
266 (math-and-bignum (math-binary-arg a w) | |
267 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
268 w)))) |
40785 | 269 |
270 (defun math-binary-arg (a w) | |
271 (if (not (Math-integerp a)) | |
272 (setq a (math-trunc a))) | |
273 (if (Math-integer-negp a) | |
274 (math-not-bignum (cdr (math-bignum-test (math-sub -1 a))) | |
275 (math-abs (if w (math-trunc w) calc-word-size))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
276 (cdr (Math-bignum-test a)))) |
40785 | 277 |
278 (defun math-binary-modulo-args (f a b w) | |
279 (let (mod) | |
280 (if (eq (car-safe a) 'mod) | |
281 (progn | |
282 (setq mod (nth 2 a) | |
283 a (nth 1 a)) | |
284 (if (eq (car-safe b) 'mod) | |
285 (if (equal mod (nth 2 b)) | |
286 (setq b (nth 1 b)) | |
287 (math-reject-arg b "*Inconsistent modulos")))) | |
288 (setq mod (nth 2 b) | |
289 b (nth 1 b))) | |
290 (if (Math-messy-integerp mod) | |
291 (setq mod (math-trunc mod)) | |
292 (or (Math-integerp mod) | |
293 (math-reject-arg mod 'integerp))) | |
294 (let ((bits (math-integer-log2 mod))) | |
295 (if bits | |
296 (if w | |
297 (if (/= w bits) | |
298 (calc-record-why | |
299 "*Warning: Modulo inconsistent with word size")) | |
300 (setq w bits)) | |
301 (calc-record-why "*Warning: Modulo is not a power of 2")) | |
302 (math-make-mod (if b | |
303 (funcall f a b w) | |
304 (funcall f a w)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
305 mod)))) |
40785 | 306 |
307 (defun math-and-bignum (a b) ; [l l l] | |
308 (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
|
309 (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
|
310 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 311 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa)) |
312 (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
|
313 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
|
314 (logand (cdr qa) (cdr qb)))))) |
40785 | 315 |
316 (defun calcFunc-or (a b &optional w) ; [I I I] [Public] | |
317 (cond ((Math-messy-integerp w) | |
318 (calcFunc-or a b (math-trunc w))) | |
319 ((and w (not (integerp w))) | |
320 (math-reject-arg w 'fixnump)) | |
321 ((and (integerp a) (integerp b)) | |
322 (math-clip (logior a b) w)) | |
323 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
324 (math-binary-modulo-args 'calcFunc-or a b w)) | |
325 ((not (Math-num-integerp a)) | |
326 (math-reject-arg a 'integerp)) | |
327 ((not (Math-num-integerp b)) | |
328 (math-reject-arg b 'integerp)) | |
329 (t (math-clip (cons 'bigpos | |
330 (math-or-bignum (math-binary-arg a w) | |
331 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
332 w)))) |
40785 | 333 |
334 (defun math-or-bignum (a b) ; [l l l] | |
335 (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
|
336 (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
|
337 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 338 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa)) |
339 (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
|
340 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
|
341 (logior (cdr qa) (cdr qb)))))) |
40785 | 342 |
343 (defun calcFunc-xor (a b &optional w) ; [I I I] [Public] | |
344 (cond ((Math-messy-integerp w) | |
345 (calcFunc-xor a b (math-trunc w))) | |
346 ((and w (not (integerp w))) | |
347 (math-reject-arg w 'fixnump)) | |
348 ((and (integerp a) (integerp b)) | |
349 (math-clip (logxor a b) w)) | |
350 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
351 (math-binary-modulo-args 'calcFunc-xor a b w)) | |
352 ((not (Math-num-integerp a)) | |
353 (math-reject-arg a 'integerp)) | |
354 ((not (Math-num-integerp b)) | |
355 (math-reject-arg b 'integerp)) | |
356 (t (math-clip (cons 'bigpos | |
357 (math-xor-bignum (math-binary-arg a w) | |
358 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
359 w)))) |
40785 | 360 |
361 (defun math-xor-bignum (a b) ; [l l l] | |
362 (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
|
363 (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
|
364 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 365 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa)) |
366 (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
|
367 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
|
368 (logxor (cdr qa) (cdr qb)))))) |
40785 | 369 |
370 (defun calcFunc-diff (a b &optional w) ; [I I I] [Public] | |
371 (cond ((Math-messy-integerp w) | |
372 (calcFunc-diff a b (math-trunc w))) | |
373 ((and w (not (integerp w))) | |
374 (math-reject-arg w 'fixnump)) | |
375 ((and (integerp a) (integerp b)) | |
376 (math-clip (logand a (lognot b)) w)) | |
377 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
378 (math-binary-modulo-args 'calcFunc-diff a b w)) | |
379 ((not (Math-num-integerp a)) | |
380 (math-reject-arg a 'integerp)) | |
381 ((not (Math-num-integerp b)) | |
382 (math-reject-arg b 'integerp)) | |
383 (t (math-clip (cons 'bigpos | |
384 (math-diff-bignum (math-binary-arg a w) | |
385 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
386 w)))) |
40785 | 387 |
388 (defun math-diff-bignum (a b) ; [l l l] | |
389 (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
|
390 (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
|
391 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 392 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa)) |
393 (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
|
394 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
|
395 (logand (cdr qa) (lognot (cdr qb))))))) |
40785 | 396 |
397 (defun calcFunc-not (a &optional w) ; [I I] [Public] | |
398 (cond ((Math-messy-integerp w) | |
399 (calcFunc-not a (math-trunc w))) | |
400 ((eq (car-safe a) 'mod) | |
401 (math-binary-modulo-args 'calcFunc-not a nil w)) | |
402 ((and w (not (integerp w))) | |
403 (math-reject-arg w 'fixnump)) | |
404 ((not (Math-num-integerp a)) | |
405 (math-reject-arg a 'integerp)) | |
406 ((< (or w (setq w calc-word-size)) 0) | |
407 (math-clip (calcFunc-not a (- w)) w)) | |
408 (t (math-normalize | |
409 (cons 'bigpos | |
410 (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
|
411 w)))))) |
40785 | 412 |
413 (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
|
414 (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
|
415 (if (<= w math-bignum-logb-digit-size) |
40785 | 416 (list (logand (lognot (cdr q)) |
417 (1- (lsh 1 w)))) | |
418 (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
|
419 (- 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
|
420 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
|
421 (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
|
422 (1- math-bignum-digit-power-of-two)))))) |
40785 | 423 |
424 (defun calcFunc-lsh (a &optional n w) ; [I I] [Public] | |
425 (setq a (math-trunc a) | |
426 n (if n (math-trunc n) 1)) | |
427 (if (eq (car-safe a) 'mod) | |
428 (math-binary-modulo-args 'calcFunc-lsh a n w) | |
429 (setq w (if w (math-trunc w) calc-word-size)) | |
430 (or (integerp w) | |
431 (math-reject-arg w 'fixnump)) | |
432 (or (Math-integerp a) | |
433 (math-reject-arg a 'integerp)) | |
434 (or (Math-integerp n) | |
435 (math-reject-arg n 'integerp)) | |
436 (if (< w 0) | |
437 (math-clip (calcFunc-lsh a n (- w)) w) | |
438 (if (Math-integer-negp a) | |
439 (setq a (math-clip a w))) | |
440 (cond ((or (Math-lessp n (- w)) | |
441 (Math-lessp w n)) | |
442 0) | |
443 ((< n 0) | |
444 (math-quotient (math-clip a w) (math-power-of-2 (- n)))) | |
445 (t | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
446 (math-clip (math-mul a (math-power-of-2 n)) w)))))) |
40785 | 447 |
448 (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
|
449 (calcFunc-lsh a (math-neg (or n 1)) w)) |
40785 | 450 |
451 (defun calcFunc-ash (a &optional n w) ; [I I] [Public] | |
452 (if (or (null n) | |
453 (not (Math-negp n))) | |
454 (calcFunc-lsh a n w) | |
455 (setq a (math-trunc a) | |
456 n (if n (math-trunc n) 1)) | |
457 (if (eq (car-safe a) 'mod) | |
458 (math-binary-modulo-args 'calcFunc-ash a n w) | |
459 (setq w (if w (math-trunc w) calc-word-size)) | |
460 (or (integerp w) | |
461 (math-reject-arg w 'fixnump)) | |
462 (or (Math-integerp a) | |
463 (math-reject-arg a 'integerp)) | |
464 (or (Math-integerp n) | |
465 (math-reject-arg n 'integerp)) | |
466 (if (< w 0) | |
467 (math-clip (calcFunc-ash a n (- w)) w) | |
468 (if (Math-integer-negp a) | |
469 (setq a (math-clip a w))) | |
470 (let ((two-to-sizem1 (math-power-of-2 (1- w))) | |
471 (sh (calcFunc-lsh a n w))) | |
472 (cond ((Math-natnum-lessp a two-to-sizem1) | |
473 sh) | |
474 ((Math-lessp n (- 1 w)) | |
475 (math-add (math-mul two-to-sizem1 2) -1)) | |
476 (t (let ((two-to-n (math-power-of-2 (- n)))) | |
477 (math-add (calcFunc-lsh (math-add two-to-n -1) | |
478 (+ w n) w) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
479 sh))))))))) |
40785 | 480 |
481 (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
|
482 (calcFunc-ash a (math-neg (or n 1)) w)) |
40785 | 483 |
484 (defun calcFunc-rot (a &optional n w) ; [I I] [Public] | |
485 (setq a (math-trunc a) | |
486 n (if n (math-trunc n) 1)) | |
487 (if (eq (car-safe a) 'mod) | |
488 (math-binary-modulo-args 'calcFunc-rot a n w) | |
489 (setq w (if w (math-trunc w) calc-word-size)) | |
490 (or (integerp w) | |
491 (math-reject-arg w 'fixnump)) | |
492 (or (Math-integerp a) | |
493 (math-reject-arg a 'integerp)) | |
494 (or (Math-integerp n) | |
495 (math-reject-arg n 'integerp)) | |
496 (if (< w 0) | |
497 (math-clip (calcFunc-rot a n (- w)) w) | |
498 (if (Math-integer-negp a) | |
499 (setq a (math-clip a w))) | |
500 (cond ((or (Math-integer-negp n) | |
501 (not (Math-natnum-lessp n w))) | |
502 (calcFunc-rot a (math-mod n w) w)) | |
503 (t | |
504 (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
|
505 (calcFunc-lsh a n w))))))) |
40785 | 506 |
507 (defun math-clip (a &optional w) ; [I I] [Public] | |
508 (cond ((Math-messy-integerp w) | |
509 (math-clip a (math-trunc w))) | |
510 ((eq (car-safe a) 'mod) | |
511 (math-binary-modulo-args 'math-clip a nil w)) | |
512 ((and w (not (integerp w))) | |
513 (math-reject-arg w 'fixnump)) | |
514 ((not (Math-num-integerp a)) | |
515 (math-reject-arg a 'integerp)) | |
516 ((< (or w (setq w calc-word-size)) 0) | |
517 (setq a (math-clip a (- w))) | |
518 (if (Math-natnum-lessp a (math-power-of-2 (- -1 w))) | |
519 a | |
520 (math-sub a (math-power-of-2 (- w))))) | |
521 ((Math-negp a) | |
522 (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
|
523 ((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
|
524 (if (> w (logb math-small-integer-size)) |
40785 | 525 a |
526 (logand a (1- (lsh 1 w))))) | |
527 (t | |
528 (math-normalize | |
529 (cons 'bigpos | |
530 (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
|
531 w)))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
532 |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
533 (defalias 'calcFunc-clip 'math-clip) |
40785 | 534 |
535 (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
|
536 (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
|
537 (if (<= w math-bignum-logb-digit-size) |
40785 | 538 (list (logand (cdr q) |
539 (1- (lsh 1 w)))) | |
540 (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
|
541 (- 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
|
542 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
|
543 (cdr q))))) |
40785 | 544 |
545 (defvar math-max-digits-cache nil) | |
546 (defun math-compute-max-digits (w r) | |
547 (let* ((pair (+ (* r 100000) w)) | |
548 (res (assq pair math-max-digits-cache))) | |
549 (if res | |
550 (cdr res) | |
551 (let* ((calc-command-flags nil) | |
552 (digs (math-ceiling (math-div w (math-real-log2 r))))) | |
553 (setq math-max-digits-cache (cons (cons pair digs) | |
554 math-max-digits-cache)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
555 digs)))) |
40785 | 556 |
557 (defvar math-log2-cache (list '(2 . 1) | |
558 '(4 . 2) | |
559 '(8 . 3) | |
560 '(10 . (float 332193 -5)) | |
561 '(16 . 4) | |
562 '(32 . 5))) | |
563 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6 | |
564 (let ((res (assq x math-log2-cache))) | |
565 (if res | |
566 (cdr res) | |
567 (let* ((calc-symbolic-mode nil) | |
568 (calc-display-working-message nil) | |
569 (log (calcFunc-log x 2))) | |
570 (setq math-log2-cache (cons (cons x log) math-log2-cache)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
571 log)))) |
40785 | 572 |
573 (defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" | |
574 "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" | |
575 "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" | |
576 "U" "V" "W" "X" "Y" "Z"]) | |
577 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
578 (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
|
579 (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
|
580 |
40785 | 581 (defun math-format-radix (a) ; [X S] |
582 (if (< a calc-number-radix) | |
583 (if (< a 0) | |
584 (concat "-" (math-format-radix (- a))) | |
585 (math-format-radix-digit a)) | |
586 (let ((s "")) | |
587 (while (> a 0) | |
588 (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s) | |
589 a (/ a calc-number-radix))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
590 s))) |
40785 | 591 |
592 (defconst math-binary-digits ["000" "001" "010" "011" | |
593 "100" "101" "110" "111"]) | |
594 (defun math-format-binary (a) ; [X S] | |
595 (if (< a 8) | |
596 (if (< a 0) | |
597 (concat "-" (math-format-binary (- a))) | |
598 (math-format-radix a)) | |
599 (let ((s "")) | |
600 (while (> a 7) | |
601 (setq s (concat (aref math-binary-digits (% a 8)) s) | |
602 a (/ a 8))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
603 (concat (math-format-radix a) s)))) |
40785 | 604 |
605 (defun math-format-bignum-radix (a) ; [X L] | |
606 (cond ((null a) "0") | |
607 ((and (null (cdr a)) | |
608 (< (car a) calc-number-radix)) | |
609 (math-format-radix-digit (car a))) | |
610 (t | |
611 (let ((q (math-div-bignum-digit a calc-number-radix))) | |
612 (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
|
613 (math-format-radix-digit (cdr q))))))) |
40785 | 614 |
615 (defun math-format-bignum-binary (a) ; [X L] | |
616 (cond ((null a) "0") | |
617 ((null (cdr a)) | |
618 (math-format-binary (car a))) | |
619 (t | |
620 (let ((q (math-div-bignum-digit a 512))) | |
621 (concat (math-format-bignum-binary (math-norm-bignum (car q))) | |
622 (aref math-binary-digits (/ (cdr q) 64)) | |
623 (aref math-binary-digits (% (/ (cdr q) 8) 8)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
624 (aref math-binary-digits (% (cdr q) 8))))))) |
40785 | 625 |
626 (defun math-format-bignum-octal (a) ; [X L] | |
627 (cond ((null a) "0") | |
628 ((null (cdr a)) | |
629 (math-format-radix (car a))) | |
630 (t | |
631 (let ((q (math-div-bignum-digit a 512))) | |
632 (concat (math-format-bignum-octal (math-norm-bignum (car q))) | |
633 (math-format-radix-digit (/ (cdr q) 64)) | |
634 (math-format-radix-digit (% (/ (cdr q) 8) 8)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
635 (math-format-radix-digit (% (cdr q) 8))))))) |
40785 | 636 |
637 (defun math-format-bignum-hex (a) ; [X L] | |
638 (cond ((null a) "0") | |
639 ((null (cdr a)) | |
640 (math-format-radix (car a))) | |
641 (t | |
642 (let ((q (math-div-bignum-digit a 256))) | |
643 (concat (math-format-bignum-hex (math-norm-bignum (car q))) | |
644 (math-format-radix-digit (/ (cdr q) 16)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
645 (math-format-radix-digit (% (cdr q) 16))))))) |
40785 | 646 |
647 ;;; Decompose into integer and fractional parts, without depending | |
648 ;;; on calc-internal-prec. | |
649 (defun math-float-parts (a need-frac) ; returns ( int frac fracdigs ) | |
650 (if (>= (nth 2 a) 0) | |
651 (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0) | |
652 (let* ((d (math-numdigs (nth 1 a))) | |
653 (n (- (nth 2 a)))) | |
654 (if need-frac | |
655 (if (>= n d) | |
656 (list 0 a n) | |
657 (let ((qr (math-idivmod (nth 1 a) (math-scale-int 1 n)))) | |
658 (list (car qr) (math-make-float (cdr qr) (- n)) n))) | |
659 (list (math-scale-rounding (nth 1 a) (nth 2 a)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
660 '(float 0 0) 0))))) |
40785 | 661 |
662 (defun math-format-radix-float (a prec) | |
663 (let ((fmt (car calc-float-format)) | |
664 (figs (nth 1 calc-float-format)) | |
665 (point calc-point-char) | |
58553
1c7dfa5d5967
(math-format-radix-float): Make pos a local variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
666 (str nil) |
1c7dfa5d5967
(math-format-radix-float): Make pos a local variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
667 pos) |
40785 | 668 (if (eq fmt 'fix) |
669 (let* ((afigs (math-abs figs)) | |
670 (fp (math-float-parts a (> afigs 0))) | |
671 (calc-internal-prec (+ 3 (max (nth 2 fp) | |
672 (math-convert-radix-digits | |
673 afigs t)))) | |
674 (int (car fp)) | |
675 (frac (math-round (math-mul (math-normalize (nth 1 fp)) | |
676 (math-radix-float-power afigs))))) | |
677 (if (not (and (math-zerop frac) (math-zerop int) (< figs 0))) | |
678 (let ((math-radix-explicit-format nil)) | |
679 (let ((calc-group-digits nil)) | |
680 (setq str (if (> afigs 0) (math-format-number frac) "")) | |
681 (if (< (length str) afigs) | |
682 (setq str (concat (make-string (- afigs (length str)) ?0) | |
683 str)) | |
684 (if (> (length str) afigs) | |
685 (setq str (substring str 1) | |
686 int (math-add int 1)))) | |
687 (setq str (concat (math-format-number int) point str))) | |
44572
9e404d7dbe1d
(math-format-radix-float): Load `calc-ext' before we call `math-group-float'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
688 (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
|
689 (setq str (math-group-float str)))) |
40785 | 690 (setq figs 0)))) |
691 (or str | |
692 (let* ((prec calc-internal-prec) | |
693 (afigs (if (> figs 0) | |
694 figs | |
695 (max 1 (+ figs | |
696 (1- (math-convert-radix-digits | |
697 (max prec | |
698 (math-numdigs (nth 1 a))))))))) | |
699 (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t))) | |
700 (explo -1) (vlo (math-radix-float-power explo)) | |
701 (exphi 1) (vhi (math-radix-float-power exphi)) | |
702 expmid vmid eadj) | |
703 (setq a (math-normalize a)) | |
704 (if (Math-zerop a) | |
705 (setq explo 0) | |
706 (if (math-lessp-float '(float 1 0) a) | |
707 (while (not (math-lessp-float a vhi)) | |
708 (setq explo exphi vlo vhi | |
709 exphi (math-mul exphi 2) | |
710 vhi (math-radix-float-power exphi))) | |
711 (while (math-lessp-float a vlo) | |
712 (setq exphi explo vhi vlo | |
713 explo (math-mul explo 2) | |
714 vlo (math-radix-float-power explo)))) | |
715 (while (not (eq (math-sub exphi explo) 1)) | |
716 (setq expmid (math-div2 (math-add explo exphi)) | |
717 vmid (math-radix-float-power expmid)) | |
718 (if (math-lessp-float a vmid) | |
719 (setq exphi expmid vhi vmid) | |
720 (setq explo expmid vlo vmid))) | |
721 (setq a (math-div-float a vlo))) | |
722 (let* ((sc (math-round (math-mul a (math-radix-float-power | |
723 (1- afigs))))) | |
724 (math-radix-explicit-format nil)) | |
725 (let ((calc-group-digits nil)) | |
726 (setq str (math-format-number sc)))) | |
727 (if (> (length str) afigs) | |
728 (setq str (substring str 0 -1) | |
729 explo (1+ explo))) | |
730 (if (and (eq fmt 'float) | |
731 (math-lessp explo (+ (if (= figs 0) | |
732 (1- (math-convert-radix-digits | |
733 prec)) | |
734 afigs) | |
735 calc-display-sci-high 1)) | |
736 (math-lessp calc-display-sci-low explo)) | |
737 (let ((dpos (1+ explo))) | |
738 (cond ((<= dpos 0) | |
739 (setq str (concat "0" point (make-string (- dpos) ?0) | |
740 str))) | |
741 ((> dpos (length str)) | |
742 (setq str (concat str (make-string (- dpos (length str)) | |
743 ?0) point))) | |
744 (t | |
745 (setq str (concat (substring str 0 dpos) point | |
746 (substring str dpos))))) | |
747 (setq explo nil)) | |
748 (setq eadj (if (eq fmt 'eng) | |
749 (min (math-mod explo 3) (length str)) | |
750 0) | |
751 str (concat (substring str 0 (1+ eadj)) point | |
752 (substring str (1+ eadj))))) | |
753 (setq pos (length str)) | |
754 (while (eq (aref str (1- pos)) ?0) (setq pos (1- pos))) | |
755 (and explo (eq (aref str (1- pos)) ?.) (setq pos (1- pos))) | |
756 (setq str (substring str 0 pos)) | |
44572
9e404d7dbe1d
(math-format-radix-float): Load `calc-ext' before we call `math-group-float'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
757 (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
|
758 (setq str (math-group-float str))) |
40785 | 759 (if explo |
760 (let ((estr (let ((calc-number-radix 10) | |
761 (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
|
762 (math-format-number |
1c7dfa5d5967
(math-format-radix-float): Make pos a local variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
763 (math-sub explo eadj))))) |
40785 | 764 (setq str (if (or (memq calc-language '(math maple)) |
765 (> calc-number-radix 14)) | |
766 (format "%s*%d.^%s" str calc-number-radix estr) | |
767 (format "%se%s" str estr))))))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
768 str)) |
40785 | 769 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
770 (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
|
771 |
40785 | 772 (defun math-convert-radix-digits (n &optional to-dec) |
773 (let ((key (cons n (cons to-dec calc-number-radix)))) | |
774 (or (cdr (assoc key math-radix-digits-cache)) | |
775 (let* ((calc-internal-prec 6) | |
776 (log (math-div (math-real-log2 calc-number-radix) | |
777 '(float 332193 -5)))) | |
778 (cdr (car (setq math-radix-digits-cache | |
779 (cons (cons key (math-ceiling (if to-dec | |
780 (math-mul n log) | |
781 (math-div n log)))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
782 math-radix-digits-cache)))))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
783 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
784 (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
|
785 (defvar math-radix-float-cache) |
40785 | 786 |
787 (defun math-radix-float-power (n) | |
788 (if (eq n 0) | |
789 '(float 1 0) | |
790 (or (and (eq calc-number-radix (car math-radix-float-cache-tag)) | |
791 (<= calc-internal-prec (cdr math-radix-float-cache-tag))) | |
792 (setq math-radix-float-cache-tag (cons calc-number-radix | |
793 calc-internal-prec) | |
794 math-radix-float-cache nil)) | |
795 (math-normalize | |
796 (or (cdr (assoc n math-radix-float-cache)) | |
797 (cdr (car (setq math-radix-float-cache | |
798 (cons (cons | |
799 n | |
800 (let ((calc-internal-prec | |
801 (cdr math-radix-float-cache-tag))) | |
802 (if (math-negp n) | |
803 (math-div-float '(float 1 0) | |
804 (math-radix-float-power | |
805 (math-neg n))) | |
806 (math-mul-float (math-sqr-float | |
807 (math-radix-float-power | |
808 (math-div2 n))) | |
809 (if (math-evenp n) | |
810 '(float 1 0) | |
811 (math-float | |
812 calc-number-radix)))))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
813 math-radix-float-cache)))))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
814 |
58648
617cbf064b4b
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58553
diff
changeset
|
815 (provide 'calc-bin) |
40785 | 816 |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
817 ;; 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
|
818 ;;; calc-bin.el ends here |