author | Claus Gittinger <cg@exept.de> |
Fri, 10 Jan 1997 20:01:23 +0100 | |
changeset 2141 | bba0027204fd |
parent 1893 | c66af5c46272 |
child 2388 | df5e0eb88999 |
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). |
|
205 |
^ (Fraction numerator:num denominator:denom) reduced |
|
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" |
|
212 |
! |
|
213 |
||
214 |
asInteger |
|
215 |
"return an integer with same value - might truncate" |
|
216 |
||
217 |
|l v sign| |
|
218 |
||
219 |
"this is stupid code - rounding errors accumulate; fix later" |
|
220 |
||
221 |
sign := self sign. |
|
222 |
v := self abs. |
|
85 | 223 |
(v >= 10.0) ifTrue:[ |
213 | 224 |
l := (v / 10.0) asInteger * 10 |
55 | 225 |
] ifFalse:[ |
213 | 226 |
l := 0 |
55 | 227 |
]. |
85 | 228 |
v := v - ((v / 10.0) floor * 10) floor. |
55 | 229 |
l := l + v truncated. |
230 |
^ l * sign |
|
231 |
||
232 |
"12345.0 asInteger" |
|
233 |
"1e15 asInteger" |
|
701 | 234 |
! |
235 |
||
236 |
coerce:aNumber |
|
237 |
"return aNumber converted into receivers type" |
|
238 |
||
1200 | 239 |
"/ as soon as Float are float & Double are doubles, |
240 |
"/ use: |
|
241 |
"/ ^ aNumber asDouble |
|
242 |
||
243 |
^ aNumber asFloat |
|
244 |
||
245 |
"Modified: 17.4.1996 / 12:36:46 / cg" |
|
701 | 246 |
! |
247 |
||
248 |
generality |
|
249 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
250 |
||
251 |
^ 80 |
|
252 |
! ! |
|
253 |
||
254 |
!LimitedPrecisionReal methodsFor:'comparing'! |
|
255 |
||
256 |
< aNumber |
|
257 |
"return true, if the argument is greater" |
|
258 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
259 |
"/ 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
|
260 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
261 |
"/ ^ aNumber lessFromDouble:self asDouble |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
262 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
263 |
^ aNumber lessFromFloat:self asFloat |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
264 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
265 |
"Modified: 17.4.1996 / 13:34:50 / cg" |
701 | 266 |
! |
267 |
||
268 |
<= aNumber |
|
269 |
"return true, if the argument is greater or equal" |
|
270 |
||
271 |
^ self retry:#<= coercing:aNumber |
|
272 |
! |
|
273 |
||
274 |
= aNumber |
|
275 |
"return true, if the arguments value are equal" |
|
276 |
||
277 |
^ self retry:#= coercing:aNumber |
|
278 |
! |
|
279 |
||
280 |
> aNumber |
|
281 |
"return true, if the argument is less" |
|
282 |
||
283 |
^ self retry:#> coercing:aNumber |
|
284 |
! |
|
285 |
||
286 |
>= aNumber |
|
287 |
"return true, if the argument is less or equal" |
|
288 |
||
289 |
^ self retry:#>= coercing:aNumber |
|
290 |
! |
|
291 |
||
292 |
~= aNumber |
|
293 |
"return true, if the arguments value are not equal" |
|
294 |
||
295 |
^ self retry:#~= coercing:aNumber |
|
296 |
! ! |
|
297 |
||
298 |
!LimitedPrecisionReal methodsFor:'copying'! |
|
299 |
||
300 |
deepCopy |
|
301 |
"return a deep copy of myself |
|
302 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
303 |
||
304 |
^ self |
|
305 |
! |
|
306 |
||
307 |
deepCopyUsing:aDictionary |
|
308 |
"return a deep copy of myself |
|
309 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
310 |
||
311 |
^ self |
|
312 |
! |
|
313 |
||
314 |
shallowCopy |
|
315 |
"return a shallow copy of the receiver" |
|
316 |
||
317 |
^ self |
|
318 |
! |
|
319 |
||
320 |
simpleDeepCopy |
|
321 |
"return a deep copy of the receiver |
|
322 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
323 |
||
324 |
^ self |
|
55 | 325 |
! ! |
326 |
||
327 |
!LimitedPrecisionReal methodsFor:'double dispatching'! |
|
328 |
||
329 |
differenceFromFraction:aFraction |
|
213 | 330 |
"sent when a fraction does not know how to subtract the receiver, a float" |
55 | 331 |
|
332 |
|d| |
|
333 |
||
334 |
d := aFraction denominator. |
|
335 |
^ (aFraction numerator - (self * d)) / d |
|
336 |
! |
|
337 |
||
338 |
productFromFraction:aFraction |
|
213 | 339 |
"sent when a fraction does not know how to multiply the receiver, a float" |
55 | 340 |
|
341 |
^ self * aFraction numerator / aFraction denominator |
|
342 |
! |
|
343 |
||
344 |
quotientFromFraction:aFraction |
|
213 | 345 |
"sent when a fraction does not know how to divide by the receiver, a float" |
55 | 346 |
|
347 |
^ aFraction numerator / (self * aFraction denominator) |
|
701 | 348 |
! |
349 |
||
350 |
sumFromFraction:aFraction |
|
351 |
"sent when a fraction does not know how to add the receiver, a float" |
|
352 |
||
353 |
|d| |
|
354 |
||
355 |
d := aFraction denominator. |
|
356 |
^ (self * d + aFraction numerator) / d |
|
357 |
! ! |
|
358 |
||
359 |
!LimitedPrecisionReal methodsFor:'printing & storing'! |
|
360 |
||
361 |
printOn:aStream |
|
362 |
"append a printed representation of the receiver to |
|
363 |
the argument, aStream" |
|
364 |
||
365 |
aStream nextPutAll:self printString |
|
1200 | 366 |
! |
367 |
||
368 |
printString |
|
369 |
"return a printed representation of the receiver" |
|
370 |
||
371 |
^ self subclassResponsibility |
|
372 |
||
373 |
"Created: 17.4.1996 / 12:12:20 / cg" |
|
701 | 374 |
! ! |
375 |
||
376 |
!LimitedPrecisionReal methodsFor:'testing'! |
|
377 |
||
378 |
isReal |
|
379 |
"return true, if the receiver is some kind of real number; |
|
380 |
false is returned here - the method is redefined from Object." |
|
381 |
||
382 |
^ true |
|
383 |
! |
|
384 |
||
385 |
negative |
|
386 |
"return true if the receiver is less than zero" |
|
387 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
388 |
"/ 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
|
389 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
390 |
"/ ^ self asDouble negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
391 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
392 |
^ self asFloat negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
393 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
394 |
"Modified: 17.4.1996 / 13:35:00 / cg" |
701 | 395 |
! |
396 |
||
397 |
positive |
|
398 |
"return true if the receiver is greater or equal to zero" |
|
399 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
400 |
"/ 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
|
401 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
402 |
"/ ^ self asDouble positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
403 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
404 |
^ self asFloat positive |
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 |
"Modified: 17.4.1996 / 13:35:10 / cg" |
55 | 407 |
! ! |
408 |
||
409 |
!LimitedPrecisionReal methodsFor:'truncation and rounding'! |
|
410 |
||
411 |
fractionPart |
|
1865
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
412 |
"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
|
413 |
(i.e. the receiver minus its truncated value)" |
55 | 414 |
|
415 |
^ self - self truncated asFloat |
|
416 |
||
1865
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
417 |
" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
418 |
1234.56789 fractionPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
419 |
1.2345e6 fractionPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
420 |
" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
421 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
422 |
"Modified: 4.11.1996 / 20:26:54 / cg" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
423 |
! |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
424 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
425 |
integerPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
426 |
"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
|
427 |
(i.e. the truncated value)" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
428 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
429 |
^ self truncated asFloat |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
430 |
|
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 integerPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
433 |
1.2345e6 integerPart |
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:21 / cg" |
55 | 437 |
! ! |
438 |
||
701 | 439 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
55 | 440 |
|
701 | 441 |
version |
2141 | 442 |
^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.18 1997-01-10 19:01:23 cg Exp $' |
55 | 443 |
! ! |