author | Claus Gittinger <cg@exept.de> |
Tue, 19 Aug 1997 22:40:49 +0200 | |
changeset 2868 | 772c890f907c |
parent 2793 | e40dedf51177 |
child 3060 | 0faf242e1142 |
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 |
||
13 |
Number subclass:#LimitedPrecisionReal |
|
1200 | 14 |
instanceVariableNames:'' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
55 | 18 |
! |
19 |
||
88 | 20 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
21 |
||
22 |
copyright |
|
23 |
" |
|
24 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
213 | 25 |
All Rights Reserved |
55 | 26 |
|
88 | 27 |
This software is furnished under a license and may be used |
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
55 | 35 |
|
88 | 36 |
documentation |
37 |
" |
|
38 |
Abstract superclass for single and double (and maybe more) |
|
39 |
precision real numbers (i.e. Float and Double). |
|
92 | 40 |
|
41 |
Due to historic reasons, ST/X's Floats are what Doubles are in ST-80. |
|
42 |
This may change soon (implementing LPReal is a first step towards this). |
|
1295 | 43 |
|
44 |
[author:] |
|
45 |
Claus Gittinger |
|
1893 | 46 |
|
47 |
[see also:] |
|
48 |
Fraction FixedPoint |
|
88 | 49 |
" |
50 |
! ! |
|
55 | 51 |
|
52 |
!LimitedPrecisionReal class methodsFor:'instance creation'! |
|
53 |
||
54 |
new:aNumber |
|
55 |
"catch this message - not allowed for floats/doubles" |
|
56 |
||
57 |
self error:'Floats/Doubles cannot be created with new:' |
|
58 |
! ! |
|
59 |
||
60 |
!LimitedPrecisionReal methodsFor:'accessing'! |
|
61 |
||
62 |
at:index |
|
63 |
"redefined to prevent access to individual bytes in a real." |
|
64 |
||
65 |
self error:'not allowed for floats/doubles' |
|
66 |
! |
|
67 |
||
68 |
at:index put:aValue |
|
69 |
"redefined to prevent access to individual bytes in a real" |
|
70 |
||
71 |
self error:'not allowed for floats/doubles' |
|
701 | 72 |
! |
73 |
||
74 |
size |
|
75 |
"redefined since reals are kludgy (ByteArry)" |
|
76 |
||
77 |
^ 0 |
|
55 | 78 |
! ! |
79 |
||
80 |
!LimitedPrecisionReal methodsFor:'arithmetic'! |
|
81 |
||
701 | 82 |
* aNumber |
83 |
"return the product of the receiver and the argument, aNumber" |
|
84 |
||
1200 | 85 |
"/ as soon as Float are float & Double are doubles, |
86 |
"/ use: |
|
87 |
"/ ^ aNumber productFromDouble:self asDouble |
|
88 |
||
89 |
^ aNumber productFromFloat:self asFloat |
|
90 |
||
91 |
"Modified: 17.4.1996 / 12:35:36 / cg" |
|
701 | 92 |
! |
93 |
||
55 | 94 |
+ aNumber |
95 |
"return the sum of the receiver and the argument, aNumber" |
|
96 |
||
1200 | 97 |
"/ as soon as Float are float & Double are doubles, |
98 |
"/ use: |
|
99 |
"/ ^ aNumber sumFromDouble:self asDouble |
|
100 |
||
101 |
^ aNumber sumFromFloat:self asFloat |
|
102 |
||
103 |
"Modified: 17.4.1996 / 12:35:55 / cg" |
|
55 | 104 |
! |
105 |
||
106 |
- aNumber |
|
107 |
"return the difference of the receiver and the argument, aNumber" |
|
108 |
||
1200 | 109 |
"/ as soon as Float are float & Double are doubles, |
110 |
"/ use: |
|
111 |
"/ ^ aNumber differenceFromDouble:self asDouble |
|
112 |
||
113 |
^ aNumber differenceFromFloat:self asFloat |
|
114 |
||
115 |
"Modified: 17.4.1996 / 12:36:07 / cg" |
|
55 | 116 |
! |
117 |
||
118 |
/ aNumber |
|
119 |
"return the quotient of the receiver and the argument, aNumber" |
|
120 |
||
121 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
1200 | 122 |
^ DivisionByZeroSignal raise. |
55 | 123 |
]. |
1200 | 124 |
"/ as soon as Float are float & Double are doubles, |
125 |
"/ use: |
|
126 |
"/ ^ aNumber quotientFromDouble:self asDouble |
|
127 |
||
128 |
^ aNumber quotientFromFloat:self asFloat |
|
129 |
||
130 |
"Modified: 17.4.1996 / 12:36:21 / cg" |
|
55 | 131 |
! |
132 |
||
133 |
// aNumber |
|
134 |
"return the integer quotient of dividing the receiver by aNumber with |
|
1882 | 135 |
truncation towards negative infinity." |
55 | 136 |
|
137 |
^ (self / aNumber) floor asInteger |
|
1882 | 138 |
|
139 |
"Modified: 5.11.1996 / 11:45:37 / cg" |
|
55 | 140 |
! |
141 |
||
142 |
\\ aNumber |
|
143 |
"return the integer remainder of dividing the receiver by aNumber with |
|
144 |
truncation towards negative infinity." |
|
145 |
||
146 |
^ (self - ((self / aNumber) floor * aNumber)) floor asInteger |
|
147 |
! ! |
|
148 |
||
149 |
!LimitedPrecisionReal methodsFor:'coercion and converting'! |
|
150 |
||
2141 | 151 |
asFixedPoint |
152 |
"return the receiver as fixedPoint number. |
|
153 |
Q: what should the scale be here ?" |
|
154 |
||
155 |
^ self asFraction asFixedPoint |
|
156 |
||
157 |
" |
|
158 |
0.3 asFixedPoint |
|
159 |
0.5 asFixedPoint |
|
160 |
(1/5) asFloat asFixedPoint |
|
161 |
(1/3) asFloat asFixedPoint |
|
162 |
(2/3) asFloat asFixedPoint |
|
163 |
(1/8) asFloat asFixedPoint |
|
164 |
3.14159 asFixedPoint |
|
165 |
" |
|
166 |
||
167 |
"Modified: 10.1.1997 / 19:59:56 / cg" |
|
168 |
! |
|
169 |
||
170 |
asFixedPoint:scale |
|
171 |
"return the receiver as fixedPoint number with the given |
|
172 |
number of post-decimal-digits." |
|
173 |
||
174 |
^ self asFraction asFixedPoint:scale |
|
175 |
||
176 |
" |
|
177 |
0.3 asFixedPoint:3 |
|
178 |
0.5 asFixedPoint:3 |
|
179 |
(1/5) asFloat asFixedPoint:1 |
|
180 |
(1/8) asFloat asFixedPoint:1 |
|
181 |
3.14159 asFixedPoint:2 |
|
182 |
3.14159 asFixedPoint:3 |
|
183 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
|
184 |
" |
|
185 |
||
186 |
"Modified: 10.1.1997 / 19:59:42 / cg" |
|
187 |
! |
|
188 |
||
55 | 189 |
asFraction |
190 |
"return a corresponding fraction |
|
191 |
- notice, that 'aFract asFloat asFraction' does not always return |
|
192 |
a good fraction ... due to rounding errors when converting to float" |
|
193 |
||
194 |
|fract digits power num denom| |
|
195 |
||
196 |
"we (indirectly) use printf which knows the precision of floats" |
|
197 |
||
198 |
fract := self fractionPart. |
|
199 |
digits := fract printString copyFrom:3. |
|
200 |
power := digits size. |
|
201 |
num := (self - fract) asInteger. |
|
202 |
denom := (10 raisedToInteger:power). |
|
203 |
num := num * denom. |
|
204 |
num := num + (Integer readFromString:digits). |
|
2793 | 205 |
^ Fraction numerator:num denominator:denom |
55 | 206 |
|
207 |
"0.3 asFraction" |
|
208 |
"0.5 asFraction" |
|
209 |
"(1/5) asFloat asFraction" |
|
210 |
"(1/8) asFloat asFraction" |
|
211 |
"(1/13) asFloat asFraction -> inexact result due to rounding errors" |
|
2793 | 212 |
|
213 |
"Modified: 28.7.1997 / 19:07:47 / cg" |
|
55 | 214 |
! |
215 |
||
216 |
asInteger |
|
217 |
"return an integer with same value - might truncate" |
|
218 |
||
219 |
|l v sign| |
|
220 |
||
2388 | 221 |
self isNaN ifTrue:[ |
222 |
^ DomainErrorSignal raise |
|
223 |
]. |
|
224 |
||
55 | 225 |
"this is stupid code - rounding errors accumulate; fix later" |
226 |
||
227 |
sign := self sign. |
|
228 |
v := self abs. |
|
85 | 229 |
(v >= 10.0) ifTrue:[ |
2388 | 230 |
l := (v / 10.0) asInteger * 10 |
55 | 231 |
] ifFalse:[ |
2388 | 232 |
l := 0 |
55 | 233 |
]. |
85 | 234 |
v := v - ((v / 10.0) floor * 10) floor. |
55 | 235 |
l := l + v truncated. |
236 |
^ l * sign |
|
237 |
||
238 |
"12345.0 asInteger" |
|
239 |
"1e15 asInteger" |
|
2388 | 240 |
|
241 |
"Modified: 12.2.1997 / 16:45:07 / cg" |
|
701 | 242 |
! |
243 |
||
244 |
coerce:aNumber |
|
245 |
"return aNumber converted into receivers type" |
|
246 |
||
1200 | 247 |
"/ as soon as Float are float & Double are doubles, |
248 |
"/ use: |
|
249 |
"/ ^ aNumber asDouble |
|
250 |
||
251 |
^ aNumber asFloat |
|
252 |
||
253 |
"Modified: 17.4.1996 / 12:36:46 / cg" |
|
701 | 254 |
! |
255 |
||
256 |
generality |
|
257 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
258 |
||
259 |
^ 80 |
|
260 |
! ! |
|
261 |
||
262 |
!LimitedPrecisionReal methodsFor:'comparing'! |
|
263 |
||
264 |
< aNumber |
|
265 |
"return true, if the argument is greater" |
|
266 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
267 |
"/ 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
|
268 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
269 |
"/ ^ aNumber lessFromDouble:self asDouble |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
270 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
271 |
^ aNumber lessFromFloat:self asFloat |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
272 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
273 |
"Modified: 17.4.1996 / 13:34:50 / cg" |
701 | 274 |
! |
275 |
||
276 |
<= aNumber |
|
277 |
"return true, if the argument is greater or equal" |
|
278 |
||
279 |
^ self retry:#<= coercing:aNumber |
|
280 |
! |
|
281 |
||
282 |
= aNumber |
|
283 |
"return true, if the arguments value are equal" |
|
284 |
||
285 |
^ self retry:#= coercing:aNumber |
|
286 |
! |
|
287 |
||
288 |
> aNumber |
|
289 |
"return true, if the argument is less" |
|
290 |
||
291 |
^ self retry:#> coercing:aNumber |
|
292 |
! |
|
293 |
||
294 |
>= aNumber |
|
295 |
"return true, if the argument is less or equal" |
|
296 |
||
297 |
^ self retry:#>= coercing:aNumber |
|
298 |
! |
|
299 |
||
300 |
~= aNumber |
|
301 |
"return true, if the arguments value are not equal" |
|
302 |
||
303 |
^ self retry:#~= coercing:aNumber |
|
304 |
! ! |
|
305 |
||
306 |
!LimitedPrecisionReal methodsFor:'copying'! |
|
307 |
||
308 |
deepCopy |
|
309 |
"return a deep copy of myself |
|
310 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
311 |
||
312 |
^ self |
|
313 |
! |
|
314 |
||
315 |
deepCopyUsing:aDictionary |
|
316 |
"return a deep copy of myself |
|
317 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
318 |
||
319 |
^ self |
|
320 |
! |
|
321 |
||
322 |
shallowCopy |
|
323 |
"return a shallow copy of the receiver" |
|
324 |
||
325 |
^ self |
|
326 |
! |
|
327 |
||
328 |
simpleDeepCopy |
|
329 |
"return a deep copy of the receiver |
|
330 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
331 |
||
332 |
^ self |
|
55 | 333 |
! ! |
334 |
||
335 |
!LimitedPrecisionReal methodsFor:'double dispatching'! |
|
336 |
||
337 |
differenceFromFraction:aFraction |
|
213 | 338 |
"sent when a fraction does not know how to subtract the receiver, a float" |
55 | 339 |
|
340 |
|d| |
|
341 |
||
342 |
d := aFraction denominator. |
|
343 |
^ (aFraction numerator - (self * d)) / d |
|
344 |
! |
|
345 |
||
346 |
productFromFraction:aFraction |
|
213 | 347 |
"sent when a fraction does not know how to multiply the receiver, a float" |
55 | 348 |
|
349 |
^ self * aFraction numerator / aFraction denominator |
|
350 |
! |
|
351 |
||
352 |
quotientFromFraction:aFraction |
|
213 | 353 |
"sent when a fraction does not know how to divide by the receiver, a float" |
55 | 354 |
|
355 |
^ aFraction numerator / (self * aFraction denominator) |
|
701 | 356 |
! |
357 |
||
358 |
sumFromFraction:aFraction |
|
359 |
"sent when a fraction does not know how to add the receiver, a float" |
|
360 |
||
361 |
|d| |
|
362 |
||
363 |
d := aFraction denominator. |
|
364 |
^ (self * d + aFraction numerator) / d |
|
365 |
! ! |
|
366 |
||
367 |
!LimitedPrecisionReal methodsFor:'printing & storing'! |
|
368 |
||
369 |
printOn:aStream |
|
370 |
"append a printed representation of the receiver to |
|
371 |
the argument, aStream" |
|
372 |
||
373 |
aStream nextPutAll:self printString |
|
1200 | 374 |
! |
375 |
||
376 |
printString |
|
377 |
"return a printed representation of the receiver" |
|
378 |
||
379 |
^ self subclassResponsibility |
|
380 |
||
381 |
"Created: 17.4.1996 / 12:12:20 / cg" |
|
701 | 382 |
! ! |
383 |
||
384 |
!LimitedPrecisionReal methodsFor:'testing'! |
|
385 |
||
2388 | 386 |
isNaN |
387 |
^ self subclassResponsibility |
|
388 |
||
389 |
"Modified: 12.2.1997 / 16:45:27 / cg" |
|
390 |
! |
|
391 |
||
701 | 392 |
isReal |
393 |
"return true, if the receiver is some kind of real number; |
|
394 |
false is returned here - the method is redefined from Object." |
|
395 |
||
396 |
^ true |
|
397 |
! |
|
398 |
||
399 |
negative |
|
400 |
"return true if the receiver is less than zero" |
|
401 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
402 |
"/ 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
|
403 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
404 |
"/ ^ self asDouble negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
405 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
406 |
^ self asFloat negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
407 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
408 |
"Modified: 17.4.1996 / 13:35:00 / cg" |
701 | 409 |
! |
410 |
||
411 |
positive |
|
412 |
"return true if the receiver is greater or equal to zero" |
|
413 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
414 |
"/ 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
|
415 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
416 |
"/ ^ self asDouble positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
417 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
418 |
^ self asFloat positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
419 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
420 |
"Modified: 17.4.1996 / 13:35:10 / cg" |
55 | 421 |
! ! |
422 |
||
423 |
!LimitedPrecisionReal methodsFor:'truncation and rounding'! |
|
424 |
||
425 |
fractionPart |
|
1865
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
426 |
"return a float with value from digits after the decimal point. |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
427 |
(i.e. the receiver minus its truncated value)" |
55 | 428 |
|
429 |
^ self - self truncated asFloat |
|
430 |
||
1865
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
431 |
" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
432 |
1234.56789 fractionPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
433 |
1.2345e6 fractionPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
434 |
" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
435 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
436 |
"Modified: 4.11.1996 / 20:26:54 / cg" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
437 |
! |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
438 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
439 |
integerPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
440 |
"return a float with value from digits before the decimal point |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
441 |
(i.e. the truncated value)" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
442 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
443 |
^ self truncated asFloat |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
444 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
445 |
" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
446 |
1234.56789 integerPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
447 |
1.2345e6 integerPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
448 |
" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
449 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
450 |
"Modified: 4.11.1996 / 20:26:21 / cg" |
55 | 451 |
! ! |
452 |
||
701 | 453 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
55 | 454 |
|
701 | 455 |
version |
2793 | 456 |
^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.20 1997-07-28 17:11:51 cg Exp $' |
55 | 457 |
! ! |