Mercurial > emacs
annotate lisp/calc/calc-bin.el @ 111602:f44b93254721
Fix some ls-lisp oddness.
* lisp/ls-lisp.el (ls-lisp-dired-ignore-case): Make it an obsolete alias.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Wed, 10 Nov 2010 19:48:46 -0800 |
parents | 1d1d5d9bd884 |
children | b2e098d47dff 376148b31b5e |
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, |
106815 | 4 ;; 2005, 2006, 2007, 2008, 2009, 2010 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))) | |
106038
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
157 (setq math-2-word-size (math-power-of-2 (math-abs n))) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
158 (setq math-half-2-word-size (math-power-of-2 (1- (math-abs n)))) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
159 (calc-do-refresh) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
160 (calc-refresh-evaltos) |
40785 | 161 (if (< n 0) |
106137
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
162 (message "Binary word size is %d bits (two's complement)" (- n)) |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
163 (message "Binary word size is %d bits" n)))) |
40785 | 164 |
165 | |
166 | |
167 | |
168 | |
169 ;;; d-prefix mode commands. | |
170 | |
106038
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
171 (defun calc-radix (n &optional arg) |
40785 | 172 (interactive "NDisplay radix (2-36): ") |
173 (calc-wrapper | |
174 (if (and (>= n 2) (<= n 36)) | |
175 (progn | |
106038
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
176 (calc-change-mode |
106137
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
177 (list 'calc-number-radix 'calc-twos-complement-mode) |
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
178 (list n (and (or (= n 2) (= n 8) (= n 16)) arg)) t) |
40785 | 179 ;; also change global value so minibuffer sees it |
180 (setq-default calc-number-radix calc-number-radix)) | |
181 (setq n calc-number-radix)) | |
106137
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
182 (if calc-twos-complement-mode |
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
183 (message "Number radix is %d, two's complement mode is on." n) |
106038
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
184 (message "Number radix is %d" n)))) |
40785 | 185 |
186 (defun calc-decimal-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 10)) |
40785 | 189 |
106038
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
190 (defun calc-binary-radix (&optional arg) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
191 (interactive "P") |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
192 (calc-radix 2 arg)) |
40785 | 193 |
106137
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
194 (defun calc-octal-radix (&optional arg) |
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
195 (interactive "P") |
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
196 (calc-radix 8 arg)) |
40785 | 197 |
106137
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
198 (defun calc-hex-radix (&optional arg) |
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
199 (interactive "P") |
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
200 (calc-radix 16 arg)) |
40785 | 201 |
202 (defun calc-leading-zeros (n) | |
203 (interactive "P") | |
204 (calc-wrapper | |
205 (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
|
206 (message "Zero-padding integers to %d digits (assuming radix %d)" |
40785 | 207 (let* ((calc-internal-prec 6)) |
208 (math-compute-max-digits (math-abs calc-word-size) | |
209 calc-number-radix)) | |
210 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
|
211 (message "Omitting leading zeros on integers")))) |
40785 | 212 |
213 | |
214 (defvar math-power-of-2-cache (list 1 2 4 8 16 32 64 128 256 512 1024)) | |
215 (defvar math-big-power-of-2-cache nil) | |
216 (defun math-power-of-2 (n) ; [I I] [Public] | |
217 (if (and (natnump n) (<= n 100)) | |
218 (or (nth n math-power-of-2-cache) | |
219 (let* ((i (length math-power-of-2-cache)) | |
220 (val (nth (1- i) math-power-of-2-cache))) | |
221 (while (<= i n) | |
222 (setq val (math-mul val 2) | |
223 math-power-of-2-cache (nconc math-power-of-2-cache | |
224 (list val)) | |
225 i (1+ i))) | |
226 val)) | |
227 (let ((found (assq n math-big-power-of-2-cache))) | |
228 (if found | |
229 (cdr found) | |
230 (let ((po2 (math-ipow 2 n))) | |
231 (setq math-big-power-of-2-cache | |
232 (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
|
233 po2))))) |
40785 | 234 |
235 (defun math-integer-log2 (n) ; [I I] [Public] | |
236 (let ((i 0) | |
237 (p math-power-of-2-cache) | |
238 val) | |
239 (while (and p (Math-natnum-lessp (setq val (car p)) n)) | |
240 (setq p (cdr p) | |
241 i (1+ i))) | |
242 (if p | |
243 (and (equal val n) | |
244 i) | |
245 (while (Math-natnum-lessp | |
246 (prog1 | |
247 (setq val (math-mul val 2)) | |
248 (setq math-power-of-2-cache (nconc math-power-of-2-cache | |
249 (list val)))) | |
250 n) | |
251 (setq i (1+ i))) | |
252 (and (equal val n) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
253 i)))) |
40785 | 254 |
255 | |
256 | |
257 | |
258 ;;; Bitwise operations. | |
259 | |
260 (defun calcFunc-and (a b &optional w) ; [I I I] [Public] | |
261 (cond ((Math-messy-integerp w) | |
262 (calcFunc-and a b (math-trunc w))) | |
263 ((and w (not (integerp w))) | |
264 (math-reject-arg w 'fixnump)) | |
265 ((and (integerp a) (integerp b)) | |
266 (math-clip (logand a b) w)) | |
267 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
268 (math-binary-modulo-args 'calcFunc-and a b w)) | |
269 ((not (Math-num-integerp a)) | |
270 (math-reject-arg a 'integerp)) | |
271 ((not (Math-num-integerp b)) | |
272 (math-reject-arg b 'integerp)) | |
273 (t (math-clip (cons 'bigpos | |
274 (math-and-bignum (math-binary-arg a w) | |
275 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
276 w)))) |
40785 | 277 |
278 (defun math-binary-arg (a w) | |
279 (if (not (Math-integerp a)) | |
280 (setq a (math-trunc a))) | |
281 (if (Math-integer-negp a) | |
282 (math-not-bignum (cdr (math-bignum-test (math-sub -1 a))) | |
283 (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
|
284 (cdr (Math-bignum-test a)))) |
40785 | 285 |
286 (defun math-binary-modulo-args (f a b w) | |
287 (let (mod) | |
288 (if (eq (car-safe a) 'mod) | |
289 (progn | |
290 (setq mod (nth 2 a) | |
291 a (nth 1 a)) | |
292 (if (eq (car-safe b) 'mod) | |
293 (if (equal mod (nth 2 b)) | |
294 (setq b (nth 1 b)) | |
295 (math-reject-arg b "*Inconsistent modulos")))) | |
296 (setq mod (nth 2 b) | |
297 b (nth 1 b))) | |
298 (if (Math-messy-integerp mod) | |
299 (setq mod (math-trunc mod)) | |
300 (or (Math-integerp mod) | |
301 (math-reject-arg mod 'integerp))) | |
302 (let ((bits (math-integer-log2 mod))) | |
303 (if bits | |
304 (if w | |
305 (if (/= w bits) | |
306 (calc-record-why | |
307 "*Warning: Modulo inconsistent with word size")) | |
308 (setq w bits)) | |
309 (calc-record-why "*Warning: Modulo is not a power of 2")) | |
310 (math-make-mod (if b | |
311 (funcall f a b w) | |
312 (funcall f a w)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
313 mod)))) |
40785 | 314 |
315 (defun math-and-bignum (a b) ; [l l l] | |
316 (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
|
317 (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
|
318 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 319 (math-mul-bignum-digit (math-and-bignum (math-norm-bignum (car qa)) |
320 (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
|
321 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
|
322 (logand (cdr qa) (cdr qb)))))) |
40785 | 323 |
324 (defun calcFunc-or (a b &optional w) ; [I I I] [Public] | |
325 (cond ((Math-messy-integerp w) | |
326 (calcFunc-or a b (math-trunc w))) | |
327 ((and w (not (integerp w))) | |
328 (math-reject-arg w 'fixnump)) | |
329 ((and (integerp a) (integerp b)) | |
330 (math-clip (logior a b) w)) | |
331 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
332 (math-binary-modulo-args 'calcFunc-or a b w)) | |
333 ((not (Math-num-integerp a)) | |
334 (math-reject-arg a 'integerp)) | |
335 ((not (Math-num-integerp b)) | |
336 (math-reject-arg b 'integerp)) | |
337 (t (math-clip (cons 'bigpos | |
338 (math-or-bignum (math-binary-arg a w) | |
339 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
340 w)))) |
40785 | 341 |
342 (defun math-or-bignum (a b) ; [l l l] | |
343 (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
|
344 (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
|
345 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 346 (math-mul-bignum-digit (math-or-bignum (math-norm-bignum (car qa)) |
347 (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
|
348 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
|
349 (logior (cdr qa) (cdr qb)))))) |
40785 | 350 |
351 (defun calcFunc-xor (a b &optional w) ; [I I I] [Public] | |
352 (cond ((Math-messy-integerp w) | |
353 (calcFunc-xor a b (math-trunc w))) | |
354 ((and w (not (integerp w))) | |
355 (math-reject-arg w 'fixnump)) | |
356 ((and (integerp a) (integerp b)) | |
357 (math-clip (logxor a b) w)) | |
358 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
359 (math-binary-modulo-args 'calcFunc-xor a b w)) | |
360 ((not (Math-num-integerp a)) | |
361 (math-reject-arg a 'integerp)) | |
362 ((not (Math-num-integerp b)) | |
363 (math-reject-arg b 'integerp)) | |
364 (t (math-clip (cons 'bigpos | |
365 (math-xor-bignum (math-binary-arg a w) | |
366 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
367 w)))) |
40785 | 368 |
369 (defun math-xor-bignum (a b) ; [l l l] | |
370 (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
|
371 (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
|
372 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 373 (math-mul-bignum-digit (math-xor-bignum (math-norm-bignum (car qa)) |
374 (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
|
375 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
|
376 (logxor (cdr qa) (cdr qb)))))) |
40785 | 377 |
378 (defun calcFunc-diff (a b &optional w) ; [I I I] [Public] | |
379 (cond ((Math-messy-integerp w) | |
380 (calcFunc-diff a b (math-trunc w))) | |
381 ((and w (not (integerp w))) | |
382 (math-reject-arg w 'fixnump)) | |
383 ((and (integerp a) (integerp b)) | |
384 (math-clip (logand a (lognot b)) w)) | |
385 ((or (eq (car-safe a) 'mod) (eq (car-safe b) 'mod)) | |
386 (math-binary-modulo-args 'calcFunc-diff a b w)) | |
387 ((not (Math-num-integerp a)) | |
388 (math-reject-arg a 'integerp)) | |
389 ((not (Math-num-integerp b)) | |
390 (math-reject-arg b 'integerp)) | |
391 (t (math-clip (cons 'bigpos | |
392 (math-diff-bignum (math-binary-arg a w) | |
393 (math-binary-arg b w))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
394 w)))) |
40785 | 395 |
396 (defun math-diff-bignum (a b) ; [l l l] | |
397 (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
|
398 (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
|
399 (qb (math-div-bignum-digit b math-bignum-digit-power-of-two))) |
40785 | 400 (math-mul-bignum-digit (math-diff-bignum (math-norm-bignum (car qa)) |
401 (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
|
402 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
|
403 (logand (cdr qa) (lognot (cdr qb))))))) |
40785 | 404 |
405 (defun calcFunc-not (a &optional w) ; [I I] [Public] | |
406 (cond ((Math-messy-integerp w) | |
407 (calcFunc-not a (math-trunc w))) | |
408 ((eq (car-safe a) 'mod) | |
409 (math-binary-modulo-args 'calcFunc-not a nil w)) | |
410 ((and w (not (integerp w))) | |
411 (math-reject-arg w 'fixnump)) | |
412 ((not (Math-num-integerp a)) | |
413 (math-reject-arg a 'integerp)) | |
414 ((< (or w (setq w calc-word-size)) 0) | |
415 (math-clip (calcFunc-not a (- w)) w)) | |
416 (t (math-normalize | |
417 (cons 'bigpos | |
418 (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
|
419 w)))))) |
40785 | 420 |
421 (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
|
422 (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
|
423 (if (<= w math-bignum-logb-digit-size) |
40785 | 424 (list (logand (lognot (cdr q)) |
425 (1- (lsh 1 w)))) | |
426 (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
|
427 (- 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
|
428 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
|
429 (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
|
430 (1- math-bignum-digit-power-of-two)))))) |
40785 | 431 |
432 (defun calcFunc-lsh (a &optional n w) ; [I I] [Public] | |
433 (setq a (math-trunc a) | |
434 n (if n (math-trunc n) 1)) | |
435 (if (eq (car-safe a) 'mod) | |
436 (math-binary-modulo-args 'calcFunc-lsh a n w) | |
437 (setq w (if w (math-trunc w) calc-word-size)) | |
438 (or (integerp w) | |
439 (math-reject-arg w 'fixnump)) | |
440 (or (Math-integerp a) | |
441 (math-reject-arg a 'integerp)) | |
442 (or (Math-integerp n) | |
443 (math-reject-arg n 'integerp)) | |
444 (if (< w 0) | |
445 (math-clip (calcFunc-lsh a n (- w)) w) | |
446 (if (Math-integer-negp a) | |
447 (setq a (math-clip a w))) | |
448 (cond ((or (Math-lessp n (- w)) | |
449 (Math-lessp w n)) | |
450 0) | |
451 ((< n 0) | |
452 (math-quotient (math-clip a w) (math-power-of-2 (- n)))) | |
453 (t | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
454 (math-clip (math-mul a (math-power-of-2 n)) w)))))) |
40785 | 455 |
456 (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
|
457 (calcFunc-lsh a (math-neg (or n 1)) w)) |
40785 | 458 |
459 (defun calcFunc-ash (a &optional n w) ; [I I] [Public] | |
460 (if (or (null n) | |
461 (not (Math-negp n))) | |
462 (calcFunc-lsh a n w) | |
463 (setq a (math-trunc a) | |
464 n (if n (math-trunc n) 1)) | |
465 (if (eq (car-safe a) 'mod) | |
466 (math-binary-modulo-args 'calcFunc-ash a n w) | |
467 (setq w (if w (math-trunc w) calc-word-size)) | |
468 (or (integerp w) | |
469 (math-reject-arg w 'fixnump)) | |
470 (or (Math-integerp a) | |
471 (math-reject-arg a 'integerp)) | |
472 (or (Math-integerp n) | |
473 (math-reject-arg n 'integerp)) | |
474 (if (< w 0) | |
475 (math-clip (calcFunc-ash a n (- w)) w) | |
476 (if (Math-integer-negp a) | |
477 (setq a (math-clip a w))) | |
478 (let ((two-to-sizem1 (math-power-of-2 (1- w))) | |
479 (sh (calcFunc-lsh a n w))) | |
480 (cond ((Math-natnum-lessp a two-to-sizem1) | |
481 sh) | |
482 ((Math-lessp n (- 1 w)) | |
483 (math-add (math-mul two-to-sizem1 2) -1)) | |
484 (t (let ((two-to-n (math-power-of-2 (- n)))) | |
485 (math-add (calcFunc-lsh (math-add two-to-n -1) | |
486 (+ w n) w) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
487 sh))))))))) |
40785 | 488 |
489 (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
|
490 (calcFunc-ash a (math-neg (or n 1)) w)) |
40785 | 491 |
492 (defun calcFunc-rot (a &optional n w) ; [I I] [Public] | |
493 (setq a (math-trunc a) | |
494 n (if n (math-trunc n) 1)) | |
495 (if (eq (car-safe a) 'mod) | |
496 (math-binary-modulo-args 'calcFunc-rot a n w) | |
497 (setq w (if w (math-trunc w) calc-word-size)) | |
498 (or (integerp w) | |
499 (math-reject-arg w 'fixnump)) | |
500 (or (Math-integerp a) | |
501 (math-reject-arg a 'integerp)) | |
502 (or (Math-integerp n) | |
503 (math-reject-arg n 'integerp)) | |
504 (if (< w 0) | |
505 (math-clip (calcFunc-rot a n (- w)) w) | |
506 (if (Math-integer-negp a) | |
507 (setq a (math-clip a w))) | |
508 (cond ((or (Math-integer-negp n) | |
509 (not (Math-natnum-lessp n w))) | |
510 (calcFunc-rot a (math-mod n w) w)) | |
511 (t | |
512 (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
|
513 (calcFunc-lsh a n w))))))) |
40785 | 514 |
515 (defun math-clip (a &optional w) ; [I I] [Public] | |
516 (cond ((Math-messy-integerp w) | |
517 (math-clip a (math-trunc w))) | |
518 ((eq (car-safe a) 'mod) | |
519 (math-binary-modulo-args 'math-clip a nil w)) | |
520 ((and w (not (integerp w))) | |
521 (math-reject-arg w 'fixnump)) | |
522 ((not (Math-num-integerp a)) | |
523 (math-reject-arg a 'integerp)) | |
524 ((< (or w (setq w calc-word-size)) 0) | |
525 (setq a (math-clip a (- w))) | |
526 (if (Math-natnum-lessp a (math-power-of-2 (- -1 w))) | |
527 a | |
528 (math-sub a (math-power-of-2 (- w))))) | |
529 ((Math-negp a) | |
530 (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
|
531 ((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
|
532 (if (> w (logb math-small-integer-size)) |
40785 | 533 a |
534 (logand a (1- (lsh 1 w))))) | |
535 (t | |
536 (math-normalize | |
537 (cons 'bigpos | |
538 (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
|
539 w)))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
540 |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
541 (defalias 'calcFunc-clip 'math-clip) |
40785 | 542 |
543 (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
|
544 (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
|
545 (if (<= w math-bignum-logb-digit-size) |
40785 | 546 (list (logand (cdr q) |
547 (1- (lsh 1 w)))) | |
548 (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
|
549 (- 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
|
550 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
|
551 (cdr q))))) |
40785 | 552 |
553 (defvar math-max-digits-cache nil) | |
554 (defun math-compute-max-digits (w r) | |
555 (let* ((pair (+ (* r 100000) w)) | |
556 (res (assq pair math-max-digits-cache))) | |
557 (if res | |
558 (cdr res) | |
559 (let* ((calc-command-flags nil) | |
560 (digs (math-ceiling (math-div w (math-real-log2 r))))) | |
561 (setq math-max-digits-cache (cons (cons pair digs) | |
562 math-max-digits-cache)) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
563 digs)))) |
40785 | 564 |
565 (defvar math-log2-cache (list '(2 . 1) | |
566 '(4 . 2) | |
567 '(8 . 3) | |
568 '(10 . (float 332193 -5)) | |
569 '(16 . 4) | |
570 '(32 . 5))) | |
571 (defun math-real-log2 (x) ;;; calc-internal-prec must be 6 | |
572 (let ((res (assq x math-log2-cache))) | |
573 (if res | |
574 (cdr res) | |
575 (let* ((calc-symbolic-mode nil) | |
576 (calc-display-working-message nil) | |
577 (log (calcFunc-log x 2))) | |
578 (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
|
579 log)))) |
40785 | 580 |
581 (defconst math-radix-digits ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" | |
582 "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" | |
583 "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" | |
584 "U" "V" "W" "X" "Y" "Z"]) | |
585 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
586 (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
|
587 (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
|
588 |
40785 | 589 (defun math-format-radix (a) ; [X S] |
590 (if (< a calc-number-radix) | |
591 (if (< a 0) | |
592 (concat "-" (math-format-radix (- a))) | |
593 (math-format-radix-digit a)) | |
594 (let ((s "")) | |
595 (while (> a 0) | |
596 (setq s (concat (math-format-radix-digit (% a calc-number-radix)) s) | |
597 a (/ a calc-number-radix))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
598 s))) |
40785 | 599 |
600 (defconst math-binary-digits ["000" "001" "010" "011" | |
601 "100" "101" "110" "111"]) | |
602 (defun math-format-binary (a) ; [X S] | |
603 (if (< a 8) | |
604 (if (< a 0) | |
605 (concat "-" (math-format-binary (- a))) | |
606 (math-format-radix a)) | |
607 (let ((s "")) | |
608 (while (> a 7) | |
609 (setq s (concat (aref math-binary-digits (% a 8)) s) | |
610 a (/ a 8))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
611 (concat (math-format-radix a) s)))) |
40785 | 612 |
613 (defun math-format-bignum-radix (a) ; [X L] | |
614 (cond ((null a) "0") | |
615 ((and (null (cdr a)) | |
616 (< (car a) calc-number-radix)) | |
617 (math-format-radix-digit (car a))) | |
618 (t | |
619 (let ((q (math-div-bignum-digit a calc-number-radix))) | |
620 (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
|
621 (math-format-radix-digit (cdr q))))))) |
40785 | 622 |
623 (defun math-format-bignum-binary (a) ; [X L] | |
624 (cond ((null a) "0") | |
625 ((null (cdr a)) | |
626 (math-format-binary (car a))) | |
627 (t | |
628 (let ((q (math-div-bignum-digit a 512))) | |
629 (concat (math-format-bignum-binary (math-norm-bignum (car q))) | |
630 (aref math-binary-digits (/ (cdr q) 64)) | |
631 (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
|
632 (aref math-binary-digits (% (cdr q) 8))))))) |
40785 | 633 |
634 (defun math-format-bignum-octal (a) ; [X L] | |
635 (cond ((null a) "0") | |
636 ((null (cdr a)) | |
637 (math-format-radix (car a))) | |
638 (t | |
639 (let ((q (math-div-bignum-digit a 512))) | |
640 (concat (math-format-bignum-octal (math-norm-bignum (car q))) | |
641 (math-format-radix-digit (/ (cdr q) 64)) | |
642 (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
|
643 (math-format-radix-digit (% (cdr q) 8))))))) |
40785 | 644 |
645 (defun math-format-bignum-hex (a) ; [X L] | |
646 (cond ((null a) "0") | |
647 ((null (cdr a)) | |
648 (math-format-radix (car a))) | |
649 (t | |
650 (let ((q (math-div-bignum-digit a 256))) | |
651 (concat (math-format-bignum-hex (math-norm-bignum (car q))) | |
652 (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
|
653 (math-format-radix-digit (% (cdr q) 16))))))) |
40785 | 654 |
655 ;;; Decompose into integer and fractional parts, without depending | |
656 ;;; on calc-internal-prec. | |
657 (defun math-float-parts (a need-frac) ; returns ( int frac fracdigs ) | |
658 (if (>= (nth 2 a) 0) | |
659 (list (math-scale-rounding (nth 1 a) (nth 2 a)) '(float 0 0) 0) | |
660 (let* ((d (math-numdigs (nth 1 a))) | |
661 (n (- (nth 2 a)))) | |
662 (if need-frac | |
663 (if (>= n d) | |
664 (list 0 a n) | |
665 (let ((qr (math-idivmod (nth 1 a) (math-scale-int 1 n)))) | |
666 (list (car qr) (math-make-float (cdr qr) (- n)) n))) | |
667 (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
|
668 '(float 0 0) 0))))) |
40785 | 669 |
670 (defun math-format-radix-float (a prec) | |
671 (let ((fmt (car calc-float-format)) | |
672 (figs (nth 1 calc-float-format)) | |
673 (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
|
674 (str nil) |
1c7dfa5d5967
(math-format-radix-float): Make pos a local variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
675 pos) |
40785 | 676 (if (eq fmt 'fix) |
677 (let* ((afigs (math-abs figs)) | |
678 (fp (math-float-parts a (> afigs 0))) | |
679 (calc-internal-prec (+ 3 (max (nth 2 fp) | |
680 (math-convert-radix-digits | |
681 afigs t)))) | |
682 (int (car fp)) | |
683 (frac (math-round (math-mul (math-normalize (nth 1 fp)) | |
684 (math-radix-float-power afigs))))) | |
685 (if (not (and (math-zerop frac) (math-zerop int) (< figs 0))) | |
686 (let ((math-radix-explicit-format nil)) | |
687 (let ((calc-group-digits nil)) | |
688 (setq str (if (> afigs 0) (math-format-number frac) "")) | |
689 (if (< (length str) afigs) | |
690 (setq str (concat (make-string (- afigs (length str)) ?0) | |
691 str)) | |
692 (if (> (length str) afigs) | |
693 (setq str (substring str 1) | |
694 int (math-add int 1)))) | |
695 (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
|
696 (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
|
697 (setq str (math-group-float str)))) |
40785 | 698 (setq figs 0)))) |
699 (or str | |
700 (let* ((prec calc-internal-prec) | |
701 (afigs (if (> figs 0) | |
702 figs | |
703 (max 1 (+ figs | |
704 (1- (math-convert-radix-digits | |
705 (max prec | |
706 (math-numdigs (nth 1 a))))))))) | |
707 (calc-internal-prec (+ 3 (math-convert-radix-digits afigs t))) | |
708 (explo -1) (vlo (math-radix-float-power explo)) | |
709 (exphi 1) (vhi (math-radix-float-power exphi)) | |
710 expmid vmid eadj) | |
711 (setq a (math-normalize a)) | |
712 (if (Math-zerop a) | |
713 (setq explo 0) | |
714 (if (math-lessp-float '(float 1 0) a) | |
715 (while (not (math-lessp-float a vhi)) | |
716 (setq explo exphi vlo vhi | |
717 exphi (math-mul exphi 2) | |
718 vhi (math-radix-float-power exphi))) | |
719 (while (math-lessp-float a vlo) | |
720 (setq exphi explo vhi vlo | |
721 explo (math-mul explo 2) | |
722 vlo (math-radix-float-power explo)))) | |
723 (while (not (eq (math-sub exphi explo) 1)) | |
724 (setq expmid (math-div2 (math-add explo exphi)) | |
725 vmid (math-radix-float-power expmid)) | |
726 (if (math-lessp-float a vmid) | |
727 (setq exphi expmid vhi vmid) | |
728 (setq explo expmid vlo vmid))) | |
729 (setq a (math-div-float a vlo))) | |
730 (let* ((sc (math-round (math-mul a (math-radix-float-power | |
731 (1- afigs))))) | |
732 (math-radix-explicit-format nil)) | |
733 (let ((calc-group-digits nil)) | |
734 (setq str (math-format-number sc)))) | |
735 (if (> (length str) afigs) | |
736 (setq str (substring str 0 -1) | |
737 explo (1+ explo))) | |
738 (if (and (eq fmt 'float) | |
739 (math-lessp explo (+ (if (= figs 0) | |
740 (1- (math-convert-radix-digits | |
741 prec)) | |
742 afigs) | |
743 calc-display-sci-high 1)) | |
744 (math-lessp calc-display-sci-low explo)) | |
745 (let ((dpos (1+ explo))) | |
746 (cond ((<= dpos 0) | |
747 (setq str (concat "0" point (make-string (- dpos) ?0) | |
748 str))) | |
749 ((> dpos (length str)) | |
750 (setq str (concat str (make-string (- dpos (length str)) | |
751 ?0) point))) | |
752 (t | |
753 (setq str (concat (substring str 0 dpos) point | |
754 (substring str dpos))))) | |
755 (setq explo nil)) | |
756 (setq eadj (if (eq fmt 'eng) | |
757 (min (math-mod explo 3) (length str)) | |
758 0) | |
759 str (concat (substring str 0 (1+ eadj)) point | |
760 (substring str (1+ eadj))))) | |
761 (setq pos (length str)) | |
762 (while (eq (aref str (1- pos)) ?0) (setq pos (1- pos))) | |
763 (and explo (eq (aref str (1- pos)) ?.) (setq pos (1- pos))) | |
764 (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
|
765 (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
|
766 (setq str (math-group-float str))) |
40785 | 767 (if explo |
768 (let ((estr (let ((calc-number-radix 10) | |
769 (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
|
770 (math-format-number |
1c7dfa5d5967
(math-format-radix-float): Make pos a local variable.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
771 (math-sub explo eadj))))) |
40785 | 772 (setq str (if (or (memq calc-language '(math maple)) |
773 (> calc-number-radix 14)) | |
774 (format "%s*%d.^%s" str calc-number-radix estr) | |
775 (format "%se%s" str estr))))))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
776 str)) |
40785 | 777 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
778 (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
|
779 |
40785 | 780 (defun math-convert-radix-digits (n &optional to-dec) |
781 (let ((key (cons n (cons to-dec calc-number-radix)))) | |
782 (or (cdr (assoc key math-radix-digits-cache)) | |
783 (let* ((calc-internal-prec 6) | |
784 (log (math-div (math-real-log2 calc-number-radix) | |
785 '(float 332193 -5)))) | |
786 (cdr (car (setq math-radix-digits-cache | |
787 (cons (cons key (math-ceiling (if to-dec | |
788 (math-mul n log) | |
789 (math-div n log)))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
790 math-radix-digits-cache)))))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
791 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41042
diff
changeset
|
792 (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
|
793 (defvar math-radix-float-cache) |
40785 | 794 |
795 (defun math-radix-float-power (n) | |
796 (if (eq n 0) | |
797 '(float 1 0) | |
798 (or (and (eq calc-number-radix (car math-radix-float-cache-tag)) | |
799 (<= calc-internal-prec (cdr math-radix-float-cache-tag))) | |
800 (setq math-radix-float-cache-tag (cons calc-number-radix | |
801 calc-internal-prec) | |
802 math-radix-float-cache nil)) | |
803 (math-normalize | |
804 (or (cdr (assoc n math-radix-float-cache)) | |
805 (cdr (car (setq math-radix-float-cache | |
806 (cons (cons | |
807 n | |
808 (let ((calc-internal-prec | |
809 (cdr math-radix-float-cache-tag))) | |
810 (if (math-negp n) | |
811 (math-div-float '(float 1 0) | |
812 (math-radix-float-power | |
813 (math-neg n))) | |
814 (math-mul-float (math-sqr-float | |
815 (math-radix-float-power | |
816 (math-div2 n))) | |
817 (if (math-evenp n) | |
818 '(float 1 0) | |
819 (math-float | |
820 calc-number-radix)))))) | |
41042
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
821 math-radix-float-cache)))))))) |
a78b609cb4b1
(calcFunc-clip): Use `defalias' instead of `fset' and
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
822 |
106137
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
823 ;;; Two's complement mode |
106038
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
824 |
106137
0e720bab47c6
(math-format-twos-complement): Renamed from math-format-complement-signed.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106068
diff
changeset
|
825 (defun math-format-twos-complement (a) |
106148
3875adeabec5
(calc-octal-radix, calc-hex-radix): Add spec.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
106137
diff
changeset
|
826 "Format an integer in two's complement mode." |
106038
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
827 (let* (;(calc-leading-zeros t) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
828 (overflow nil) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
829 (negative nil) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
830 (num |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
831 (cond |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
832 ((or (eq a 0) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
833 (and (Math-integer-posp a))) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
834 (if (integerp a) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
835 (math-format-radix a) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
836 (math-format-bignum-radix (cdr a)))) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
837 ((Math-integer-negp a) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
838 (let ((newa (math-add a math-2-word-size))) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
839 (if (integerp newa) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
840 (math-format-radix newa) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
841 (math-format-bignum-radix (cdr newa)))))))) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
842 (let* ((calc-internal-prec 6) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
843 (digs (math-compute-max-digits (math-abs calc-word-size) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
844 calc-number-radix)) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
845 (len (length num))) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
846 (if (< len digs) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
847 (setq num (concat (make-string (- digs len) ?0) num)))) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
848 (concat |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
849 (number-to-string calc-number-radix) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
850 "##" |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
851 num))) |
9ddf1eafde9a
(calc-word-size): Reset the variables `math-2-word-size' and `math-half-2-word-size'.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
100908
diff
changeset
|
852 |
58648
617cbf064b4b
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58553
diff
changeset
|
853 (provide 'calc-bin) |
40785 | 854 |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
855 ;; 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
|
856 ;;; calc-bin.el ends here |