author | Claus Gittinger <cg@exept.de> |
Tue, 17 Jun 2003 16:11:23 +0200 | |
changeset 7403 | 857702a5a921 |
parent 7398 | 0f96525b931e |
child 7405 | 9b0334a4591b |
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 |
||
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
13 |
"{ Package: 'stx:libbasic' }" |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
14 |
|
55 | 15 |
Number subclass:#LimitedPrecisionReal |
1200 | 16 |
instanceVariableNames:'' |
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Magnitude-Numbers' |
|
55 | 20 |
! |
21 |
||
88 | 22 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
213 | 27 |
All Rights Reserved |
55 | 28 |
|
88 | 29 |
This software is furnished under a license and may be used |
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
55 | 37 |
|
88 | 38 |
documentation |
39 |
" |
|
40 |
Abstract superclass for single and double (and maybe more) |
|
41 |
precision real numbers (i.e. Float and Double). |
|
92 | 42 |
|
43 |
Due to historic reasons, ST/X's Floats are what Doubles are in ST-80. |
|
44 |
This may change soon (implementing LPReal is a first step towards this). |
|
1295 | 45 |
|
46 |
[author:] |
|
47 |
Claus Gittinger |
|
1893 | 48 |
|
49 |
[see also:] |
|
50 |
Fraction FixedPoint |
|
88 | 51 |
" |
52 |
! ! |
|
55 | 53 |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
54 |
!LimitedPrecisionReal class methodsFor:'initialization'! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
55 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
56 |
initialize |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
57 |
"initialize ANSI compliant float globals" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
58 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
59 |
Smalltalk at:#FloatE put:ShortFloat. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
60 |
Smalltalk at:#FloatD put:Float. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
61 |
Smalltalk at:#FloatQ put:LongFloat. |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
62 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
63 |
" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
64 |
self initialize |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
65 |
" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
66 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
67 |
"Created: / 7.9.2001 / 14:02:45 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
68 |
"Modified: / 7.9.2001 / 14:02:54 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
69 |
! ! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
70 |
|
55 | 71 |
!LimitedPrecisionReal class methodsFor:'instance creation'! |
72 |
||
73 |
new:aNumber |
|
74 |
"catch this message - not allowed for floats/doubles" |
|
75 |
||
76 |
self error:'Floats/Doubles cannot be created with new:' |
|
77 |
! ! |
|
78 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
79 |
!LimitedPrecisionReal class methodsFor:'constants & defaults'! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
80 |
|
7390 | 81 |
NaN |
82 |
"return the constant NaN (not a Number)." |
|
83 |
||
84 |
^ self zero uncheckedDivide:self zero |
|
85 |
||
86 |
" |
|
87 |
ShortFloat NaN |
|
88 |
Float NaN |
|
89 |
LongFloat NaN |
|
90 |
" |
|
91 |
! |
|
92 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
93 |
e |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
94 |
"return the closest approximation of the irrational number e" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
95 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
96 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
97 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
98 |
"Modified: / 7.9.2001 / 14:05:02 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
99 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
100 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
101 |
emax |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
102 |
"return the largest exponent" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
103 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
104 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
105 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
106 |
"Created: / 7.9.2001 / 14:05:28 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
107 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
108 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
109 |
emin |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
110 |
"return the smallest exponent" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
111 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
112 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
113 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
114 |
"Created: / 7.9.2001 / 14:05:35 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
115 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
116 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
117 |
epsilon |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
118 |
"return the maximum relative spacing" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
119 |
|
6575
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
120 |
^ self radix asFloat raisedTo:(1 - self precision) |
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
121 |
|
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
122 |
" |
7356 | 123 |
ShortFloat epsilon |
124 |
Float epsilon |
|
125 |
LongFloat epsilon |
|
6575
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
126 |
" |
b07cd54c4375
Define #decimalPrecision, #epsilon
Stefan Vogel <sv@exept.de>
parents:
6195
diff
changeset
|
127 |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
128 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
129 |
"Created: / 7.9.2001 / 14:06:41 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
130 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
131 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
132 |
fmax |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
133 |
"return the largest value allowed" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
134 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
135 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
136 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
137 |
"Created: / 7.9.2001 / 14:06:56 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
138 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
139 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
140 |
fmin |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
141 |
"return the minimum value allowed" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
142 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
143 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
144 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
145 |
"Created: / 7.9.2001 / 14:07:06 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
146 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
147 |
|
7390 | 148 |
infinity |
149 |
"return a float representing infinity" |
|
150 |
||
151 |
^ self unity uncheckedDivide:self zero |
|
152 |
||
153 |
" |
|
154 |
ShortFloat infinity |
|
155 |
Float infinity |
|
156 |
LongFloat infinity |
|
157 |
" |
|
158 |
! |
|
159 |
||
160 |
negativeInfinity |
|
161 |
"return a float representing negative infinity" |
|
162 |
||
163 |
^ self unity negated uncheckedDivide:self zero |
|
164 |
||
165 |
" |
|
166 |
ShortFloat negativeInfinity |
|
167 |
Float negativeInfinity |
|
168 |
LongFloat negativeInfinity |
|
169 |
" |
|
170 |
! |
|
171 |
||
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
172 |
pi |
7356 | 173 |
"return the closest approximation of the irrational number pi" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
174 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
175 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
176 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
177 |
"Created: / 7.9.2001 / 14:07:35 / cg" |
7356 | 178 |
! ! |
179 |
||
180 |
!LimitedPrecisionReal class methodsFor:'queries'! |
|
181 |
||
182 |
decimalPrecision |
|
183 |
"return the number of valid decimal digits" |
|
184 |
||
185 |
^ (self precision / (10.0 log:self radix)) floor |
|
186 |
||
187 |
" |
|
188 |
ShortFloat decimalPrecision |
|
189 |
Float decimalPrecision |
|
190 |
LongFloat decimalPrecision |
|
191 |
" |
|
192 |
! |
|
193 |
||
194 |
numBitsInExponent |
|
195 |
"return the number of bits in the exponent" |
|
196 |
||
197 |
^ self subclassResponsibility |
|
198 |
! |
|
199 |
||
200 |
numBitsInIntegerPart |
|
201 |
^ 0 |
|
202 |
! |
|
203 |
||
204 |
numBitsInMantissa |
|
205 |
"return the number of bits in the mantissa |
|
206 |
(typically 1 less than the precision due to the hidden bit)" |
|
207 |
||
208 |
^ self subclassResponsibility |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
209 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
210 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
211 |
precision |
7356 | 212 |
"return the number of valid mantissa bits" |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
213 |
|
7356 | 214 |
^ self numBitsInMantissa + 1 - self numBitsInIntegerPart |
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
215 |
! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
216 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
217 |
radix |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
218 |
"return the radix (base)" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
219 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
220 |
self subclassResponsibility |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
221 |
|
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
222 |
"Created: / 7.9.2001 / 14:08:20 / cg" |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
223 |
! ! |
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
224 |
|
55 | 225 |
!LimitedPrecisionReal methodsFor:'accessing'! |
226 |
||
227 |
at:index |
|
228 |
"redefined to prevent access to individual bytes in a real." |
|
229 |
||
230 |
self error:'not allowed for floats/doubles' |
|
231 |
! |
|
232 |
||
233 |
at:index put:aValue |
|
234 |
"redefined to prevent access to individual bytes in a real" |
|
235 |
||
236 |
self error:'not allowed for floats/doubles' |
|
237 |
! ! |
|
238 |
||
239 |
!LimitedPrecisionReal methodsFor:'arithmetic'! |
|
240 |
||
701 | 241 |
* aNumber |
242 |
"return the product of the receiver and the argument, aNumber" |
|
243 |
||
1200 | 244 |
"/ as soon as Float are float & Double are doubles, |
245 |
"/ use: |
|
246 |
"/ ^ aNumber productFromDouble:self asDouble |
|
247 |
||
248 |
^ aNumber productFromFloat:self asFloat |
|
249 |
||
250 |
"Modified: 17.4.1996 / 12:35:36 / cg" |
|
701 | 251 |
! |
252 |
||
55 | 253 |
+ aNumber |
254 |
"return the sum of the receiver and the argument, aNumber" |
|
255 |
||
1200 | 256 |
"/ as soon as Float are float & Double are doubles, |
257 |
"/ use: |
|
258 |
"/ ^ aNumber sumFromDouble:self asDouble |
|
259 |
||
260 |
^ aNumber sumFromFloat:self asFloat |
|
261 |
||
262 |
"Modified: 17.4.1996 / 12:35:55 / cg" |
|
55 | 263 |
! |
264 |
||
265 |
- aNumber |
|
266 |
"return the difference of the receiver and the argument, aNumber" |
|
267 |
||
1200 | 268 |
"/ as soon as Float are float & Double are doubles, |
269 |
"/ use: |
|
270 |
"/ ^ aNumber differenceFromDouble:self asDouble |
|
271 |
||
272 |
^ aNumber differenceFromFloat:self asFloat |
|
273 |
||
274 |
"Modified: 17.4.1996 / 12:36:07 / cg" |
|
55 | 275 |
! |
276 |
||
277 |
/ aNumber |
|
278 |
"return the quotient of the receiver and the argument, aNumber" |
|
279 |
||
280 |
((aNumber == 0) or:[aNumber = 0.0]) ifTrue:[ |
|
7141
033f2c26d8e6
replaced refs to DivisionByZeroSignal with ZeroDivide
Claus Gittinger <cg@exept.de>
parents:
6890
diff
changeset
|
281 |
^ ZeroDivide raiseRequestWith:thisContext. |
55 | 282 |
]. |
1200 | 283 |
"/ as soon as Float are float & Double are doubles, |
284 |
"/ use: |
|
285 |
"/ ^ aNumber quotientFromDouble:self asDouble |
|
286 |
||
287 |
^ aNumber quotientFromFloat:self asFloat |
|
288 |
||
4455
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
3898
diff
changeset
|
289 |
"Modified: / 17.4.1996 / 12:36:21 / cg" |
2d31d0d986be
Raise DivisionByZeroSignal proceedable (as in ST-80)!
Stefan Vogel <sv@exept.de>
parents:
3898
diff
changeset
|
290 |
"Modified: / 26.7.1999 / 10:46:11 / stefan" |
55 | 291 |
! |
292 |
||
293 |
// aNumber |
|
294 |
"return the integer quotient of dividing the receiver by aNumber with |
|
1882 | 295 |
truncation towards negative infinity." |
55 | 296 |
|
297 |
^ (self / aNumber) floor asInteger |
|
1882 | 298 |
|
299 |
"Modified: 5.11.1996 / 11:45:37 / cg" |
|
7356 | 300 |
! |
301 |
||
302 |
ceiling |
|
7398 | 303 |
^ self asTrueFraction ceiling |
7356 | 304 |
! |
305 |
||
306 |
floor |
|
7398 | 307 |
^ self asTrueFraction floor |
55 | 308 |
! ! |
309 |
||
5238 | 310 |
!LimitedPrecisionReal methodsFor:'coercing & converting'! |
55 | 311 |
|
2141 | 312 |
asFixedPoint |
313 |
"return the receiver as fixedPoint number. |
|
314 |
Q: what should the scale be here ?" |
|
315 |
||
316 |
^ self asFraction asFixedPoint |
|
317 |
||
318 |
" |
|
319 |
0.3 asFixedPoint |
|
320 |
0.5 asFixedPoint |
|
321 |
(1/5) asFloat asFixedPoint |
|
322 |
(1/3) asFloat asFixedPoint |
|
323 |
(2/3) asFloat asFixedPoint |
|
324 |
(1/8) asFloat asFixedPoint |
|
325 |
3.14159 asFixedPoint |
|
3060 | 326 |
0.0000001 asFraction |
327 |
0.0000001 asFixedPoint |
|
2141 | 328 |
" |
329 |
||
3060 | 330 |
"Modified: / 25.10.1997 / 15:36:54 / cg" |
2141 | 331 |
! |
332 |
||
333 |
asFixedPoint:scale |
|
334 |
"return the receiver as fixedPoint number with the given |
|
335 |
number of post-decimal-digits." |
|
336 |
||
337 |
^ self asFraction asFixedPoint:scale |
|
338 |
||
339 |
" |
|
3732 | 340 |
0.3 asFixedPoint:4 |
2141 | 341 |
0.3 asFixedPoint:3 |
3732 | 342 |
0.3 asFixedPoint:2 |
343 |
0.3 asFixedPoint:1 |
|
344 |
0.3 asFixedPoint:0 |
|
345 |
||
2141 | 346 |
0.5 asFixedPoint:3 |
347 |
(1/5) asFloat asFixedPoint:1 |
|
348 |
(1/8) asFloat asFixedPoint:1 |
|
6641 | 349 |
1.0 asFixedPoint:2 |
2141 | 350 |
3.14159 asFixedPoint:2 |
351 |
3.14159 asFixedPoint:3 |
|
352 |
(3.14159 asFixedPoint:2) asFixedPoint:5 |
|
353 |
" |
|
354 |
||
3732 | 355 |
"Modified: / 5.8.1998 / 13:29:51 / cg" |
2141 | 356 |
! |
357 |
||
55 | 358 |
asFraction |
5883 | 359 |
"Answer a rational number (Integer or Fraction) representing the receiver. |
360 |
This conversion uses the continued fraction method to approximate |
|
361 |
a floating point number." |
|
362 |
||
363 |
|num1 denom1 num2 denom2 int frac newD temp limit| |
|
55 | 364 |
|
7394 | 365 |
limit := 10 raisedToInteger:self defaultNumberOfDigits. |
5883 | 366 |
num1 := self truncated. |
367 |
denom1 := 1. "The first of two alternating denominators" |
|
368 |
num2 := 1. "The second numerator" |
|
369 |
denom2 := 0. "The second denominator--will update" |
|
370 |
int := num1. "The integer part of self" |
|
371 |
frac := self fractionPart. |
|
372 |
[frac = 0] whileFalse:[ |
|
373 |
newD := 1.0 / frac. |
|
374 |
int := newD truncated. |
|
375 |
frac := newD fractionPart. "save the fractional part for next time" |
|
376 |
temp := num2. "old numerator and save it" |
|
377 |
num2 := num1. |
|
378 |
num1 := num1 * int + temp. "Update first numerator" |
|
379 |
temp := denom2. "old denominator and save it" |
|
380 |
denom2 := denom1. |
|
381 |
denom1 := int * denom1 + temp. "Update first denominator" |
|
382 |
limit < denom1 ifTrue:[ |
|
383 |
"Is ratio past float precision? If so, pick which of the two ratios to use" |
|
384 |
num2 = 0.0 ifTrue:[ |
|
385 |
"Is second denominator 0?" |
|
386 |
^ Fraction numerator:num1 denominator:denom1 |
|
387 |
]. |
|
388 |
^ Fraction numerator:num2 denominator:denom2 |
|
389 |
] |
|
3060 | 390 |
]. |
391 |
||
5883 | 392 |
"If fractional part is zero, return the first ratio" |
393 |
denom1 = 1 ifTrue:[ |
|
394 |
"Am i really an Integer?" |
|
395 |
^ num1 "Yes, return Integer result" |
|
3060 | 396 |
]. |
5883 | 397 |
"Otherwise return Fraction result" |
398 |
^ Fraction numerator:num1 denominator:denom1 |
|
55 | 399 |
|
3060 | 400 |
" |
5883 | 401 |
1.1 asFraction |
402 |
1.2 asFraction |
|
403 |
0.3 asFraction |
|
7394 | 404 |
0.5 asFraction |
405 |
(1/5) asFloat asFraction |
|
5883 | 406 |
(1/8) asFloat asFraction |
407 |
(1/13) asFloat asFraction |
|
408 |
3.14159 asFraction |
|
409 |
3.14159 asFraction asFloat |
|
7394 | 410 |
1.3 asFraction |
411 |
1.0 asFraction |
|
412 |
1E6 asFraction |
|
413 |
1E-6 asFraction |
|
3060 | 414 |
" |
2793 | 415 |
|
3060 | 416 |
"Modified: / 25.10.1997 / 16:41:19 / cg" |
55 | 417 |
! |
418 |
||
419 |
asInteger |
|
420 |
"return an integer with same value - might truncate" |
|
421 |
||
3185 | 422 |
|max maxF| |
55 | 423 |
|
2388 | 424 |
self isNaN ifTrue:[ |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
425 |
^ self class |
7403 | 426 |
raise:#conversionErrorSignal |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
427 |
receiver:self |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
428 |
selector:#asInteger |
6195 | 429 |
arguments:#() |
3395
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
430 |
errorString:'receiver is NaN in #asInteger' |
02f47135d80f
pass messageSend as parameter to domainError exception
Claus Gittinger <cg@exept.de>
parents:
3225
diff
changeset
|
431 |
|
2388 | 432 |
]. |
433 |
||
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
434 |
self abs < 2e16 ifTrue:[ |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
435 |
"/ NOTICE: this must be redefined in float |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
436 |
"/ subclasses to handle the smallinteger range; |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
437 |
"/ i.e. this may only be invoked for reals |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
438 |
"/ which are NOT within the smallInt range. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
439 |
"/ otherwise, endless recursion is the consequence. |
3426 | 440 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
441 |
max := SmallInteger maxVal // 2 + 1. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
442 |
maxF := max asFloat. |
3426 | 443 |
|
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
444 |
^ (self quo:maxF) * max + (self rem:maxF) truncated |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
445 |
]. |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
446 |
^ self asTrueFraction |
55 | 447 |
|
3137 | 448 |
" |
4615
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
449 |
12345.0 asInteger |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
450 |
1e15 asInteger |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
451 |
1e33 asInteger asFloat |
e480d1e6090f
largFloat -> largeInt conversion fixed.
Claus Gittinger <cg@exept.de>
parents:
4455
diff
changeset
|
452 |
1e303 asInteger asFloat |
3137 | 453 |
" |
2388 | 454 |
|
6195 | 455 |
"Modified: / 16.11.2001 / 14:15:33 / cg" |
701 | 456 |
! |
457 |
||
7378 | 458 |
asLimitedPrecisionReal |
459 |
"return a float of any precision with same value" |
|
460 |
||
461 |
^ self |
|
462 |
! |
|
463 |
||
5883 | 464 |
asRational |
465 |
"Answer a Rational number--Integer or Fraction--representing the receiver. |
|
466 |
Same as asFraction fro st-80 compatibility." |
|
467 |
||
468 |
^ self asFraction |
|
469 |
||
470 |
" |
|
471 |
1.1 asRational |
|
472 |
1.2 asRational |
|
473 |
0.3 asRational |
|
474 |
0.5 asRational |
|
475 |
(1/5) asFloat asRational |
|
476 |
(1/8) asFloat asRational |
|
477 |
(1/13) asFloat asRational |
|
478 |
3.14159 asRational |
|
479 |
3.14159 asRational asFloat |
|
480 |
1.3 asRational |
|
481 |
1.0 asRational |
|
482 |
" |
|
483 |
! |
|
484 |
||
7356 | 485 |
asTrueFraction |
486 |
"Answer a fraction or integer that EXACTLY represents self, |
|
487 |
an any-precision IEEE floating point number, consisting of: |
|
488 |
numMantissaBits bits of normalized mantissa (i.e. with hidden leading 1-bit) |
|
489 |
optional numExtraBits between mantissa and exponent (normalized flag for ext-real) |
|
490 |
numExponentBits bits of 2s complement exponent |
|
491 |
1 sign bit. |
|
492 |
Taken from Floats asTrueFraction" |
|
493 |
||
494 |
|shifty sign expPart exp fraction fractionPart result zeroBitsCount |
|
495 |
numBytes numBits numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
496 |
biasExponent numIntegerBits| |
|
497 |
||
498 |
self isFinite ifFalse:[ |
|
499 |
^ self class |
|
500 |
raise:#conversionErrorSignal |
|
501 |
receiver:self |
|
502 |
selector:#asTrueFraction |
|
503 |
arguments:#() |
|
504 |
errorString:'Cannot represent non-finite float as a fraction'. |
|
505 |
]. |
|
506 |
||
507 |
"Extract the bits of an IEEE anySize float " |
|
508 |
numBytes := self basicSize. |
|
509 |
numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
|
510 |
numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
511 |
numIntegerBits := self class numBitsInIntegerPart. |
|
512 |
numBits := numBitsInMantissa + numBitsInExponent. "not (numBytes * 8 - 1) -- there could be 80 float bits in 96 overall bits" |
|
513 |
biasExponent := maskExponent bitShift:-1. |
|
514 |
||
515 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
516 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
517 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
518 |
] ifFalse:[ |
|
519 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
520 |
]. |
|
521 |
||
522 |
" Extract the sign and the biased exponent " |
|
523 |
sign := (shifty bitAt:numBits+1) == 0 ifTrue: [1] ifFalse: [-1]. |
|
524 |
expPart := (shifty bitShift:numBitsInMantissa negated) bitAnd: maskExponent. |
|
525 |
||
526 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
527 |
fractionPart := shifty bitAnd:maskMantissa. |
|
528 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
|
701 | 529 |
|
7356 | 530 |
numIntegerBits == 0 ifTrue:[ |
531 |
" Replace omitted leading 1 in fraction (Notice: quadIEEE format does not do this)" |
|
532 |
fraction := fractionPart bitOr: (maskMantissa + 1). |
|
533 |
] ifFalse:[ |
|
534 |
fraction := fractionPart. |
|
535 |
]. |
|
536 |
||
7390 | 537 |
"Unbias exponent" |
7356 | 538 |
exp := biasExponent - expPart + (numBitsInMantissa - numIntegerBits). |
1200 | 539 |
|
7390 | 540 |
" Form the result. When exp>fractionalPrecision, the exponent is adjusted by |
7356 | 541 |
the number of trailing zero bits in the fraction to minimize |
7390 | 542 |
the (huge) time otherwise spent in #gcd: of fraction handling code." |
7356 | 543 |
exp negative ifTrue: [ |
544 |
result := sign * fraction bitShift: exp negated |
|
545 |
] ifFalse:[ |
|
546 |
zeroBitsCount := fraction lowBit - 1. |
|
547 |
exp := exp - zeroBitsCount. |
|
548 |
exp <= 0 ifTrue: [ |
|
549 |
zeroBitsCount := zeroBitsCount + exp. |
|
550 |
result := sign * fraction bitShift:zeroBitsCount negated |
|
551 |
] ifFalse: [ |
|
552 |
result := Fraction |
|
553 |
numerator: (sign * fraction bitShift: zeroBitsCount negated) |
|
554 |
denominator: (1 bitShift:exp) |
|
555 |
] |
|
556 |
]. |
|
1200 | 557 |
|
7356 | 558 |
"Low cost validation omitted after extensive testing" |
559 |
"(result asFloat = self) ifFalse: [self error: 'asTrueFraction validation failed']." |
|
560 |
||
561 |
^ result |
|
562 |
||
563 |
" |
|
7390 | 564 |
0.3 asFloat asTrueFraction |
7356 | 565 |
0.3 asShortFloat asTrueFraction |
7390 | 566 |
0.3 asLongFloat asTrueFraction |
567 |
||
568 |
1.25 asTrueFraction |
|
569 |
0.25 asTrueFraction |
|
570 |
-0.25 asTrueFraction |
|
571 |
3e37 asTrueFraction |
|
572 |
||
573 |
Float NaN asTrueFraction -> error |
|
574 |
Float infinity asTrueFraction -> error |
|
575 |
Float negativeInfinity asTrueFraction -> error |
|
576 |
" |
|
577 |
! |
|
578 |
||
579 |
exponent |
|
580 |
"extract a normalized floats exponent. |
|
581 |
The returned value depends on the float-representation of |
|
582 |
the underlying machine and is therefore highly unportable. |
|
583 |
This is not for general use. |
|
584 |
This assumes that the mantissa is normalized to |
|
585 |
0.5 .. 1.0 and the floats value is mantissa * 2^exp" |
|
586 |
||
587 |
|shifty expPart exp numBytes numBitsInMantissa maskMantissa numBitsInExponent maskExponent |
|
588 |
biasExponent numIntegerBits fractionPart| |
|
7356 | 589 |
|
7390 | 590 |
"Extract the bits of an IEEE anySize float " |
591 |
numBytes := self basicSize. |
|
592 |
numBitsInMantissa := self class numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1. |
|
593 |
numBitsInExponent := self class numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1. |
|
594 |
numIntegerBits := self class numBitsInIntegerPart. |
|
595 |
biasExponent := maskExponent bitShift:-1. |
|
596 |
||
597 |
shifty := LargeInteger basicNew numberOfDigits:numBytes. |
|
598 |
UninterpretedBytes isBigEndian ifTrue:[ |
|
599 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(self basicAt:i)]. |
|
600 |
] ifFalse:[ |
|
601 |
1 to:numBytes do:[:i | shifty digitAt:i put:(self basicAt:i)]. |
|
602 |
]. |
|
603 |
||
604 |
" Extract the sign and the biased exponent " |
|
605 |
expPart := (shifty bitShift:numBitsInMantissa negated) bitAnd: maskExponent. |
|
606 |
||
607 |
" Extract fractional part; answer 0 if this is a true 0.0 value " |
|
608 |
fractionPart := shifty bitAnd:maskMantissa. |
|
609 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ 0 ]. |
|
610 |
||
611 |
exp := expPart - biasExponent + 1. |
|
612 |
^ exp |
|
613 |
||
614 |
" |
|
615 |
0.3 asFloat exponent |
|
616 |
0.3 asShortFloat exponent |
|
617 |
0.3 asLongFloat exponent |
|
618 |
||
619 |
0.0 exponent2 0 |
|
620 |
1.0 exponent2 1 |
|
621 |
2.0 exponent2 2 |
|
622 |
3.0 exponent2 2 |
|
623 |
4.0 exponent2 3 |
|
624 |
0.5 exponent2 0 |
|
625 |
0.4 exponent2 -1 |
|
626 |
0.25 exponent2 -1 |
|
627 |
0.00000011111 exponent2 -23 |
|
7356 | 628 |
" |
701 | 629 |
! |
630 |
||
3898 | 631 |
fractionalPart |
632 |
"This has been renamed to #fractionPart for ST80 compatibility. |
|
633 |
||
634 |
extract the after-decimal fraction part. |
|
635 |
the floats value is |
|
636 |
float truncated + float fractionalPart" |
|
637 |
||
5868 | 638 |
<resource:#obsolete> |
639 |
||
3898 | 640 |
self obsoleteMethodWarning:'please use #fractionPart'. |
641 |
^ self fractionPart |
|
642 |
||
643 |
"Modified: / 28.10.1998 / 17:10:12 / cg" |
|
644 |
"Created: / 28.10.1998 / 17:10:32 / cg" |
|
645 |
! |
|
646 |
||
701 | 647 |
generality |
648 |
"return the generality value - see ArithmeticValue>>retry:coercing:" |
|
649 |
||
650 |
^ 80 |
|
7356 | 651 |
! |
652 |
||
653 |
i |
|
654 |
"return a complex number, with the receiver as imaginary part, 0 as real part" |
|
655 |
||
656 |
^ Complex |
|
657 |
real:0 |
|
658 |
imaginary:self |
|
659 |
||
660 |
" |
|
661 |
3.0i |
|
662 |
(1+1i) |
|
663 |
" |
|
701 | 664 |
! ! |
665 |
||
666 |
!LimitedPrecisionReal methodsFor:'comparing'! |
|
667 |
||
668 |
< aNumber |
|
669 |
"return true, if the argument is greater" |
|
670 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
671 |
"/ 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
|
672 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
673 |
"/ ^ aNumber lessFromDouble:self asDouble |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
674 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
675 |
^ aNumber lessFromFloat:self asFloat |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
676 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
677 |
"Modified: 17.4.1996 / 13:34:50 / cg" |
701 | 678 |
! |
679 |
||
680 |
<= aNumber |
|
681 |
"return true, if the argument is greater or equal" |
|
682 |
||
683 |
^ self retry:#<= coercing:aNumber |
|
684 |
! |
|
685 |
||
686 |
> aNumber |
|
687 |
"return true, if the argument is less" |
|
688 |
||
689 |
^ self retry:#> coercing:aNumber |
|
690 |
! |
|
691 |
||
692 |
>= aNumber |
|
693 |
"return true, if the argument is less or equal" |
|
694 |
||
695 |
^ self retry:#>= coercing:aNumber |
|
696 |
! ! |
|
697 |
||
698 |
!LimitedPrecisionReal methodsFor:'copying'! |
|
699 |
||
700 |
deepCopy |
|
701 |
"return a deep copy of myself |
|
702 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
703 |
||
704 |
^ self |
|
705 |
! |
|
706 |
||
707 |
deepCopyUsing:aDictionary |
|
708 |
"return a deep copy of myself |
|
709 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
710 |
||
711 |
^ self |
|
712 |
! |
|
713 |
||
714 |
shallowCopy |
|
715 |
"return a shallow copy of the receiver" |
|
716 |
||
717 |
^ self |
|
718 |
! |
|
719 |
||
720 |
simpleDeepCopy |
|
721 |
"return a deep copy of the receiver |
|
722 |
- because storing into floats is not recommended/allowed, its ok to return the receiver" |
|
723 |
||
724 |
^ self |
|
55 | 725 |
! ! |
726 |
||
727 |
!LimitedPrecisionReal methodsFor:'double dispatching'! |
|
728 |
||
729 |
differenceFromFraction:aFraction |
|
213 | 730 |
"sent when a fraction does not know how to subtract the receiver, a float" |
55 | 731 |
|
732 |
|d| |
|
733 |
||
734 |
d := aFraction denominator. |
|
735 |
^ (aFraction numerator - (self * d)) / d |
|
736 |
! |
|
737 |
||
738 |
productFromFraction:aFraction |
|
213 | 739 |
"sent when a fraction does not know how to multiply the receiver, a float" |
55 | 740 |
|
741 |
^ self * aFraction numerator / aFraction denominator |
|
742 |
! |
|
743 |
||
744 |
quotientFromFraction:aFraction |
|
213 | 745 |
"sent when a fraction does not know how to divide by the receiver, a float" |
55 | 746 |
|
747 |
^ aFraction numerator / (self * aFraction denominator) |
|
701 | 748 |
! |
749 |
||
750 |
sumFromFraction:aFraction |
|
751 |
"sent when a fraction does not know how to add the receiver, a float" |
|
752 |
||
753 |
|d| |
|
754 |
||
755 |
d := aFraction denominator. |
|
756 |
^ (self * d + aFraction numerator) / d |
|
757 |
! ! |
|
758 |
||
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
759 |
!LimitedPrecisionReal methodsFor:'encoding'! |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
760 |
|
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
761 |
encodeOn:anEncoder with:aParameter |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
762 |
|
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
763 |
anEncoder encodeFloat:self with:aParameter |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
764 |
|
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
765 |
|
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
766 |
! ! |
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
767 |
|
701 | 768 |
!LimitedPrecisionReal methodsFor:'printing & storing'! |
769 |
||
770 |
printOn:aStream |
|
771 |
"append a printed representation of the receiver to |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
772 |
the argument, aStream. |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
773 |
|
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
774 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
775 |
#printOn: as basic print mechanism." |
701 | 776 |
|
777 |
aStream nextPutAll:self printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
778 |
|
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
779 |
"Modified: / 20.1.1998 / 14:10:46 / stefan" |
1200 | 780 |
! |
781 |
||
782 |
printString |
|
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
783 |
"return a printed representation of the receiver |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
784 |
LimitedPrecisonReal and its subclasses use #printString instead of |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
785 |
#printOn: as basic print mechanism." |
1200 | 786 |
|
787 |
^ self subclassResponsibility |
|
788 |
||
3194
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
789 |
"Created: / 17.4.1996 / 12:12:20 / cg" |
16073834a825
Add comment about #printOn: and #printString
Stefan Vogel <sv@exept.de>
parents:
3185
diff
changeset
|
790 |
"Modified: / 20.1.1998 / 14:10:47 / stefan" |
701 | 791 |
! ! |
792 |
||
5557 | 793 |
!LimitedPrecisionReal methodsFor:'queries'! |
794 |
||
5883 | 795 |
defaultNumberOfDigits |
7356 | 796 |
"Answer how many digits of accuracy this class supports" |
797 |
||
798 |
^ self class decimalPrecision - 1 |
|
5883 | 799 |
! |
800 |
||
5557 | 801 |
size |
802 |
"redefined since reals are kludgy (ByteArry)" |
|
803 |
||
804 |
^ 0 |
|
805 |
! ! |
|
5470
5e56d8cff0c9
General encoding method (#encodeOn:with:)
Stefan Vogel <sv@exept.de>
parents:
5357
diff
changeset
|
806 |
|
701 | 807 |
!LimitedPrecisionReal methodsFor:'testing'! |
808 |
||
3148
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
809 |
isFinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
810 |
^ self subclassResponsibility |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
811 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
812 |
"Created: / 7.1.1998 / 12:02:06 / stefan" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
813 |
! |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
814 |
|
6185 | 815 |
isFloat |
816 |
"return true, if the receiver is some kind of floating point number; |
|
817 |
false is returned here. |
|
818 |
Same as #isLimitedPrecisionReal, but a better name ;-)" |
|
819 |
||
820 |
^ true |
|
821 |
||
822 |
"Created: / 14.11.2001 / 14:57:55 / cg" |
|
823 |
! |
|
824 |
||
3148
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
825 |
isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
826 |
"return true, if the receiver is an infinite float (Inf). |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
827 |
These are not created by ST/X float operations (they raise an exception); |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
828 |
however, inline C-code could produce them ..." |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
829 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
830 |
^ (self isFinite or:[self isNaN]) not. |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
831 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
832 |
" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
833 |
1.0 isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
834 |
(0.0 uncheckedDivide: 0.0) isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
835 |
(1.0 uncheckedDivide: 0.0) isInfinite |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
836 |
" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
837 |
|
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
838 |
"Modified: / 7.1.1998 / 12:01:30 / stefan" |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
839 |
! |
1434542209e7
New #finite, define #isInfinite using #finite.
Stefan Vogel <sv@exept.de>
parents:
3137
diff
changeset
|
840 |
|
6086
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
841 |
isLimitedPrecisionReal |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
842 |
"return true, if the receiver is some kind of limited precision real (i.e. floating point) number; |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
843 |
true is returned here - the method is redefined from Object." |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
844 |
|
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
845 |
^ true |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
846 |
! |
0bd476397581
added isLimitedPrecisionReal
Claus Gittinger <cg@exept.de>
parents:
5989
diff
changeset
|
847 |
|
2388 | 848 |
isNaN |
849 |
^ self subclassResponsibility |
|
850 |
||
851 |
"Modified: 12.2.1997 / 16:45:27 / cg" |
|
852 |
! |
|
853 |
||
7356 | 854 |
isNegativeInfinity |
855 |
^ self negative and:[self isInfinite] |
|
856 |
! |
|
857 |
||
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
858 |
isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
859 |
"many systems have two float.Pnt zeros" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
860 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
861 |
^ (self printString first == $-) |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
862 |
|
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
863 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
864 |
0.0 isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
865 |
-0.0 isNegativeZero |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
866 |
" |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
867 |
! |
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
868 |
|
7356 | 869 |
isPositiveInfinity |
870 |
^ self positive and:[self isInfinite] |
|
871 |
! |
|
872 |
||
701 | 873 |
negative |
874 |
"return true if the receiver is less than zero" |
|
875 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
876 |
"/ 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
|
877 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
878 |
"/ ^ self asDouble negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
879 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
880 |
^ self asFloat negative |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
881 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
882 |
"Modified: 17.4.1996 / 13:35:00 / cg" |
701 | 883 |
! |
884 |
||
5357 | 885 |
numberOfBits |
886 |
"return the size (in bits) of the real; |
|
887 |
typically, this is 64 for Floats and 32 for ShortFloats, |
|
888 |
but who knows ..." |
|
889 |
||
890 |
self subclassResponsibility |
|
891 |
! |
|
892 |
||
701 | 893 |
positive |
894 |
"return true if the receiver is greater or equal to zero" |
|
895 |
||
1201
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
896 |
"/ 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
|
897 |
"/ use: |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
898 |
"/ ^ self asDouble positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
899 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
900 |
^ self asFloat positive |
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
901 |
|
24a5faa7e305
oops - asDouble is not yet there
Claus Gittinger <cg@exept.de>
parents:
1200
diff
changeset
|
902 |
"Modified: 17.4.1996 / 13:35:10 / cg" |
55 | 903 |
! ! |
904 |
||
7356 | 905 |
!LimitedPrecisionReal methodsFor:'truncation & rounding'! |
906 |
||
907 |
ceilingAsFloat |
|
908 |
^ self coerce:(self ceiling). |
|
909 |
||
910 |
" |
|
911 |
0.4 asLongFloat ceilingAsFloat |
|
912 |
" |
|
913 |
! |
|
914 |
||
915 |
floorAsFloat |
|
916 |
^ self coerce:(self floor). |
|
917 |
||
918 |
" |
|
919 |
0.4 asLongFloat floorAsFloat |
|
920 |
" |
|
921 |
! |
|
922 |
||
923 |
roundedAsFloat |
|
924 |
self negative ifTrue:[ ^ (self - 0.5) ceilingAsFloat ]. |
|
925 |
^ (self + 0.5) floorAsFloat |
|
7398 | 926 |
! |
927 |
||
928 |
truncatedAsFloat |
|
929 |
^ self coerce:(self truncated). |
|
930 |
||
931 |
" |
|
932 |
0.4 asLongFloat truncatedAsFloat |
|
933 |
" |
|
7356 | 934 |
! ! |
935 |
||
701 | 936 |
!LimitedPrecisionReal class methodsFor:'documentation'! |
55 | 937 |
|
701 | 938 |
version |
7403 | 939 |
^ '$Header: /cvs/stx/stx/libbasic/LimitedPrecisionReal.st,v 1.53 2003-06-17 14:10:40 cg Exp $' |
55 | 940 |
! ! |
6890
8e74eff54c3d
added isNegativeZero to test for -0.0
Claus Gittinger <cg@exept.de>
parents:
6641
diff
changeset
|
941 |
|
5989
8e3ff880f561
ANSI preparations (incomplete)
Claus Gittinger <cg@exept.de>
parents:
5954
diff
changeset
|
942 |
LimitedPrecisionReal initialize! |