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