author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 19:02:53 +0200 | |
changeset 1295 | 83f594f05c52 |
parent 1201 | 24a5faa7e305 |
child 1865 | 5168897c326f |
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 |
|
88 | 46 |
" |
47 |
! ! |
|
55 | 48 |
|
49 |
!LimitedPrecisionReal class methodsFor:'instance creation'! |
|
50 |
||
51 |
new:aNumber |
|
52 |
"catch this message - not allowed for floats/doubles" |
|
53 |
||
54 |
self error:'Floats/Doubles cannot be created with new:' |
|
55 |
! ! |
|
56 |
||
57 |
!LimitedPrecisionReal methodsFor:'accessing'! |
|
58 |
||
59 |
at:index |
|
60 |
"redefined to prevent access to individual bytes in a real." |
|
61 |
||
62 |
self error:'not allowed for floats/doubles' |
|
63 |
! |
|
64 |
||
65 |
at:index put:aValue |
|
66 |
"redefined to prevent access to individual bytes in a real" |
|
67 |
||
68 |
self error:'not allowed for floats/doubles' |
|
701 | 69 |
! |
70 |
||
71 |
size |
|
72 |
"redefined since reals are kludgy (ByteArry)" |
|
73 |
||
74 |
^ 0 |
|
55 | 75 |
! ! |
76 |
||
77 |
!LimitedPrecisionReal methodsFor:'arithmetic'! |
|
78 |
||
701 | 79 |
* aNumber |
80 |
"return the product of the receiver and the argument, aNumber" |
|
81 |
||
1200 | 82 |
"/ as soon as Float are float & Double are doubles, |
83 |
"/ use: |
|
84 |
"/ ^ aNumber productFromDouble:self asDouble |
|
85 |
||
86 |
^ aNumber productFromFloat:self asFloat |
|
87 |
||
88 |
"Modified: 17.4.1996 / 12:35:36 / cg" |
|
701 | 89 |
! |
90 |
||
55 | 91 |
+ aNumber |
92 |
"return the sum of the receiver and the argument, aNumber" |
|
93 |
||
1200 | 94 |
"/ as soon as Float are float & Double are doubles, |
95 |
"/ use: |
|
96 |
"/ ^ aNumber sumFromDouble:self asDouble |
|
97 |
||
98 |
^ aNumber sumFromFloat:self asFloat |
|
99 |
||
100 |
"Modified: 17.4.1996 / 12:35:55 / cg" |
|
55 | 101 |
! |
102 |
||
103 |
- aNumber |
|
104 |
"return the difference of the receiver and the argument, aNumber" |
|
105 |
||
1200 | 106 |
"/ as soon as Float are float & Double are doubles, |
107 |
"/ use: |
|
108 |
"/ ^ aNumber differenceFromDouble:self asDouble |
|
109 |
||
110 |
^ aNumber differenceFromFloat:self asFloat |
|
111 |
||
112 |
"Modified: 17.4.1996 / 12:36:07 / cg" |
|
55 | 113 |
! |
114 |
||
115 |
/ aNumber |
|
116 |
"return the quotient of the receiver and the argument, aNumber" |
|
117 |
||
118 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
1200 | 119 |
^ DivisionByZeroSignal raise. |
55 | 120 |
]. |
1200 | 121 |
"/ as soon as Float are float & Double are doubles, |
122 |
"/ use: |
|
123 |
"/ ^ aNumber quotientFromDouble:self asDouble |
|
124 |
||
125 |
^ aNumber quotientFromFloat:self asFloat |
|
126 |
||
127 |
"Modified: 17.4.1996 / 12:36:21 / cg" |
|
55 | 128 |
! |
129 |
||
130 |
// aNumber |
|
131 |
"return the integer quotient of dividing the receiver by aNumber with |
|
132 |
truncation towards negative infinity." |
|
133 |
||
134 |
^ (self / aNumber) floor asInteger |
|
135 |
! |
|
136 |
||
137 |
\\ aNumber |
|
138 |
"return the integer remainder of dividing the receiver by aNumber with |
|
139 |
truncation towards negative infinity." |
|
140 |
||
141 |
^ (self - ((self / aNumber) floor * aNumber)) floor asInteger |
|
142 |
! ! |
|
143 |
||
144 |
!LimitedPrecisionReal methodsFor:'coercion and converting'! |
|
145 |
||
146 |
asFraction |
|
147 |
"return a corresponding fraction |
|
148 |
- notice, that 'aFract asFloat asFraction' does not always return |
|
149 |
a good fraction ... due to rounding errors when converting to float" |
|
150 |
||
151 |
|fract digits power num denom| |
|
152 |
||
153 |
"we (indirectly) use printf which knows the precision of floats" |
|
154 |
||
155 |
fract := self fractionPart. |
|
156 |
digits := fract printString copyFrom:3. |
|
157 |
power := digits size. |
|
158 |
num := (self - fract) asInteger. |
|
159 |
denom := (10 raisedToInteger:power). |
|
160 |
num := num * denom. |
|
161 |
num := num + (Integer readFromString:digits). |
|
162 |
^ (Fraction numerator:num denominator:denom) reduced |
|
163 |
||
164 |
"0.3 asFraction" |
|
165 |
"0.5 asFraction" |
|
166 |
"(1/5) asFloat asFraction" |
|
167 |
"(1/8) asFloat asFraction" |
|
168 |
"(1/13) asFloat asFraction -> inexact result due to rounding errors" |
|
169 |
! |
|
170 |
||
171 |
asInteger |
|
172 |
"return an integer with same value - might truncate" |
|
173 |
||
174 |
|l v sign| |
|
175 |
||
176 |
"this is stupid code - rounding errors accumulate; fix later" |
|
177 |
||
178 |
sign := self sign. |
|
179 |
v := self abs. |
|
85 | 180 |
(v >= 10.0) ifTrue:[ |
213 | 181 |
l := (v / 10.0) asInteger * 10 |
55 | 182 |
] ifFalse:[ |
213 | 183 |
l := 0 |
55 | 184 |
]. |
85 | 185 |
v := v - ((v / 10.0) floor * 10) floor. |
55 | 186 |
l := l + v truncated. |
187 |
^ l * sign |
|
188 |
||
189 |
"12345.0 asInteger" |
|
190 |
"1e15 asInteger" |
|
701 | 191 |
! |
192 |
||
193 |
coerce:aNumber |
|
194 |
"return aNumber converted into receivers type" |
|
195 |
||
1200 | 196 |
"/ as soon as Float are float & Double are doubles, |
197 |
"/ use: |
|
198 |
"/ ^ aNumber asDouble |
|
199 |
||
200 |
^ aNumber asFloat |
|
201 |
||
202 |
"Modified: 17.4.1996 / 12:36:46 / cg" |
|
701 | 203 |
! |
204 |
||
205 |
generality |
|
206 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
207 |
||
208 |
^ 80 |
|
209 |
! ! |
|
210 |
||
211 |
!LimitedPrecisionReal methodsFor:'comparing'! |
|
212 |
||
213 |
< aNumber |
|
214 |
"return true, if the argument is greater" |
|
215 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
216 |
"/ 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
|
217 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
218 |
"/ ^ aNumber lessFromDouble:self asDouble |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
219 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
220 |
^ aNumber lessFromFloat:self asFloat |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
221 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
222 |
"Modified: 17.4.1996 / 13:34:50 / cg" |
701 | 223 |
! |
224 |
||
225 |
<= aNumber |
|
226 |
"return true, if the argument is greater or equal" |
|
227 |
||
228 |
^ self retry:#<= coercing:aNumber |
|
229 |
! |
|
230 |
||
231 |
= aNumber |
|
232 |
"return true, if the arguments value are equal" |
|
233 |
||
234 |
^ self retry:#= coercing:aNumber |
|
235 |
! |
|
236 |
||
237 |
> aNumber |
|
238 |
"return true, if the argument is less" |
|
239 |
||
240 |
^ self retry:#> coercing:aNumber |
|
241 |
! |
|
242 |
||
243 |
>= aNumber |
|
244 |
"return true, if the argument is less or equal" |
|
245 |
||
246 |
^ self retry:#>= coercing:aNumber |
|
247 |
! |
|
248 |
||
249 |
~= aNumber |
|
250 |
"return true, if the arguments value are not equal" |
|
251 |
||
252 |
^ self retry:#~= coercing:aNumber |
|
253 |
! ! |
|
254 |
||
255 |
!LimitedPrecisionReal methodsFor:'copying'! |
|
256 |
||
257 |
deepCopy |
|
258 |
"return a deep copy of myself |
|
259 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
260 |
||
261 |
^ self |
|
262 |
! |
|
263 |
||
264 |
deepCopyUsing:aDictionary |
|
265 |
"return a deep copy of myself |
|
266 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
267 |
||
268 |
^ self |
|
269 |
! |
|
270 |
||
271 |
shallowCopy |
|
272 |
"return a shallow copy of the receiver" |
|
273 |
||
274 |
^ self |
|
275 |
! |
|
276 |
||
277 |
simpleDeepCopy |
|
278 |
"return a deep copy of the receiver |
|
279 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
280 |
||
281 |
^ self |
|
55 | 282 |
! ! |
283 |
||
284 |
!LimitedPrecisionReal methodsFor:'double dispatching'! |
|
285 |
||
286 |
differenceFromFraction:aFraction |
|
213 | 287 |
"sent when a fraction does not know how to subtract the receiver, a float" |
55 | 288 |
|
289 |
|d| |
|
290 |
||
291 |
d := aFraction denominator. |
|
292 |
^ (aFraction numerator - (self * d)) / d |
|
293 |
! |
|
294 |
||
295 |
productFromFraction:aFraction |
|
213 | 296 |
"sent when a fraction does not know how to multiply the receiver, a float" |
55 | 297 |
|
298 |
^ self * aFraction numerator / aFraction denominator |
|
299 |
! |
|
300 |
||
301 |
quotientFromFraction:aFraction |
|
213 | 302 |
"sent when a fraction does not know how to divide by the receiver, a float" |
55 | 303 |
|
304 |
^ aFraction numerator / (self * aFraction denominator) |
|
701 | 305 |
! |
306 |
||
307 |
sumFromFraction:aFraction |
|
308 |
"sent when a fraction does not know how to add the receiver, a float" |
|
309 |
||
310 |
|d| |
|
311 |
||
312 |
d := aFraction denominator. |
|
313 |
^ (self * d + aFraction numerator) / d |
|
314 |
! ! |
|
315 |
||
316 |
!LimitedPrecisionReal methodsFor:'printing & storing'! |
|
317 |
||
318 |
printOn:aStream |
|
319 |
"append a printed representation of the receiver to |
|
320 |
the argument, aStream" |
|
321 |
||
322 |
aStream nextPutAll:self printString |
|
1200 | 323 |
! |
324 |
||
325 |
printString |
|
326 |
"return a printed representation of the receiver" |
|
327 |
||
328 |
^ self subclassResponsibility |
|
329 |
||
330 |
"Created: 17.4.1996 / 12:12:20 / cg" |
|
701 | 331 |
! ! |
332 |
||
333 |
!LimitedPrecisionReal methodsFor:'testing'! |
|
334 |
||
335 |
isReal |
|
336 |
"return true, if the receiver is some kind of real number; |
|
337 |
false is returned here - the method is redefined from Object." |
|
338 |
||
339 |
^ true |
|
340 |
! |
|
341 |
||
342 |
negative |
|
343 |
"return true if the receiver is less than zero" |
|
344 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
345 |
"/ 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
|
346 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
347 |
"/ ^ self asDouble negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
348 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
349 |
^ self asFloat negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
350 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
351 |
"Modified: 17.4.1996 / 13:35:00 / cg" |
701 | 352 |
! |
353 |
||
354 |
positive |
|
355 |
"return true if the receiver is greater or equal to zero" |
|
356 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
357 |
"/ 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
|
358 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
359 |
"/ ^ self asDouble positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
360 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
361 |
^ self asFloat positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
362 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
363 |
"Modified: 17.4.1996 / 13:35:10 / cg" |
55 | 364 |
! ! |
365 |
||
366 |
!LimitedPrecisionReal methodsFor:'truncation and rounding'! |
|
367 |
||
368 |
fractionPart |
|
369 |
"return a float with value from digits after the decimal point" |
|
370 |
||
371 |
^ self - self truncated asFloat |
|
372 |
||
373 |
"1234.56789 fractionPart" |
|
374 |
"1.2345e6 fractionPart" |
|
375 |
! ! |
|
376 |
||
701 | 377 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
55 | 378 |
|
701 | 379 |
version |
1295 | 380 |
^ '$Header: /cvs/stx/stx/libbasic/Attic/LPReal.st,v 1.14 1996-04-25 17:00:05 cg Exp $' |
55 | 381 |
! ! |