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