author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 15:23:19 +0100 | |
changeset 624 | f09ad5cf21d4 |
parent 530 | 07d0bce293c9 |
child 701 | a309e3ef7faf |
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 |
|
14 |
instanceVariableNames:'' |
|
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Magnitude-Numbers' |
|
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 |
version |
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
482
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.10 1995-11-11 15:23:39 cg Exp $' |
88 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
Abstract superclass for single and double (and maybe more) |
|
43 |
precision real numbers (i.e. Float and Double). |
|
92 | 44 |
|
45 |
Due to historic reasons, ST/X's Floats are what Doubles are in ST-80. |
|
46 |
This may change soon (implementing LPReal is a first step towards this). |
|
88 | 47 |
" |
48 |
! ! |
|
55 | 49 |
|
50 |
!LimitedPrecisionReal class methodsFor:'instance creation'! |
|
51 |
||
52 |
new:aNumber |
|
53 |
"catch this message - not allowed for floats/doubles" |
|
54 |
||
55 |
self error:'Floats/Doubles cannot be created with new:' |
|
56 |
! ! |
|
57 |
||
58 |
!LimitedPrecisionReal methodsFor:'copying'! |
|
59 |
||
60 |
shallowCopy |
|
61 |
"return a shallow copy of the receiver" |
|
62 |
||
63 |
^ self |
|
64 |
! |
|
65 |
||
66 |
simpleDeepCopy |
|
67 |
"return a deep copy of the receiver |
|
68 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
69 |
||
70 |
^ self |
|
71 |
! |
|
72 |
||
73 |
deepCopyUsing:aDictionary |
|
74 |
"return a deep copy of myself |
|
75 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
76 |
||
77 |
^ self |
|
78 |
! |
|
79 |
||
80 |
deepCopy |
|
81 |
"return a deep copy of myself |
|
82 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
83 |
||
84 |
^ self |
|
85 |
! ! |
|
86 |
||
87 |
!LimitedPrecisionReal methodsFor:'accessing'! |
|
88 |
||
89 |
size |
|
90 |
"redefined since reals are kludgy (ByteArry)" |
|
91 |
||
92 |
^ 0 |
|
93 |
! |
|
94 |
||
95 |
at:index |
|
96 |
"redefined to prevent access to individual bytes in a real." |
|
97 |
||
98 |
self error:'not allowed for floats/doubles' |
|
99 |
! |
|
100 |
||
101 |
at:index put:aValue |
|
102 |
"redefined to prevent access to individual bytes in a real" |
|
103 |
||
104 |
self error:'not allowed for floats/doubles' |
|
105 |
! ! |
|
106 |
||
107 |
!LimitedPrecisionReal methodsFor:'arithmetic'! |
|
108 |
||
109 |
+ aNumber |
|
110 |
"return the sum of the receiver and the argument, aNumber" |
|
111 |
||
112 |
^ aNumber sumFromDouble:self asDouble |
|
113 |
! |
|
114 |
||
115 |
- aNumber |
|
116 |
"return the difference of the receiver and the argument, aNumber" |
|
117 |
||
118 |
^ aNumber differenceFromDouble:self asDouble |
|
119 |
! |
|
120 |
||
121 |
* aNumber |
|
122 |
"return the product of the receiver and the argument, aNumber" |
|
123 |
||
124 |
^ aNumber productFromDouble:self asDouble |
|
125 |
! |
|
126 |
||
127 |
/ aNumber |
|
128 |
"return the quotient of the receiver and the argument, aNumber" |
|
129 |
||
130 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
213 | 131 |
^ DivisionByZeroSignal raise. |
55 | 132 |
]. |
133 |
^ aNumber quotientFromDouble:self asDouble |
|
134 |
! |
|
135 |
||
136 |
// aNumber |
|
137 |
"return the integer quotient of dividing the receiver by aNumber with |
|
138 |
truncation towards negative infinity." |
|
139 |
||
140 |
^ (self / aNumber) floor asInteger |
|
141 |
! |
|
142 |
||
143 |
\\ aNumber |
|
144 |
"return the integer remainder of dividing the receiver by aNumber with |
|
145 |
truncation towards negative infinity." |
|
146 |
||
147 |
^ (self - ((self / aNumber) floor * aNumber)) floor asInteger |
|
148 |
! ! |
|
149 |
||
150 |
!LimitedPrecisionReal methodsFor:'testing'! |
|
151 |
||
152 |
positive |
|
153 |
"return true if the receiver is greater or equal to zero" |
|
154 |
||
155 |
^ self asDouble positive |
|
156 |
! |
|
157 |
||
158 |
negative |
|
159 |
"return true if the receiver is less than zero" |
|
160 |
||
161 |
^ self asDouble negative |
|
482 | 162 |
! |
163 |
||
164 |
isReal |
|
165 |
"return true, if the receiver is some kind of real number; |
|
166 |
false is returned here - the method is redefined from Object." |
|
167 |
||
168 |
^ true |
|
55 | 169 |
! ! |
170 |
||
171 |
!LimitedPrecisionReal methodsFor:'comparing'! |
|
172 |
||
173 |
< aNumber |
|
174 |
"return true, if the argument is greater" |
|
175 |
||
176 |
^ aNumber lessFromDouble:self asDouble |
|
177 |
! |
|
178 |
||
179 |
> aNumber |
|
180 |
"return true, if the argument is less" |
|
181 |
||
182 |
^ self retry:#> coercing:aNumber |
|
183 |
! |
|
184 |
||
185 |
<= aNumber |
|
186 |
"return true, if the argument is greater or equal" |
|
187 |
||
188 |
^ self retry:#<= coercing:aNumber |
|
189 |
! |
|
190 |
||
191 |
>= aNumber |
|
192 |
"return true, if the argument is less or equal" |
|
193 |
||
194 |
^ self retry:#>= coercing:aNumber |
|
195 |
! |
|
196 |
||
197 |
= aNumber |
|
198 |
"return true, if the arguments value are equal" |
|
199 |
||
200 |
^ self retry:#= coercing:aNumber |
|
201 |
! |
|
202 |
||
203 |
~= aNumber |
|
204 |
"return true, if the arguments value are not equal" |
|
205 |
||
206 |
^ self retry:#~= coercing:aNumber |
|
207 |
! ! |
|
208 |
||
209 |
!LimitedPrecisionReal methodsFor:'coercion and converting'! |
|
210 |
||
211 |
coerce:aNumber |
|
212 |
"return aNumber converted into receivers type" |
|
213 |
||
214 |
^ aNumber asDouble |
|
215 |
! |
|
216 |
||
217 |
generality |
|
218 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
219 |
||
220 |
^ 80 |
|
221 |
! |
|
222 |
||
223 |
asFraction |
|
224 |
"return a corresponding fraction |
|
225 |
- notice, that 'aFract asFloat asFraction' does not always return |
|
226 |
a good fraction ... due to rounding errors when converting to float" |
|
227 |
||
228 |
|fract digits power num denom| |
|
229 |
||
230 |
"we (indirectly) use printf which knows the precision of floats" |
|
231 |
||
232 |
fract := self fractionPart. |
|
233 |
digits := fract printString copyFrom:3. |
|
234 |
power := digits size. |
|
235 |
num := (self - fract) asInteger. |
|
236 |
denom := (10 raisedToInteger:power). |
|
237 |
num := num * denom. |
|
238 |
num := num + (Integer readFromString:digits). |
|
239 |
^ (Fraction numerator:num denominator:denom) reduced |
|
240 |
||
241 |
"0.3 asFraction" |
|
242 |
"0.5 asFraction" |
|
243 |
"(1/5) asFloat asFraction" |
|
244 |
"(1/8) asFloat asFraction" |
|
245 |
"(1/13) asFloat asFraction -> inexact result due to rounding errors" |
|
246 |
! |
|
247 |
||
248 |
asInteger |
|
249 |
"return an integer with same value - might truncate" |
|
250 |
||
251 |
|l v sign| |
|
252 |
||
253 |
"this is stupid code - rounding errors accumulate; fix later" |
|
254 |
||
255 |
sign := self sign. |
|
256 |
v := self abs. |
|
85 | 257 |
(v >= 10.0) ifTrue:[ |
213 | 258 |
l := (v / 10.0) asInteger * 10 |
55 | 259 |
] ifFalse:[ |
213 | 260 |
l := 0 |
55 | 261 |
]. |
85 | 262 |
v := v - ((v / 10.0) floor * 10) floor. |
55 | 263 |
l := l + v truncated. |
264 |
^ l * sign |
|
265 |
||
266 |
"12345.0 asInteger" |
|
267 |
"1e15 asInteger" |
|
268 |
! ! |
|
269 |
||
270 |
!LimitedPrecisionReal methodsFor:'double dispatching'! |
|
271 |
||
272 |
sumFromFraction:aFraction |
|
213 | 273 |
"sent when a fraction does not know how to add the receiver, a float" |
55 | 274 |
|
275 |
|d| |
|
276 |
||
277 |
d := aFraction denominator. |
|
278 |
^ (self * d + aFraction numerator) / d |
|
279 |
! |
|
280 |
||
281 |
differenceFromFraction:aFraction |
|
213 | 282 |
"sent when a fraction does not know how to subtract the receiver, a float" |
55 | 283 |
|
284 |
|d| |
|
285 |
||
286 |
d := aFraction denominator. |
|
287 |
^ (aFraction numerator - (self * d)) / d |
|
288 |
! |
|
289 |
||
290 |
productFromFraction:aFraction |
|
213 | 291 |
"sent when a fraction does not know how to multiply the receiver, a float" |
55 | 292 |
|
293 |
^ self * aFraction numerator / aFraction denominator |
|
294 |
! |
|
295 |
||
296 |
quotientFromFraction:aFraction |
|
213 | 297 |
"sent when a fraction does not know how to divide by the receiver, a float" |
55 | 298 |
|
299 |
^ aFraction numerator / (self * aFraction denominator) |
|
300 |
! ! |
|
301 |
||
302 |
!LimitedPrecisionReal methodsFor:'truncation and rounding'! |
|
303 |
||
304 |
fractionPart |
|
305 |
"return a float with value from digits after the decimal point" |
|
306 |
||
307 |
^ self - self truncated asFloat |
|
308 |
||
309 |
"1234.56789 fractionPart" |
|
310 |
"1.2345e6 fractionPart" |
|
311 |
! ! |
|
312 |
||
92 | 313 |
!LimitedPrecisionReal methodsFor:'printing & storing'! |
55 | 314 |
|
315 |
printOn:aStream |
|
316 |
"append a printed representation of the receiver to |
|
317 |
the argument, aStream" |
|
318 |
||
319 |
aStream nextPutAll:self printString |
|
320 |
! ! |