Mercurial > emacs
annotate lisp/obsolete/float.el @ 94344:2005e38cd431
*** empty log message ***
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Fri, 25 Apr 2008 09:51:03 +0000 |
parents | ee22366f2a68 |
children | 43d30a1ea764 |
rev | line source |
---|---|
64758
1b36b4662ec6
Remove spurious character.
Juanma Barranquero <lekktu@gmail.com>
parents:
64751
diff
changeset
|
1 ;;; float.el --- obsolete floating point arithmetic package |
51342 | 2 |
74509 | 3 ;; Copyright (C) 1986, 2001, 2002, 2003, 2004, 2005, |
79715 | 4 ;; 2006, 2007, 2008 Free Software Foundation, Inc. |
51342 | 5 |
6 ;; Author: Bill Rosenblatt | |
7 ;; Maintainer: FSF | |
8 ;; Keywords: extensions | |
9 | |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
78228
1e016d63f292
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75347
diff
changeset
|
14 ;; the Free Software Foundation; either version 3, or (at your option) |
51342 | 15 ;; any later version. |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
51342 | 26 |
27 ;;; Commentary: | |
28 | |
94000
ee22366f2a68
Add a comment giving version of obsolescence.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
29 ;; This file has been obsolete since Emacs 22.1. |
ee22366f2a68
Add a comment giving version of obsolescence.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
30 |
51342 | 31 ;; Floating point numbers are represented by dot-pairs (mant . exp) |
32 ;; where mant is the 24-bit signed integral mantissa and exp is the | |
33 ;; base 2 exponent. | |
34 ;; | |
35 ;; Emacs LISP supports a 24-bit signed integer data type, which has a | |
36 ;; range of -(2**23) to +(2**23)-1, or -8388608 to 8388607 decimal. | |
37 ;; This gives six significant decimal digit accuracy. Exponents can | |
38 ;; be anything in the range -(2**23) to +(2**23)-1. | |
39 ;; | |
40 ;; User interface: | |
41 ;; function f converts from integer to floating point | |
42 ;; function string-to-float converts from string to floating point | |
43 ;; function fint converts a floating point to integer (with truncation) | |
44 ;; function float-to-string converts from floating point to string | |
45 ;; | |
46 ;; Caveats: | |
47 ;; - Exponents outside of the range of +/-100 or so will cause certain | |
48 ;; functions (especially conversion routines) to take forever. | |
49 ;; - Very little checking is done for fixed point overflow/underflow. | |
50 ;; - No checking is done for over/underflow of the exponent | |
51 ;; (hardly necessary when exponent can be 2**23). | |
52 ;; | |
53 ;; | |
54 ;; Bill Rosenblatt | |
55 ;; June 20, 1986 | |
56 ;; | |
57 | |
58 ;;; Code: | |
59 | |
60 ;; fundamental implementation constants | |
61 (defconst exp-base 2 | |
62 "Base of exponent in this floating point representation.") | |
63 | |
64 (defconst mantissa-bits 24 | |
65 "Number of significant bits in this floating point representation.") | |
66 | |
67 (defconst decimal-digits 6 | |
68 "Number of decimal digits expected to be accurate.") | |
69 | |
70 (defconst expt-digits 2 | |
71 "Maximum permitted digits in a scientific notation exponent.") | |
72 | |
73 ;; other constants | |
74 (defconst maxbit (1- mantissa-bits) | |
75 "Number of highest bit") | |
76 | |
77 (defconst mantissa-maxval (1- (ash 1 maxbit)) | |
78 "Maximum permissible value of mantissa") | |
79 | |
80 (defconst mantissa-minval (ash 1 maxbit) | |
81 "Minimum permissible value of mantissa") | |
82 | |
83 (defconst floating-point-regexp | |
84 "^[ \t]*\\(-?\\)\\([0-9]*\\)\ | |
85 \\(\\.\\([0-9]*\\)\\|\\)\ | |
86 \\(\\(\\([Ee]\\)\\(-?\\)\\([0-9][0-9]*\\)\\)\\|\\)[ \t]*$" | |
87 "Regular expression to match floating point numbers. Extract matches: | |
88 1 - minus sign | |
89 2 - integer part | |
90 4 - fractional part | |
91 8 - minus sign for power of ten | |
92 9 - power of ten | |
93 ") | |
94 | |
95 (defconst high-bit-mask (ash 1 maxbit) | |
96 "Masks all bits except the high-order (sign) bit.") | |
97 | |
98 (defconst second-bit-mask (ash 1 (1- maxbit)) | |
99 "Masks all bits except the highest-order magnitude bit") | |
100 | |
101 ;; various useful floating point constants | |
102 (defconst _f0 '(0 . 1)) | |
103 | |
104 (defconst _f1/2 '(4194304 . -23)) | |
105 | |
106 (defconst _f1 '(4194304 . -22)) | |
107 | |
108 (defconst _f10 '(5242880 . -19)) | |
109 | |
110 ;; support for decimal conversion routines | |
111 (defvar powers-of-10 (make-vector (1+ decimal-digits) _f1)) | |
112 (aset powers-of-10 1 _f10) | |
113 (aset powers-of-10 2 '(6553600 . -16)) | |
114 (aset powers-of-10 3 '(8192000 . -13)) | |
115 (aset powers-of-10 4 '(5120000 . -9)) | |
116 (aset powers-of-10 5 '(6400000 . -6)) | |
117 (aset powers-of-10 6 '(8000000 . -3)) | |
118 | |
119 (defconst all-decimal-digs-minval (aref powers-of-10 (1- decimal-digits))) | |
120 (defconst highest-power-of-10 (aref powers-of-10 decimal-digits)) | |
121 | |
122 (defun fashl (fnum) ; floating-point arithmetic shift left | |
123 (cons (ash (car fnum) 1) (1- (cdr fnum)))) | |
124 | |
125 (defun fashr (fnum) ; floating point arithmetic shift right | |
126 (cons (ash (car fnum) -1) (1+ (cdr fnum)))) | |
127 | |
128 (defun normalize (fnum) | |
129 (if (> (car fnum) 0) ; make sure next-to-highest bit is set | |
130 (while (zerop (logand (car fnum) second-bit-mask)) | |
131 (setq fnum (fashl fnum))) | |
132 (if (< (car fnum) 0) ; make sure highest bit is set | |
133 (while (zerop (logand (car fnum) high-bit-mask)) | |
134 (setq fnum (fashl fnum))) | |
135 (setq fnum _f0))) ; "standard 0" | |
136 fnum) | |
137 | |
138 (defun abs (n) ; integer absolute value | |
139 (if (>= n 0) n (- n))) | |
140 | |
141 (defun fabs (fnum) ; re-normalize after taking abs value | |
142 (normalize (cons (abs (car fnum)) (cdr fnum)))) | |
143 | |
144 (defun xor (a b) ; logical exclusive or | |
145 (and (or a b) (not (and a b)))) | |
146 | |
147 (defun same-sign (a b) ; two f-p numbers have same sign? | |
148 (not (xor (natnump (car a)) (natnump (car b))))) | |
149 | |
150 (defun extract-match (str i) ; used after string-match | |
151 (condition-case () | |
152 (substring str (match-beginning i) (match-end i)) | |
153 (error ""))) | |
154 | |
155 ;; support for the multiplication function | |
156 (defconst halfword-bits (/ mantissa-bits 2)) ; bits in a halfword | |
157 (defconst masklo (1- (ash 1 halfword-bits))) ; isolate the lower halfword | |
158 (defconst maskhi (lognot masklo)) ; isolate the upper halfword | |
159 (defconst round-limit (ash 1 (/ halfword-bits 2))) | |
160 | |
161 (defun hihalf (n) ; return high halfword, shifted down | |
162 (ash (logand n maskhi) (- halfword-bits))) | |
163 | |
164 (defun lohalf (n) ; return low halfword | |
165 (logand n masklo)) | |
166 | |
167 ;; Visible functions | |
168 | |
169 ;; Arithmetic functions | |
170 (defun f+ (a1 a2) | |
171 "Returns the sum of two floating point numbers." | |
172 (let ((f1 (fmax a1 a2)) | |
173 (f2 (fmin a1 a2))) | |
174 (if (same-sign a1 a2) | |
175 (setq f1 (fashr f1) ; shift right to avoid overflow | |
176 f2 (fashr f2))) | |
177 (normalize | |
178 (cons (+ (car f1) (ash (car f2) (- (cdr f2) (cdr f1)))) | |
179 (cdr f1))))) | |
180 | |
181 (defun f- (a1 &optional a2) ; unary or binary minus | |
182 "Returns the difference of two floating point numbers." | |
183 (if a2 | |
184 (f+ a1 (f- a2)) | |
185 (normalize (cons (- (car a1)) (cdr a1))))) | |
186 | |
187 (defun f* (a1 a2) ; multiply in halfword chunks | |
188 "Returns the product of two floating point numbers." | |
189 (let* ((i1 (car (fabs a1))) | |
190 (i2 (car (fabs a2))) | |
191 (sign (not (same-sign a1 a2))) | |
192 (prodlo (+ (hihalf (* (lohalf i1) (lohalf i2))) | |
193 (lohalf (* (hihalf i1) (lohalf i2))) | |
194 (lohalf (* (lohalf i1) (hihalf i2))))) | |
195 (prodhi (+ (* (hihalf i1) (hihalf i2)) | |
196 (hihalf (* (hihalf i1) (lohalf i2))) | |
197 (hihalf (* (lohalf i1) (hihalf i2))) | |
198 (hihalf prodlo)))) | |
199 (if (> (lohalf prodlo) round-limit) | |
200 (setq prodhi (1+ prodhi))) ; round off truncated bits | |
201 (normalize | |
202 (cons (if sign (- prodhi) prodhi) | |
203 (+ (cdr (fabs a1)) (cdr (fabs a2)) mantissa-bits))))) | |
204 | |
205 (defun f/ (a1 a2) ; SLOW subtract-and-shift algorithm | |
206 "Returns the quotient of two floating point numbers." | |
207 (if (zerop (car a2)) ; if divide by 0 | |
208 (signal 'arith-error (list "attempt to divide by zero" a1 a2)) | |
209 (let ((bits (1- maxbit)) | |
210 (quotient 0) | |
211 (dividend (car (fabs a1))) | |
212 (divisor (car (fabs a2))) | |
213 (sign (not (same-sign a1 a2)))) | |
214 (while (natnump bits) | |
215 (if (< (- dividend divisor) 0) | |
216 (setq quotient (ash quotient 1)) | |
217 (setq quotient (1+ (ash quotient 1)) | |
218 dividend (- dividend divisor))) | |
219 (setq dividend (ash dividend 1) | |
220 bits (1- bits))) | |
221 (normalize | |
222 (cons (if sign (- quotient) quotient) | |
223 (- (cdr (fabs a1)) (cdr (fabs a2)) (1- maxbit))))))) | |
224 | |
225 (defun f% (a1 a2) | |
226 "Returns the remainder of first floating point number divided by second." | |
227 (f- a1 (f* (ftrunc (f/ a1 a2)) a2))) | |
228 | |
229 | |
230 ;; Comparison functions | |
231 (defun f= (a1 a2) | |
232 "Returns t if two floating point numbers are equal, nil otherwise." | |
233 (equal a1 a2)) | |
234 | |
235 (defun f> (a1 a2) | |
236 "Returns t if first floating point number is greater than second, | |
237 nil otherwise." | |
238 (cond ((and (natnump (car a1)) (< (car a2) 0)) | |
239 t) ; a1 nonnegative, a2 negative | |
240 ((and (> (car a1) 0) (<= (car a2) 0)) | |
241 t) ; a1 positive, a2 nonpositive | |
242 ((and (<= (car a1) 0) (natnump (car a2))) | |
243 nil) ; a1 nonpos, a2 nonneg | |
244 ((/= (cdr a1) (cdr a2)) ; same signs. exponents differ | |
245 (> (cdr a1) (cdr a2))) ; compare the mantissas. | |
246 (t | |
247 (> (car a1) (car a2))))) ; same exponents. | |
248 | |
249 (defun f>= (a1 a2) | |
250 "Returns t if first floating point number is greater than or equal to | |
251 second, nil otherwise." | |
252 (or (f> a1 a2) (f= a1 a2))) | |
253 | |
254 (defun f< (a1 a2) | |
255 "Returns t if first floating point number is less than second, | |
256 nil otherwise." | |
257 (not (f>= a1 a2))) | |
258 | |
259 (defun f<= (a1 a2) | |
260 "Returns t if first floating point number is less than or equal to | |
261 second, nil otherwise." | |
262 (not (f> a1 a2))) | |
263 | |
264 (defun f/= (a1 a2) | |
265 "Returns t if first floating point number is not equal to second, | |
266 nil otherwise." | |
267 (not (f= a1 a2))) | |
268 | |
269 (defun fmin (a1 a2) | |
270 "Returns the minimum of two floating point numbers." | |
271 (if (f< a1 a2) a1 a2)) | |
272 | |
273 (defun fmax (a1 a2) | |
274 "Returns the maximum of two floating point numbers." | |
275 (if (f> a1 a2) a1 a2)) | |
276 | |
277 (defun fzerop (fnum) | |
278 "Returns t if the floating point number is zero, nil otherwise." | |
279 (= (car fnum) 0)) | |
280 | |
281 (defun floatp (fnum) | |
282 "Returns t if the arg is a floating point number, nil otherwise." | |
283 (and (consp fnum) (integerp (car fnum)) (integerp (cdr fnum)))) | |
284 | |
285 ;; Conversion routines | |
286 (defun f (int) | |
287 "Convert the integer argument to floating point, like a C cast operator." | |
288 (normalize (cons int '0))) | |
289 | |
290 (defun int-to-hex-string (int) | |
291 "Convert the integer argument to a C-style hexadecimal string." | |
292 (let ((shiftval -20) | |
293 (str "0x") | |
294 (hex-chars "0123456789ABCDEF")) | |
295 (while (<= shiftval 0) | |
296 (setq str (concat str (char-to-string | |
297 (aref hex-chars | |
298 (logand (lsh int shiftval) 15)))) | |
299 shiftval (+ shiftval 4))) | |
300 str)) | |
301 | |
302 (defun ftrunc (fnum) ; truncate fractional part | |
303 "Truncate the fractional part of a floating point number." | |
304 (cond ((natnump (cdr fnum)) ; it's all integer, return number as is | |
305 fnum) | |
306 ((<= (cdr fnum) (- maxbit)) ; it's all fractional, return 0 | |
307 '(0 . 1)) | |
308 (t ; otherwise mask out fractional bits | |
309 (let ((mant (car fnum)) (exp (cdr fnum))) | |
310 (normalize | |
311 (cons (if (natnump mant) ; if negative, use absolute value | |
312 (ash (ash mant exp) (- exp)) | |
313 (- (ash (ash (- mant) exp) (- exp)))) | |
314 exp)))))) | |
315 | |
316 (defun fint (fnum) ; truncate and convert to integer | |
317 "Convert the floating point number to integer, with truncation, | |
318 like a C cast operator." | |
319 (let* ((tf (ftrunc fnum)) (tint (car tf)) (texp (cdr tf))) | |
320 (cond ((>= texp mantissa-bits) ; too high, return "maxint" | |
321 mantissa-maxval) | |
322 ((<= texp (- mantissa-bits)) ; too low, return "minint" | |
323 mantissa-minval) | |
324 (t ; in range | |
325 (ash tint texp))))) ; shift so that exponent is 0 | |
326 | |
327 (defun float-to-string (fnum &optional sci) | |
328 "Convert the floating point number to a decimal string. | |
329 Optional second argument non-nil means use scientific notation." | |
330 (let* ((value (fabs fnum)) (sign (< (car fnum) 0)) | |
331 (power 0) (result 0) (str "") | |
332 (temp 0) (pow10 _f1)) | |
333 | |
334 (if (f= fnum _f0) | |
335 "0" | |
336 (if (f>= value _f1) ; find largest power of 10 <= value | |
337 (progn ; value >= 1, power is positive | |
338 (while (f<= (setq temp (f* pow10 highest-power-of-10)) value) | |
339 (setq pow10 temp | |
340 power (+ power decimal-digits))) | |
341 (while (f<= (setq temp (f* pow10 _f10)) value) | |
342 (setq pow10 temp | |
343 power (1+ power)))) | |
344 (progn ; value < 1, power is negative | |
345 (while (f> (setq temp (f/ pow10 highest-power-of-10)) value) | |
346 (setq pow10 temp | |
347 power (- power decimal-digits))) | |
348 (while (f> pow10 value) | |
349 (setq pow10 (f/ pow10 _f10) | |
350 power (1- power))))) | |
351 ; get value in range 100000 to 999999 | |
352 (setq value (f* (f/ value pow10) all-decimal-digs-minval) | |
353 result (ftrunc value)) | |
354 (let (int) | |
355 (if (f> (f- value result) _f1/2) ; round up if remainder > 0.5 | |
356 (setq int (1+ (fint result))) | |
357 (setq int (fint result))) | |
358 (setq str (int-to-string int)) | |
359 (if (>= int 1000000) | |
360 (setq power (1+ power)))) | |
361 | |
362 (if sci ; scientific notation | |
363 (setq str (concat (substring str 0 1) "." (substring str 1) | |
364 "E" (int-to-string power))) | |
365 | |
366 ; regular decimal string | |
367 (cond ((>= power (1- decimal-digits)) | |
368 ; large power, append zeroes | |
369 (let ((zeroes (- power decimal-digits))) | |
370 (while (natnump zeroes) | |
371 (setq str (concat str "0") | |
372 zeroes (1- zeroes))))) | |
373 | |
374 ; negative power, prepend decimal | |
375 ((< power 0) ; point and zeroes | |
376 (let ((zeroes (- (- power) 2))) | |
377 (while (natnump zeroes) | |
378 (setq str (concat "0" str) | |
379 zeroes (1- zeroes))) | |
380 (setq str (concat "0." str)))) | |
381 | |
382 (t ; in range, insert decimal point | |
383 (setq str (concat | |
384 (substring str 0 (1+ power)) | |
385 "." | |
386 (substring str (1+ power))))))) | |
387 | |
388 (if sign ; if negative, prepend minus sign | |
389 (concat "-" str) | |
390 str)))) | |
391 | |
392 | |
393 ;; string to float conversion. | |
394 ;; accepts scientific notation, but ignores anything after the first two | |
395 ;; digits of the exponent. | |
396 (defun string-to-float (str) | |
397 "Convert the string to a floating point number. | |
398 Accepts a decimal string in scientific notation, with exponent preceded | |
399 by either E or e. Only the six most significant digits of the integer | |
400 and fractional parts are used; only the first two digits of the exponent | |
401 are used. Negative signs preceding both the decimal number and the exponent | |
402 are recognized." | |
403 | |
404 (if (string-match floating-point-regexp str 0) | |
405 (let (power) | |
406 (f* | |
407 ; calculate the mantissa | |
408 (let* ((int-subst (extract-match str 2)) | |
409 (fract-subst (extract-match str 4)) | |
410 (digit-string (concat int-subst fract-subst)) | |
411 (mant-sign (equal (extract-match str 1) "-")) | |
412 (leading-0s 0) (round-up nil)) | |
413 | |
414 ; get rid of leading 0's | |
415 (setq power (- (length int-subst) decimal-digits)) | |
416 (while (and (< leading-0s (length digit-string)) | |
417 (= (aref digit-string leading-0s) ?0)) | |
418 (setq leading-0s (1+ leading-0s))) | |
419 (setq power (- power leading-0s) | |
420 digit-string (substring digit-string leading-0s)) | |
421 | |
422 ; if more than 6 digits, round off | |
423 (if (> (length digit-string) decimal-digits) | |
424 (setq round-up (>= (aref digit-string decimal-digits) ?5) | |
425 digit-string (substring digit-string 0 decimal-digits)) | |
426 (setq power (+ power (- decimal-digits (length digit-string))))) | |
427 | |
428 ; round up and add minus sign, if necessary | |
62194
dda44460de33
(string-to-float): Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
429 (f (* (+ (string-to-number digit-string) |
51342 | 430 (if round-up 1 0)) |
431 (if mant-sign -1 1)))) | |
432 | |
433 ; calculate the exponent (power of ten) | |
434 (let* ((expt-subst (extract-match str 9)) | |
435 (expt-sign (equal (extract-match str 8) "-")) | |
436 (expt 0) (chunks 0) (tens 0) (exponent _f1) | |
437 (func 'f*)) | |
438 | |
62194
dda44460de33
(string-to-float): Replace `string-to-int' by `string-to-number'.
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
439 (setq expt (+ (* (string-to-number |
51342 | 440 (substring expt-subst 0 |
441 (min expt-digits (length expt-subst)))) | |
442 (if expt-sign -1 1)) | |
443 power)) | |
444 (if (< expt 0) ; if power of 10 negative | |
445 (setq expt (- expt) ; take abs val of exponent | |
446 func 'f/)) ; and set up to divide, not multiply | |
447 | |
448 (setq chunks (/ expt decimal-digits) | |
449 tens (% expt decimal-digits)) | |
450 ; divide or multiply by "chunks" of 10**6 | |
451 (while (> chunks 0) | |
452 (setq exponent (funcall func exponent highest-power-of-10) | |
453 chunks (1- chunks))) | |
454 ; divide or multiply by remaining power of ten | |
455 (funcall func exponent (aref powers-of-10 tens))))) | |
456 | |
457 _f0)) ; if invalid, return 0 | |
458 | |
459 (provide 'float) | |
460 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
79715
diff
changeset
|
461 ;; arch-tag: cc0c89c6-5718-49af-978e-585f6b14e347 |
51342 | 462 ;;; float.el ends here |