author | Claus Gittinger <cg@exept.de> |
Fri, 08 Nov 1996 16:27:05 +0100 | |
changeset 1938 | e1a56370a833 |
parent 1893 | c66af5c46272 |
child 2141 | bba0027204fd |
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 |
||
151 |
asFraction |
|
152 |
"return a corresponding fraction |
|
153 |
- notice, that 'aFract asFloat asFraction' does not always return |
|
154 |
a good fraction ... due to rounding errors when converting to float" |
|
155 |
||
156 |
|fract digits power num denom| |
|
157 |
||
158 |
"we (indirectly) use printf which knows the precision of floats" |
|
159 |
||
160 |
fract := self fractionPart. |
|
161 |
digits := fract printString copyFrom:3. |
|
162 |
power := digits size. |
|
163 |
num := (self - fract) asInteger. |
|
164 |
denom := (10 raisedToInteger:power). |
|
165 |
num := num * denom. |
|
166 |
num := num + (Integer readFromString:digits). |
|
167 |
^ (Fraction numerator:num denominator:denom) reduced |
|
168 |
||
169 |
"0.3 asFraction" |
|
170 |
"0.5 asFraction" |
|
171 |
"(1/5) asFloat asFraction" |
|
172 |
"(1/8) asFloat asFraction" |
|
173 |
"(1/13) asFloat asFraction -> inexact result due to rounding errors" |
|
174 |
! |
|
175 |
||
176 |
asInteger |
|
177 |
"return an integer with same value - might truncate" |
|
178 |
||
179 |
|l v sign| |
|
180 |
||
181 |
"this is stupid code - rounding errors accumulate; fix later" |
|
182 |
||
183 |
sign := self sign. |
|
184 |
v := self abs. |
|
85 | 185 |
(v >= 10.0) ifTrue:[ |
213 | 186 |
l := (v / 10.0) asInteger * 10 |
55 | 187 |
] ifFalse:[ |
213 | 188 |
l := 0 |
55 | 189 |
]. |
85 | 190 |
v := v - ((v / 10.0) floor * 10) floor. |
55 | 191 |
l := l + v truncated. |
192 |
^ l * sign |
|
193 |
||
194 |
"12345.0 asInteger" |
|
195 |
"1e15 asInteger" |
|
701 | 196 |
! |
197 |
||
198 |
coerce:aNumber |
|
199 |
"return aNumber converted into receivers type" |
|
200 |
||
1200 | 201 |
"/ as soon as Float are float & Double are doubles, |
202 |
"/ use: |
|
203 |
"/ ^ aNumber asDouble |
|
204 |
||
205 |
^ aNumber asFloat |
|
206 |
||
207 |
"Modified: 17.4.1996 / 12:36:46 / cg" |
|
701 | 208 |
! |
209 |
||
210 |
generality |
|
211 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
212 |
||
213 |
^ 80 |
|
214 |
! ! |
|
215 |
||
216 |
!LimitedPrecisionReal methodsFor:'comparing'! |
|
217 |
||
218 |
< aNumber |
|
219 |
"return true, if the argument is greater" |
|
220 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
221 |
"/ 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
|
222 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
223 |
"/ ^ aNumber lessFromDouble:self asDouble |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
224 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
225 |
^ aNumber lessFromFloat:self asFloat |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
226 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
227 |
"Modified: 17.4.1996 / 13:34:50 / cg" |
701 | 228 |
! |
229 |
||
230 |
<= aNumber |
|
231 |
"return true, if the argument is greater or equal" |
|
232 |
||
233 |
^ self retry:#<= coercing:aNumber |
|
234 |
! |
|
235 |
||
236 |
= aNumber |
|
237 |
"return true, if the arguments value are equal" |
|
238 |
||
239 |
^ self retry:#= coercing:aNumber |
|
240 |
! |
|
241 |
||
242 |
> aNumber |
|
243 |
"return true, if the argument is less" |
|
244 |
||
245 |
^ self retry:#> coercing:aNumber |
|
246 |
! |
|
247 |
||
248 |
>= aNumber |
|
249 |
"return true, if the argument is less or equal" |
|
250 |
||
251 |
^ self retry:#>= coercing:aNumber |
|
252 |
! |
|
253 |
||
254 |
~= aNumber |
|
255 |
"return true, if the arguments value are not equal" |
|
256 |
||
257 |
^ self retry:#~= coercing:aNumber |
|
258 |
! ! |
|
259 |
||
260 |
!LimitedPrecisionReal methodsFor:'copying'! |
|
261 |
||
262 |
deepCopy |
|
263 |
"return a deep copy of myself |
|
264 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
265 |
||
266 |
^ self |
|
267 |
! |
|
268 |
||
269 |
deepCopyUsing:aDictionary |
|
270 |
"return a deep copy of myself |
|
271 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
272 |
||
273 |
^ self |
|
274 |
! |
|
275 |
||
276 |
shallowCopy |
|
277 |
"return a shallow copy of the receiver" |
|
278 |
||
279 |
^ self |
|
280 |
! |
|
281 |
||
282 |
simpleDeepCopy |
|
283 |
"return a deep copy of the receiver |
|
284 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
285 |
||
286 |
^ self |
|
55 | 287 |
! ! |
288 |
||
289 |
!LimitedPrecisionReal methodsFor:'double dispatching'! |
|
290 |
||
291 |
differenceFromFraction:aFraction |
|
213 | 292 |
"sent when a fraction does not know how to subtract the receiver, a float" |
55 | 293 |
|
294 |
|d| |
|
295 |
||
296 |
d := aFraction denominator. |
|
297 |
^ (aFraction numerator - (self * d)) / d |
|
298 |
! |
|
299 |
||
300 |
productFromFraction:aFraction |
|
213 | 301 |
"sent when a fraction does not know how to multiply the receiver, a float" |
55 | 302 |
|
303 |
^ self * aFraction numerator / aFraction denominator |
|
304 |
! |
|
305 |
||
306 |
quotientFromFraction:aFraction |
|
213 | 307 |
"sent when a fraction does not know how to divide by the receiver, a float" |
55 | 308 |
|
309 |
^ aFraction numerator / (self * aFraction denominator) |
|
701 | 310 |
! |
311 |
||
312 |
sumFromFraction:aFraction |
|
313 |
"sent when a fraction does not know how to add the receiver, a float" |
|
314 |
||
315 |
|d| |
|
316 |
||
317 |
d := aFraction denominator. |
|
318 |
^ (self * d + aFraction numerator) / d |
|
319 |
! ! |
|
320 |
||
321 |
!LimitedPrecisionReal methodsFor:'printing & storing'! |
|
322 |
||
323 |
printOn:aStream |
|
324 |
"append a printed representation of the receiver to |
|
325 |
the argument, aStream" |
|
326 |
||
327 |
aStream nextPutAll:self printString |
|
1200 | 328 |
! |
329 |
||
330 |
printString |
|
331 |
"return a printed representation of the receiver" |
|
332 |
||
333 |
^ self subclassResponsibility |
|
334 |
||
335 |
"Created: 17.4.1996 / 12:12:20 / cg" |
|
701 | 336 |
! ! |
337 |
||
338 |
!LimitedPrecisionReal methodsFor:'testing'! |
|
339 |
||
340 |
isReal |
|
341 |
"return true, if the receiver is some kind of real number; |
|
342 |
false is returned here - the method is redefined from Object." |
|
343 |
||
344 |
^ true |
|
345 |
! |
|
346 |
||
347 |
negative |
|
348 |
"return true if the receiver is less than zero" |
|
349 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
350 |
"/ 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
|
351 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
352 |
"/ ^ self asDouble negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
353 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
354 |
^ self asFloat negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
355 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
356 |
"Modified: 17.4.1996 / 13:35:00 / cg" |
701 | 357 |
! |
358 |
||
359 |
positive |
|
360 |
"return true if the receiver is greater or equal to zero" |
|
361 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
362 |
"/ 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
|
363 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
364 |
"/ ^ self asDouble positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
365 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
366 |
^ self asFloat positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
367 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
368 |
"Modified: 17.4.1996 / 13:35:10 / cg" |
55 | 369 |
! ! |
370 |
||
371 |
!LimitedPrecisionReal methodsFor:'truncation and rounding'! |
|
372 |
||
373 |
fractionPart |
|
1865
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
374 |
"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
|
375 |
(i.e. the receiver minus its truncated value)" |
55 | 376 |
|
377 |
^ self - self truncated asFloat |
|
378 |
||
1865
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
379 |
" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
380 |
1234.56789 fractionPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
381 |
1.2345e6 fractionPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
382 |
" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
383 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
384 |
"Modified: 4.11.1996 / 20:26:54 / cg" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
385 |
! |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
386 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
387 |
integerPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
388 |
"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
|
389 |
(i.e. the truncated value)" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
390 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
391 |
^ self truncated asFloat |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
392 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
393 |
" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
394 |
1234.56789 integerPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
395 |
1.2345e6 integerPart |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
396 |
" |
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
397 |
|
5168897c326f
added integerPart for protocol completeness
Claus Gittinger <cg@exept.de>
parents:
1295
diff
changeset
|
398 |
"Modified: 4.11.1996 / 20:26:21 / cg" |
55 | 399 |
! ! |
400 |
||
701 | 401 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
55 | 402 |
|
701 | 403 |
version |
1893 | 404 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/LPReal.st,v 1.17 1996-11-05 18:38:50 cg Exp $' |
55 | 405 |
! ! |