author | Claus Gittinger <cg@exept.de> |
Tue, 17 Jun 2003 16:35:41 +0200 | |
changeset 7405 | 9b0334a4591b |
parent 7403 | 857702a5a921 |
child 7418 | 521174de046d |
permissions | -rw-r--r-- |
55 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
213 | 3 |
All Rights Reserved |
55 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
14 |
|
55 | 15 |
Number subclass:#LimitedPrecisionReal |
1200 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Magnitude-Numbers' |
|
55 | 20 |
! |
21 |
||
88 | 22 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
213 | 27 |
All Rights Reserved |
55 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
55 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
7405 | 40 |
Abstract superclass for any-precision floating point numbers (i.e. Float and Double). |
92 | 41 |
|
42 |
Due to historic reasons, ST/X's Floats are what Doubles are in ST-80. |
|
43 |
This may change soon (implementing LPReal is a first step towards this). |
|
1295 | 44 |
|
7405 | 45 |
Range and Precision of Storage Formats: |
46 |
||
47 |
Format | Class | Significant Digits (Binary) | Smallest Pos Number | Largest Pos Number | Significant Digits (Decimal) |
|
48 |
-------+------------+-----------------------------+---------------------+--------------------+------------------------------ |
|
49 |
single | ShortFloat | 24 | 1.175... 10-38 | 3.402... 10+38 | 6-9 |
|
50 |
-------+------------+-----------------------------+---------------------+--------------------+------------------------------ |
|
51 |
double | Float | 53 | 2.225... 10-308 | 1.797... 10+308 | 15-17 |
|
52 |
-------+------------+-----------------------------+---------------------+--------------------+------------------------------ |
|
53 |
double | LongFloat | 113 | 3.362... 10-4932 | 1.189... 10+4932 | 33-36 |
|
54 |
ext | | | | | |
|
55 |
(SPARC)| | | | | |
|
56 |
-------+ |-----------------------------+---------------------+--------------------+------------------------------ |
|
57 |
double | | 64 | 3.362... 10-4932 | 1.189... 10+4932 | 18-21 |
|
58 |
ext | | | | | |
|
59 |
(x86) | | | | | |
|
60 |
-------+------------+-----------------------------+---------------------+--------------------+------------------------------ |
|
61 |
||
62 |
||
1295 | 63 |
[author:] |
64 |
Claus Gittinger |
|
1893 | 65 |
|
66 |
[see also:] |
|
67 |
Fraction FixedPoint |
|
88 | 68 |
" |
69 |
! ! |
|
55 | 70 |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
71 |
!LimitedPrecisionReal class methodsFor:'initialization'! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
72 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
73 |
initialize |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
74 |
"initialize ANSI compliant float globals" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
75 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
76 |
Smalltalk at:#FloatE put:ShortFloat. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
77 |
Smalltalk at:#FloatD put:Float. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
78 |
Smalltalk at:#FloatQ put:LongFloat. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
79 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
80 |
" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
81 |
self initialize |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
82 |
" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
83 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
84 |
"Created: / 7.9.2001 / 14:02:45 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
85 |
"Modified: / 7.9.2001 / 14:02:54 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
86 |
! ! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
87 |
|
55 | 88 |
!LimitedPrecisionReal class methodsFor:'instance creation'! |
89 |
||
90 |
new:aNumber |
|
91 |
"catch this message - not allowed for floats/doubles" |
|
92 |
||
93 |
self error:'Floats/Doubles cannot be created with new:' |
|
94 |
! ! |
|
95 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
96 |
!LimitedPrecisionReal class methodsFor:'constants & defaults'! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
97 |
|
7390 | 98 |
NaN |
99 |
"return the constant NaN (not a Number)." |
|
100 |
||
101 |
^ self zero uncheckedDivide:self zero |
|
102 |
||
103 |
" |
|
104 |
ShortFloat NaN |
|
105 |
Float NaN |
|
106 |
LongFloat NaN |
|
107 |
" |
|
108 |
! |
|
109 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
110 |
e |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
111 |
"return the closest approximation of the irrational number e" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
112 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
113 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
114 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
115 |
"Modified: / 7.9.2001 / 14:05:02 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
116 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
117 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
118 |
emax |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
119 |
"return the largest exponent" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
120 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
121 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
122 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
123 |
"Created: / 7.9.2001 / 14:05:28 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
124 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
125 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
126 |
emin |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
127 |
"return the smallest exponent" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
128 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
129 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
130 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
131 |
"Created: / 7.9.2001 / 14:05:35 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
132 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
133 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
134 |
epsilon |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
135 |
"return the maximum relative spacing" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
136 |
|
6575
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
137 |
^ self radix asFloat raisedTo:(1 - self precision) |
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
138 |
|
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
139 |
" |
7356 | 140 |
ShortFloat epsilon |
141 |
Float epsilon |
|
142 |
LongFloat epsilon |
|
6575
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
143 |
" |
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
144 |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
145 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
146 |
"Created: / 7.9.2001 / 14:06:41 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
147 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
148 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
149 |
fmax |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
150 |
"return the largest value allowed" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
151 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
152 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
153 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
154 |
"Created: / 7.9.2001 / 14:06:56 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
155 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
156 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
157 |
fmin |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
158 |
"return the minimum value allowed" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
159 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
160 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
161 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
162 |
"Created: / 7.9.2001 / 14:07:06 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
163 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
164 |
|
7390 | 165 |
infinity |
166 |
"return a float representing infinity" |
|
167 |
||
168 |
^ self unity uncheckedDivide:self zero |
|
169 |
||
170 |
" |
|
171 |
ShortFloat infinity |
|
172 |
Float infinity |
|
173 |
LongFloat infinity |
|
174 |
" |
|
175 |
! |
|
176 |
||
177 |
negativeInfinity |
|
178 |
"return a float representing negative infinity" |
|
179 |
||
180 |
^ self unity negated uncheckedDivide:self zero |
|
181 |
||
182 |
" |
|
183 |
ShortFloat negativeInfinity |
|
184 |
Float negativeInfinity |
|
185 |
LongFloat negativeInfinity |
|
186 |
" |
|
187 |
! |
|
188 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
189 |
pi |
7356 | 190 |
"return the closest approximation of the irrational number pi" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
191 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
192 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
193 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
194 |
"Created: / 7.9.2001 / 14:07:35 / cg" |
7356 | 195 |
! ! |
196 |
||
197 |
!LimitedPrecisionReal class methodsFor:'queries'! |
|
198 |
||
199 |
decimalPrecision |
|
200 |
"return the number of valid decimal digits" |
|
201 |
||
202 |
^ (self precision / (10.0 log:self radix)) floor |
|
203 |
||
204 |
" |
|
205 |
ShortFloat decimalPrecision |
|
206 |
Float decimalPrecision |
|
207 |
LongFloat decimalPrecision |
|
208 |
" |
|
209 |
! |
|
210 |
||
211 |
numBitsInExponent |
|
212 |
"return the number of bits in the exponent" |
|
213 |
||
214 |
^ self subclassResponsibility |
|
215 |
! |
|
216 |
||
217 |
numBitsInIntegerPart |
|
218 |
^ 0 |
|
219 |
! |
|
220 |
||
221 |
numBitsInMantissa |
|
222 |
"return the number of bits in the mantissa |
|
223 |
(typically 1 less than the precision due to the hidden bit)" |
|
224 |
||
225 |
^ self subclassResponsibility |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
226 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
227 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
228 |
precision |
7356 | 229 |
"return the number of valid mantissa bits" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
230 |
|
7356 | 231 |
^ self numBitsInMantissa + 1 - self numBitsInIntegerPart |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
232 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
233 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
234 |
radix |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
235 |
"return the radix (base)" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
236 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
237 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
238 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
239 |
"Created: / 7.9.2001 / 14:08:20 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
240 |
! ! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
241 |
|
55 | 242 |
!LimitedPrecisionReal methodsFor:'accessing'! |
243 |
||
244 |
at:index |
|
245 |
"redefined to prevent access to individual bytes in a real." |
|
246 |
||
247 |
self error:'not allowed for floats/doubles' |
|
248 |
! |
|
249 |
||
250 |
at:index put:aValue |
|
251 |
"redefined to prevent access to individual bytes in a real" |
|
252 |
||
253 |
self error:'not allowed for floats/doubles' |
|
254 |
! ! |
|
255 |
||
256 |
!LimitedPrecisionReal methodsFor:'arithmetic'! |
|
257 |
||
701 | 258 |
* aNumber |
259 |
"return the product of the receiver and the argument, aNumber" |
|
260 |
||
1200 | 261 |
"/ as soon as Float are float & Double are doubles, |
262 |
"/ use: |
|
263 |
"/ ^ aNumber productFromDouble:self asDouble |
|
264 |
||
265 |
^ aNumber productFromFloat:self asFloat |
|
266 |
||
267 |
"Modified: 17.4.1996 / 12:35:36 / cg" |
|
701 | 268 |
! |
269 |
||
55 | 270 |
+ aNumber |
271 |
"return the sum of the receiver and the argument, aNumber" |
|
272 |
||
1200 | 273 |
"/ as soon as Float are float & Double are doubles, |
274 |
"/ use: |
|
275 |
"/ ^ aNumber sumFromDouble:self asDouble |
|
276 |
||
277 |
^ aNumber sumFromFloat:self asFloat |
|
278 |
||
279 |
"Modified: 17.4.1996 / 12:35:55 / cg" |
|
55 | 280 |
! |
281 |
||
282 |
- aNumber |
|
283 |
"return the difference of the receiver and the argument, aNumber" |
|
284 |
||
1200 | 285 |
"/ as soon as Float are float & Double are doubles, |
286 |
"/ use: |
|
287 |
"/ ^ aNumber differenceFromDouble:self asDouble |
|
288 |
||
289 |
^ aNumber differenceFromFloat:self asFloat |
|
290 |
||
291 |
"Modified: 17.4.1996 / 12:36:07 / cg" |
|
55 | 292 |
! |
293 |
||
294 |
/ aNumber |
|
295 |
"return the quotient of the receiver and the argument, aNumber" |
|
296 |
||
297 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
7141
033f2c26d8e6
replaced refs to DivisionByZeroSignal with ZeroDivide
Claus Gittinger <cg@exept.de>
parents:
6890
diff
changeset
|
298 |
^ ZeroDivide raiseRequestWith:thisContext. |
55 | 299 |
]. |
1200 | 300 |
"/ as soon as Float are float & Double are doubles, |
301 |
"/ use: |
|
302 |
"/ ^ aNumber quotientFromDouble:self asDouble |
|
303 |
||
304 |
^ aNumber quotientFromFloat:self asFloat |
|
305 |
||
4455
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
3898
diff
changeset
|
306 |
"Modified: / 17.4.1996 / 12:36:21 / cg" |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
3898
diff
changeset
|
307 |
"Modified: / 26.7.1999 / 10:46:11 / stefan" |
55 | 308 |
! |
309 |
||
310 |
// aNumber |
|
311 |
"return the integer quotient of dividing the receiver by aNumber with |
|
1882 | 312 |
truncation towards negative infinity." |
55 | 313 |
|
314 |
^ (self / aNumber) floor asInteger |
|
1882 | 315 |
|
316 |
"Modified: 5.11.1996 / 11:45:37 / cg" |
|
7356 | 317 |
! |
318 |
||
319 |
ceiling |
|
7398 | 320 |
^ self asTrueFraction ceiling |
7356 | 321 |
! |
322 |
||
323 |
floor |
|
7398 | 324 |
^ self asTrueFraction floor |
55 | 325 |
! ! |
326 |
||
5238 | 327 |
!LimitedPrecisionReal methodsFor:'coercing & converting'! |
55 | 328 |
|
2141 | 329 |
asFixedPoint |
330 |
"return the receiver as fixedPoint number. |
|
331 |
Q: what should the scale be here ?" |
|
332 |
||
333 |
^ self asFraction asFixedPoint |
|
334 |
||
335 |
" |
|
336 |
0.3 asFixedPoint |
|
337 |
0.5 asFixedPoint |
|
338 |
(1/5) asFloat asFixedPoint |
|
339 |
(1/3) asFloat asFixedPoint |
|
340 |
(2/3) asFloat asFixedPoint |
|
341 |
(1/8) asFloat asFixedPoint |
|
342 |
3.14159 asFixedPoint |
|
3060 | 343 |
0.0000001 asFraction |
344 |
0.0000001 asFixedPoint |
|
2141 | 345 |
" |
346 |
||
3060 | 347 |
"Modified: / 25.10.1997 / 15:36:54 / cg" |
2141 | 348 |
! |
349 |
||
350 |
asFixedPoint:scale |
|
351 |
"return the receiver as fixedPoint number with the given |
|
352 |
number of post-decimal-digits." |
|
353 |
||
354 |
^ self asFraction asFixedPoint:scale |
|
355 |
||
356 |
" |
|
3732 | 357 |
0.3 asFixedPoint:4 |
2141 | 358 |
0.3 asFixedPoint:3 |
3732 | 359 |
0.3 asFixedPoint:2 |
360 |
0.3 asFixedPoint:1 |
|
361 |
0.3 asFixedPoint:0 |
|
362 |
||
2141 | 363 |
0.5 asFixedPoint:3 |
364 |
(1/5) asFloat asFixedPoint:1 |
|
365 |
(1/8) asFloat asFixedPoint:1 |
|
6641 | 366 |
1.0 asFixedPoint:2 |
2141 | 367 |
3.14159 asFixedPoint:2 |
368 |
3.14159 asFixedPoint:3 |
|
369 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
|
370 |
" |
|
371 |
||
3732 | 372 |
"Modified: / 5.8.1998 / 13:29:51 / cg" |
2141 | 373 |
! |
374 |
||
55 | 375 |
asFraction |
5883 | 376 |
"Answer a rational number (Integer or Fraction) representing the receiver. |
377 |
This conversion uses the continued fraction method to approximate |
|
378 |
a floating point number." |
|
379 |
||
380 |
|num1 denom1 num2 denom2 int frac newD temp limit| |
|
55 | 381 |
|
7394 | 382 |
limit := 10 raisedToInteger:self defaultNumberOfDigits. |
5883 | 383 |
num1 := self truncated. |
384 |
denom1 := 1. "The first of two alternating denominators" |
|
385 |
num2 := 1. "The second numerator" |
|
386 |
denom2 := 0. "The second denominator--will update" |
|
387 |
int := num1. "The integer part of self" |
|
388 |
frac := self fractionPart. |
|
389 |
[frac = 0] whileFalse:[ |
|
390 |
newD := 1.0 / frac. |
|
391 |
int := newD truncated. |
|
392 |
frac := newD fractionPart. "save the fractional part for next time" |
|
393 |
temp := num2. "old numerator and save it" |
|
394 |
num2 := num1. |
|
395 |
num1 := num1 * int + temp. "Update first numerator" |
|
396 |
temp := denom2. "old denominator and save it" |
|
397 |
denom2 := denom1. |
|
398 |
denom1 := int * denom1 + temp. "Update first denominator" |
|
399 |
limit < denom1 ifTrue:[ |
|
400 |
"Is ratio past float precision? If so, pick which of the two ratios to use" |
|
401 |
num2 = 0.0 ifTrue:[ |
|
402 |
"Is second denominator 0?" |
|
403 |
^ Fraction numerator:num1 denominator:denom1 |
|
404 |
]. |
|
405 |
^ Fraction numerator:num2 denominator:denom2 |
|
406 |
] |
|
3060 | 407 |
]. |
408 |
||
5883 | 409 |
"If fractional part is zero, return the first ratio" |
410 |
denom1 = 1 ifTrue:[ |
|
411 |
"Am i really an Integer?" |
|
412 |
^ num1 "Yes, return Integer result" |
|
3060 | 413 |
]. |
5883 | 414 |
"Otherwise return Fraction result" |
415 |
^ Fraction numerator:num1 denominator:denom1 |
|
55 | 416 |
|
3060 | 417 |
" |
5883 | 418 |
1.1 asFraction |
419 |
1.2 asFraction |
|
420 |
0.3 asFraction |
|
7394 | 421 |
0.5 asFraction |
422 |
(1/5) asFloat asFraction |
|
5883 | 423 |
(1/8) asFloat asFraction |
424 |
(1/13) asFloat asFraction |
|
425 |
3.14159 asFraction |
|
426 |
3.14159 asFraction asFloat |
|
7394 | 427 |
1.3 asFraction |
428 |
1.0 asFraction |
|
429 |
1E6 asFraction |
|
430 |
1E-6 asFraction |
|
3060 | 431 |
" |
2793 | 432 |
|
3060 | 433 |
"Modified: / 25.10.1997 / 16:41:19 / cg" |
55 | 434 |
! |
435 |
||
436 |
asInteger |
|
437 |
"return an integer with same value - might truncate" |
|
438 |
||
3185 | 439 |
|max maxF| |
55 | 440 |
|
2388 | 441 |
self isNaN ifTrue:[ |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
442 |
^ self class |
7403 | 443 |
raise:#conversionErrorSignal |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
444 |
receiver:self |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
445 |
selector:#asInteger |
6195 | 446 |
arguments:#() |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
447 |
errorString:'receiver is NaN in #asInteger' |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
448 |
|
2388 | 449 |
]. |
450 |
||
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
451 |
self abs < 2e16 ifTrue:[ |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
452 |
"/ NOTICE: this must be redefined in float |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
453 |
"/ subclasses to handle the smallinteger range; |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
454 |
"/ i.e. this may only be invoked for reals |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
455 |
"/ which are NOT within the smallInt range. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
456 |
"/ otherwise, endless recursion is the consequence. |
3426 | 457 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
458 |
max := SmallInteger maxVal // 2 + 1. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
459 |
maxF := max asFloat. |
3426 | 460 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
461 |
^ (self quo:maxF) * max + (self rem:maxF) truncated |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
462 |
]. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
463 |
^ self asTrueFraction |
55 | 464 |
|
3137 | 465 |
" |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
466 |
12345.0 asInteger |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
467 |
1e15 asInteger |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
468 |
1e33 asInteger asFloat |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
469 |
1e303 asInteger asFloat |
3137 | 470 |
" |
2388 | 471 |
|
6195 | 472 |
"Modified: / 16.11.2001 / 14:15:33 / cg" |
701 | 473 |
! |
474 |
||
7378 | 475 |
asLimitedPrecisionReal |
476 |
"return a float of any precision with same value" |
|
477 |
||
478 |
^ self |
|
479 |
! |
|
480 |
||
5883 | 481 |
asRational |
482 |
"Answer a Rational number--Integer or Fraction--representing the receiver. |
|
483 |
Same as asFraction fro st-80 compatibility." |
|
484 |
||
485 |
^ self asFraction |
|
486 |
||
487 |
" |
|
488 |
1.1 asRational |
|
489 |
1.2 asRational |
|
490 |
0.3 asRational |
|
491 |
0.5 asRational |
|
492 |
(1/5) asFloat asRational |
|
493 |
(1/8) asFloat asRational |
|
494 |
(1/13) asFloat asRational |
|
495 |
3.14159 asRational |
|
496 |
3.14159 asRational asFloat |
|
497 |
1.3 asRational |
|
498 |
1.0 asRational |
|
499 |
" |
|
500 |
! |
|
501 |
||
7356 | 502 |
asTrueFraction |
503 |
"Answer a fraction or integer that EXACTLY represents self, |
|
504 |
an any-precision IEEE floating point number, consisting of: |
|
505 |
numMantissaBits bits of normalized mantissa (i.e. with hidden leading 1-bit) |
|
506 |
optional numExtraBits between mantissa and exponent (normalized flag for ext-real) |
|
507 |
numExponentBits bits of 2s complement exponent |
|
508 |
1 sign bit. |
|
509 |
Taken from Floats asTrueFraction" |
|
510 |
||
511 |
|shifty sign expPart exp fraction fractionPart result zeroBitsCount |
|
512 |
numBytes numBits numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
513 |
biasExponent numIntegerBits| |
|
514 |
||
515 |
self isFinite ifFalse:[ |
|
516 |
^ self class |
|
517 |
raise:#conversionErrorSignal |
|
518 |
receiver:self |
|
519 |
selector:#asTrueFraction |
|
520 |
arguments:#() |
|
521 |
errorString:'Cannot represent non-finite float as a fraction'. |
|
522 |
]. |
|
523 |
||
524 |
"Extract the bits of an IEEE anySize float " |
|
525 |
numBytes := self basicSize. |
|
526 |
numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
|
527 |
numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
528 |
numIntegerBits := self class numBitsInIntegerPart. |
|
529 |
numBits := numBitsInMantissa + numBitsInExponent. "not (numBytes * 8 - 1) -- there could be 80 float bits in 96 overall bits" |
|
530 |
biasExponent := maskExponent bitShift:-1. |
|
531 |
||
532 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
533 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
534 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
535 |
] ifFalse:[ |
|
536 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
537 |
]. |
|
538 |
||
539 |
" Extract the sign and the biased exponent " |
|
540 |
sign := (shifty bitAt:numBits+1) == 0 ifTrue: [1] ifFalse: [-1]. |
|
541 |
expPart := (shifty bitShift:numBitsInMantissa negated) bitAnd: maskExponent. |
|
542 |
||
543 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
544 |
fractionPart := shifty bitAnd:maskMantissa. |
|
545 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
|
701 | 546 |
|
7356 | 547 |
numIntegerBits == 0 ifTrue:[ |
548 |
" Replace omitted leading 1 in fraction (Notice: quadIEEE format does not do this)" |
|
549 |
fraction := fractionPart bitOr: (maskMantissa + 1). |
|
550 |
] ifFalse:[ |
|
551 |
fraction := fractionPart. |
|
552 |
]. |
|
553 |
||
7390 | 554 |
"Unbias exponent" |
7356 | 555 |
exp := biasExponent - expPart + (numBitsInMantissa - numIntegerBits). |
1200 | 556 |
|
7390 | 557 |
" Form the result. When exp>fractionalPrecision, the exponent is adjusted by |
7356 | 558 |
the number of trailing zero bits in the fraction to minimize |
7390 | 559 |
the (huge) time otherwise spent in #gcd: of fraction handling code." |
7356 | 560 |
exp negative ifTrue: [ |
561 |
result := sign * fraction bitShift: exp negated |
|
562 |
] ifFalse:[ |
|
563 |
zeroBitsCount := fraction lowBit - 1. |
|
564 |
exp := exp - zeroBitsCount. |
|
565 |
exp <= 0 ifTrue: [ |
|
566 |
zeroBitsCount := zeroBitsCount + exp. |
|
567 |
result := sign * fraction bitShift:zeroBitsCount negated |
|
568 |
] ifFalse: [ |
|
569 |
result := Fraction |
|
570 |
numerator: (sign * fraction bitShift: zeroBitsCount negated) |
|
571 |
denominator: (1 bitShift:exp) |
|
572 |
] |
|
573 |
]. |
|
1200 | 574 |
|
7356 | 575 |
"Low cost validation omitted after extensive testing" |
576 |
"(result asFloat = self) ifFalse: [self error: 'asTrueFraction validation failed']." |
|
577 |
||
578 |
^ result |
|
579 |
||
580 |
" |
|
7390 | 581 |
0.3 asFloat asTrueFraction |
7356 | 582 |
0.3 asShortFloat asTrueFraction |
7390 | 583 |
0.3 asLongFloat asTrueFraction |
584 |
||
585 |
1.25 asTrueFraction |
|
586 |
0.25 asTrueFraction |
|
587 |
-0.25 asTrueFraction |
|
588 |
3e37 asTrueFraction |
|
589 |
||
590 |
Float NaN asTrueFraction -> error |
|
591 |
Float infinity asTrueFraction -> error |
|
592 |
Float negativeInfinity asTrueFraction -> error |
|
593 |
" |
|
594 |
! |
|
595 |
||
596 |
exponent |
|
597 |
"extract a normalized floats exponent. |
|
598 |
The returned value depends on the float-representation of |
|
599 |
the underlying machine and is therefore highly unportable. |
|
600 |
This is not for general use. |
|
601 |
This assumes that the mantissa is normalized to |
|
602 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
603 |
||
604 |
|shifty expPart exp numBytes numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
605 |
biasExponent numIntegerBits fractionPart| |
|
7356 | 606 |
|
7390 | 607 |
"Extract the bits of an IEEE anySize float " |
608 |
numBytes := self basicSize. |
|
609 |
numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
|
610 |
numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
611 |
numIntegerBits := self class numBitsInIntegerPart. |
|
612 |
biasExponent := maskExponent bitShift:-1. |
|
613 |
||
614 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
615 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
616 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
617 |
] ifFalse:[ |
|
618 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
619 |
]. |
|
620 |
||
621 |
" Extract the sign and the biased exponent " |
|
622 |
expPart := (shifty bitShift:numBitsInMantissa negated) bitAnd: maskExponent. |
|
623 |
||
624 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
625 |
fractionPart := shifty bitAnd:maskMantissa. |
|
626 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
|
627 |
||
628 |
exp := expPart - biasExponent + 1. |
|
629 |
^ exp |
|
630 |
||
631 |
" |
|
632 |
0.3 asFloat exponent |
|
633 |
0.3 asShortFloat exponent |
|
634 |
0.3 asLongFloat exponent |
|
635 |
||
636 |
0.0 exponent2 0 |
|
637 |
1.0 exponent2 1 |
|
638 |
2.0 exponent2 2 |
|
639 |
3.0 exponent2 2 |
|
640 |
4.0 exponent2 3 |
|
641 |
0.5 exponent2 0 |
|
642 |
0.4 exponent2 -1 |
|
643 |
0.25 exponent2 -1 |
|
644 |
0.00000011111 exponent2 -23 |
|
7356 | 645 |
" |
701 | 646 |
! |
647 |
||
3898 | 648 |
fractionalPart |
649 |
"This has been renamed to #fractionPart for ST80 compatibility. |
|
650 |
||
651 |
extract the after-decimal fraction part. |
|
652 |
the floats value is |
|
653 |
float truncated + float fractionalPart" |
|
654 |
||
5868 | 655 |
<resource:#obsolete> |
656 |
||
3898 | 657 |
self obsoleteMethodWarning:'please use #fractionPart'. |
658 |
^ self fractionPart |
|
659 |
||
660 |
"Modified: / 28.10.1998 / 17:10:12 / cg" |
|
661 |
"Created: / 28.10.1998 / 17:10:32 / cg" |
|
662 |
! |
|
663 |
||
701 | 664 |
generality |
665 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
666 |
||
667 |
^ 80 |
|
7356 | 668 |
! |
669 |
||
670 |
i |
|
671 |
"return a complex number, with the receiver as imaginary part, 0 as real part" |
|
672 |
||
673 |
^ Complex |
|
674 |
real:0 |
|
675 |
imaginary:self |
|
676 |
||
677 |
" |
|
678 |
3.0i |
|
679 |
(1+1i) |
|
680 |
" |
|
701 | 681 |
! ! |
682 |
||
683 |
!LimitedPrecisionReal methodsFor:'comparing'! |
|
684 |
||
685 |
< aNumber |
|
686 |
"return true, if the argument is greater" |
|
687 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
688 |
"/ as soon as Float are float & Double are doubles, |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
689 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
690 |
"/ ^ aNumber lessFromDouble:self asDouble |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
691 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
692 |
^ aNumber lessFromFloat:self asFloat |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
693 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
694 |
"Modified: 17.4.1996 / 13:34:50 / cg" |
701 | 695 |
! |
696 |
||
697 |
<= aNumber |
|
698 |
"return true, if the argument is greater or equal" |
|
699 |
||
700 |
^ self retry:#<= coercing:aNumber |
|
701 |
! |
|
702 |
||
703 |
> aNumber |
|
704 |
"return true, if the argument is less" |
|
705 |
||
706 |
^ self retry:#> coercing:aNumber |
|
707 |
! |
|
708 |
||
709 |
>= aNumber |
|
710 |
"return true, if the argument is less or equal" |
|
711 |
||
712 |
^ self retry:#>= coercing:aNumber |
|
713 |
! ! |
|
714 |
||
715 |
!LimitedPrecisionReal methodsFor:'copying'! |
|
716 |
||
717 |
deepCopy |
|
718 |
"return a deep copy of myself |
|
719 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
720 |
||
721 |
^ self |
|
722 |
! |
|
723 |
||
724 |
deepCopyUsing:aDictionary |
|
725 |
"return a deep copy of myself |
|
726 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
727 |
||
728 |
^ self |
|
729 |
! |
|
730 |
||
731 |
shallowCopy |
|
732 |
"return a shallow copy of the receiver" |
|
733 |
||
734 |
^ self |
|
735 |
! |
|
736 |
||
737 |
simpleDeepCopy |
|
738 |
"return a deep copy of the receiver |
|
739 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
740 |
||
741 |
^ self |
|
55 | 742 |
! ! |
743 |
||
744 |
!LimitedPrecisionReal methodsFor:'double dispatching'! |
|
745 |
||
746 |
differenceFromFraction:aFraction |
|
213 | 747 |
"sent when a fraction does not know how to subtract the receiver, a float" |
55 | 748 |
|
749 |
|d| |
|
750 |
||
751 |
d := aFraction denominator. |
|
752 |
^ (aFraction numerator - (self * d)) / d |
|
753 |
! |
|
754 |
||
755 |
productFromFraction:aFraction |
|
213 | 756 |
"sent when a fraction does not know how to multiply the receiver, a float" |
55 | 757 |
|
758 |
^ self * aFraction numerator / aFraction denominator |
|
759 |
! |
|
760 |
||
761 |
quotientFromFraction:aFraction |
|
213 | 762 |
"sent when a fraction does not know how to divide by the receiver, a float" |
55 | 763 |
|
764 |
^ aFraction numerator / (self * aFraction denominator) |
|
701 | 765 |
! |
766 |
||
767 |
sumFromFraction:aFraction |
|
768 |
"sent when a fraction does not know how to add the receiver, a float" |
|
769 |
||
770 |
|d| |
|
771 |
||
772 |
d := aFraction denominator. |
|
773 |
^ (self * d + aFraction numerator) / d |
|
774 |
! ! |
|
775 |
||
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
776 |
!LimitedPrecisionReal methodsFor:'encoding'! |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
777 |
|
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
778 |
encodeOn:anEncoder with:aParameter |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
779 |
|
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
780 |
anEncoder encodeFloat:self with:aParameter |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
781 |
|
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
782 |
|
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
783 |
! ! |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
784 |
|
701 | 785 |
!LimitedPrecisionReal methodsFor:'printing & storing'! |
786 |
||
787 |
printOn:aStream |
|
788 |
"append a printed representation of the receiver to |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
789 |
the argument, aStream. |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
790 |
|
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
791 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
792 |
#printOn: as basic print mechanism." |
701 | 793 |
|
794 |
aStream nextPutAll:self printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
795 |
|
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
796 |
"Modified: / 20.1.1998 / 14:10:46 / stefan" |
1200 | 797 |
! |
798 |
||
799 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
800 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
801 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
802 |
#printOn: as basic print mechanism." |
1200 | 803 |
|
804 |
^ self subclassResponsibility |
|
805 |
||
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
806 |
"Created: / 17.4.1996 / 12:12:20 / cg" |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
807 |
"Modified: / 20.1.1998 / 14:10:47 / stefan" |
701 | 808 |
! ! |
809 |
||
5557 | 810 |
!LimitedPrecisionReal methodsFor:'queries'! |
811 |
||
5883 | 812 |
defaultNumberOfDigits |
7356 | 813 |
"Answer how many digits of accuracy this class supports" |
814 |
||
815 |
^ self class decimalPrecision - 1 |
|
5883 | 816 |
! |
817 |
||
5557 | 818 |
size |
819 |
"redefined since reals are kludgy (ByteArry)" |
|
820 |
||
821 |
^ 0 |
|
822 |
! ! |
|
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
823 |
|
701 | 824 |
!LimitedPrecisionReal methodsFor:'testing'! |
825 |
||
3148
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
826 |
isFinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
827 |
^ self subclassResponsibility |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
828 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
829 |
"Created: / 7.1.1998 / 12:02:06 / stefan" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
830 |
! |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
831 |
|
6185 | 832 |
isFloat |
833 |
"return true, if the receiver is some kind of floating point number; |
|
834 |
false is returned here. |
|
835 |
Same as #isLimitedPrecisionReal, but a better name ;-)" |
|
836 |
||
837 |
^ true |
|
838 |
||
839 |
"Created: / 14.11.2001 / 14:57:55 / cg" |
|
840 |
! |
|
841 |
||
3148
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
842 |
isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
843 |
"return true, if the receiver is an infinite float (Inf). |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
844 |
These are not created by ST/X float operations (they raise an exception); |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
845 |
however, inline C-code could produce them ..." |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
846 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
847 |
^ (self isFinite or:[self isNaN]) not. |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
848 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
849 |
" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
850 |
1.0 isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
851 |
(0.0 uncheckedDivide: 0.0) isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
852 |
(1.0 uncheckedDivide: 0.0) isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
853 |
" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
854 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
855 |
"Modified: / 7.1.1998 / 12:01:30 / stefan" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
856 |
! |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
857 |
|
6086
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
858 |
isLimitedPrecisionReal |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
859 |
"return true, if the receiver is some kind of limited precision real (i.e. floating point) number; |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
860 |
true is returned here - the method is redefined from Object." |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
861 |
|
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
862 |
^ true |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
863 |
! |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
864 |
|
2388 | 865 |
isNaN |
866 |
^ self subclassResponsibility |
|
867 |
||
868 |
"Modified: 12.2.1997 / 16:45:27 / cg" |
|
869 |
! |
|
870 |
||
7356 | 871 |
isNegativeInfinity |
872 |
^ self negative and:[self isInfinite] |
|
873 |
! |
|
874 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
875 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
876 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
877 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
878 |
^ (self printString first == $-) |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
879 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
880 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
881 |
0.0 isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
882 |
-0.0 isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
883 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
884 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
885 |
|
7356 | 886 |
isPositiveInfinity |
887 |
^ self positive and:[self isInfinite] |
|
888 |
! |
|
889 |
||
701 | 890 |
negative |
891 |
"return true if the receiver is less than zero" |
|
892 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
893 |
"/ as soon as Float are float & Double are doubles, |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
894 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
895 |
"/ ^ self asDouble negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
896 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
897 |
^ self asFloat negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
898 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
899 |
"Modified: 17.4.1996 / 13:35:00 / cg" |
701 | 900 |
! |
901 |
||
5357 | 902 |
numberOfBits |
903 |
"return the size (in bits) of the real; |
|
904 |
typically, this is 64 for Floats and 32 for ShortFloats, |
|
905 |
but who knows ..." |
|
906 |
||
907 |
self subclassResponsibility |
|
908 |
! |
|
909 |
||
701 | 910 |
positive |
911 |
"return true if the receiver is greater or equal to zero" |
|
912 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
913 |
"/ as soon as Float are float & Double are doubles, |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
914 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
915 |
"/ ^ self asDouble positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
916 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
917 |
^ self asFloat positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
918 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
919 |
"Modified: 17.4.1996 / 13:35:10 / cg" |
55 | 920 |
! ! |
921 |
||
7356 | 922 |
!LimitedPrecisionReal methodsFor:'truncation & rounding'! |
923 |
||
924 |
ceilingAsFloat |
|
925 |
^ self coerce:(self ceiling). |
|
926 |
||
927 |
" |
|
928 |
0.4 asLongFloat ceilingAsFloat |
|
929 |
" |
|
930 |
! |
|
931 |
||
932 |
floorAsFloat |
|
933 |
^ self coerce:(self floor). |
|
934 |
||
935 |
" |
|
936 |
0.4 asLongFloat floorAsFloat |
|
937 |
" |
|
938 |
! |
|
939 |
||
940 |
roundedAsFloat |
|
941 |
self negative ifTrue:[ ^ (self - 0.5) ceilingAsFloat ]. |
|
942 |
^ (self + 0.5) floorAsFloat |
|
7398 | 943 |
! |
944 |
||
945 |
truncatedAsFloat |
|
946 |
^ self coerce:(self truncated). |
|
947 |
||
948 |
" |
|
949 |
0.4 asLongFloat truncatedAsFloat |
|
950 |
" |
|
7356 | 951 |
! ! |
952 |
||
701 | 953 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
55 | 954 |
|
701 | 955 |
version |
7405 | 956 |
^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.54 2003-06-17 14:35:32 cg Exp $' |
55 | 957 |
! ! |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
958 |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
959 |
LimitedPrecisionReal initialize! |