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