7445
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2003 by eXept Software AG
|
|
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 |
"{ Package: 'stx:libbasic' }"
|
|
14 |
|
|
15 |
LimitedPrecisionReal subclass:#LargeFloat
|
|
16 |
instanceVariableNames:'exponent mantissa precision'
|
|
17 |
classVariableNames:'Zero One NaN PositiveInfinity NegativeInfinity Pi_1000 E_1000'
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'Magnitude-Numbers'
|
|
20 |
!
|
|
21 |
|
|
22 |
!LargeFloat class methodsFor:'documentation'!
|
|
23 |
|
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 2003 by eXept Software AG
|
|
27 |
All Rights Reserved
|
|
28 |
|
|
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 |
!
|
|
37 |
|
|
38 |
documentation
|
|
39 |
"
|
7547
|
40 |
Experimental Code.
|
8634
|
41 |
The implementation is neither complete nor tuned for performance - still being developed.
|
7547
|
42 |
|
7445
|
43 |
This class provides arbitrary precision floats. These are represented as:
|
|
44 |
exponent,
|
|
45 |
mantissa
|
|
46 |
|
|
47 |
[author:]
|
|
48 |
Claus Gittinger
|
|
49 |
|
|
50 |
[see also:]
|
|
51 |
Number
|
|
52 |
Float LongFloat ShortFloat Fraction FixedPoint
|
|
53 |
SmallInteger LargeInteger
|
|
54 |
"
|
|
55 |
!
|
|
56 |
|
|
57 |
examples
|
|
58 |
"
|
|
59 |
|
|
60 |
(1 to:1000) inject:1 asLargeFloat into:[:p :m | p * m]
|
7550
|
61 |
(1 to:1000) inject:1 into:[:p :m | p * m]
|
7445
|
62 |
|
|
63 |
Time millisecondsToRun:[
|
|
64 |
(1 to:20000) inject:1 asLargeFloat into:[:p :m | p * m]
|
|
65 |
]
|
|
66 |
|
|
67 |
Time millisecondsToRun:[
|
|
68 |
(1 to:20000) inject:1 into:[:p :m | p * m]
|
|
69 |
]
|
|
70 |
"
|
|
71 |
! !
|
|
72 |
|
|
73 |
!LargeFloat class methodsFor:'instance creation'!
|
|
74 |
|
|
75 |
fromInteger:anInteger
|
|
76 |
^ self basicNew
|
|
77 |
mantissa:anInteger
|
|
78 |
exponent:0
|
|
79 |
|
|
80 |
"
|
|
81 |
LargeFloat fromInteger:123456
|
|
82 |
|
|
83 |
1 asLargeFloat
|
|
84 |
2 asLargeFloat
|
|
85 |
1000 factorial asLargeFloat
|
|
86 |
"
|
|
87 |
!
|
|
88 |
|
|
89 |
fromLimitedPrecisionReal:aLimitedPrecisionReal
|
|
90 |
|shifty numBytes numBitsInMantissa maskMantissa numBitsInExponent maskExponent
|
|
91 |
numIntegerBits numBits biasExponent lpRealClass sign expPart fractionPart fraction exp|
|
|
92 |
|
|
93 |
aLimitedPrecisionReal isFinite ifFalse:[
|
|
94 |
aLimitedPrecisionReal isNaN ifTrue:[^ self NaN].
|
|
95 |
aLimitedPrecisionReal > 0 ifTrue:[^ self infinity].
|
|
96 |
^ self negativeInfinity
|
|
97 |
].
|
|
98 |
|
|
99 |
lpRealClass := aLimitedPrecisionReal class.
|
|
100 |
numBytes := aLimitedPrecisionReal basicSize.
|
|
101 |
numBitsInMantissa := lpRealClass numBitsInMantissa. maskMantissa := (1 bitShift:numBitsInMantissa) - 1.
|
|
102 |
numBitsInExponent := lpRealClass numBitsInExponent. maskExponent := (1 bitShift:numBitsInExponent) - 1.
|
|
103 |
numIntegerBits := lpRealClass numBitsInIntegerPart.
|
|
104 |
numBits := numBitsInMantissa + numBitsInExponent.
|
|
105 |
biasExponent := maskExponent bitShift:-1.
|
|
106 |
|
|
107 |
shifty := LargeInteger basicNew numberOfDigits:numBytes.
|
|
108 |
UninterpretedBytes isBigEndian ifTrue:[
|
|
109 |
1 to:numBytes do:[:i | shifty digitAt:(numBytes+1-i) put:(aLimitedPrecisionReal basicAt:i)].
|
|
110 |
] ifFalse:[
|
|
111 |
1 to:numBytes do:[:i | shifty digitAt:i put:(aLimitedPrecisionReal basicAt:i)].
|
|
112 |
].
|
|
113 |
sign := (shifty bitAt:numBits+1) == 0 ifTrue: [1] ifFalse: [-1].
|
|
114 |
expPart := (shifty bitShift:numBitsInMantissa negated) bitAnd: maskExponent.
|
|
115 |
fractionPart := shifty bitAnd:maskMantissa.
|
|
116 |
( expPart=0 and: [ fractionPart=0 ] ) ifTrue: [ ^ self zero ].
|
|
117 |
|
|
118 |
numIntegerBits == 0 ifTrue:[
|
|
119 |
" Replace omitted leading 1 in fraction (Notice: quadIEEE format does not do this)"
|
|
120 |
fraction := fractionPart bitOr: (maskMantissa + 1).
|
|
121 |
] ifFalse:[
|
|
122 |
fraction := fractionPart.
|
|
123 |
].
|
|
124 |
|
|
125 |
"Unbias exponent"
|
|
126 |
exp := biasExponent - expPart + (numBitsInMantissa - numIntegerBits).
|
|
127 |
|
|
128 |
^ self basicNew
|
|
129 |
mantissa:(fraction * sign)
|
|
130 |
exponent:(exp negated)
|
|
131 |
|
|
132 |
"
|
|
133 |
1.0 asLargeFloat
|
|
134 |
2.0 asLargeFloat
|
|
135 |
20000.0 asLargeFloat
|
|
136 |
2e6 asLargeFloat
|
|
137 |
1e300 asLargeFloat
|
|
138 |
2e300 asLargeFloat
|
|
139 |
|
|
140 |
0.5 asLargeFloat
|
|
141 |
0.25 asLargeFloat
|
|
142 |
(1.0/20000.0) asLargeFloat
|
|
143 |
2e-6 asLargeFloat
|
|
144 |
2e-300 asLargeFloat
|
|
145 |
|
|
146 |
-1.0 asLargeFloat
|
|
147 |
-0.5 asLargeFloat
|
|
148 |
|
|
149 |
Float NaN asLargeFloat
|
|
150 |
Float infinity asLargeFloat
|
|
151 |
Float negativeInfinity asLargeFloat
|
|
152 |
"
|
|
153 |
!
|
|
154 |
|
|
155 |
mantissa:m exponent:e
|
|
156 |
^ self basicNew mantissa:m exponent:e
|
|
157 |
|
|
158 |
"
|
|
159 |
LargeFloat mantissa:1 exponent:0
|
|
160 |
LargeFloat mantissa:2 exponent:0
|
|
161 |
LargeFloat mantissa:4 exponent:0
|
|
162 |
LargeFloat mantissa:8 exponent:0
|
|
163 |
LargeFloat mantissa:1 exponent:-1
|
|
164 |
LargeFloat mantissa:1 exponent:-2
|
|
165 |
LargeFloat mantissa:1 exponent:-3
|
|
166 |
"
|
|
167 |
!
|
|
168 |
|
|
169 |
mantissa:m exponent:e precision:p
|
|
170 |
^ self basicNew mantissa:m exponent:e precision:p
|
|
171 |
! !
|
|
172 |
|
|
173 |
!LargeFloat class methodsFor:'class initialization'!
|
|
174 |
|
|
175 |
initialize
|
|
176 |
NaN := self mantissa:0 exponent:999.
|
|
177 |
PositiveInfinity := self mantissa:0 exponent:1.
|
|
178 |
NegativeInfinity := self mantissa:0 exponent:-1.
|
|
179 |
One := self mantissa:1 exponent:0.
|
|
180 |
Zero := self mantissa:0 exponent:0.
|
|
181 |
|
|
182 |
"
|
|
183 |
LargeFloat initialize
|
|
184 |
"
|
|
185 |
! !
|
|
186 |
|
|
187 |
!LargeFloat class methodsFor:'constants'!
|
|
188 |
|
|
189 |
NaN
|
|
190 |
^ NaN
|
|
191 |
|
|
192 |
"
|
|
193 |
LargeFloat NaN
|
|
194 |
(0.0 uncheckedDivide:0.0)
|
|
195 |
"
|
|
196 |
!
|
|
197 |
|
|
198 |
infinity
|
|
199 |
^ PositiveInfinity
|
|
200 |
|
|
201 |
"
|
|
202 |
LargeFloat infinity
|
|
203 |
(1.0 uncheckedDivide:0.0)
|
|
204 |
"
|
|
205 |
!
|
|
206 |
|
|
207 |
negativeInfinity
|
|
208 |
^ NegativeInfinity
|
|
209 |
|
|
210 |
"
|
|
211 |
LargeFloat negativeInfinity
|
|
212 |
(-1.0 uncheckedDivide:0.0)
|
|
213 |
"
|
|
214 |
!
|
|
215 |
|
|
216 |
pi
|
|
217 |
Pi_1000 isNil ifTrue:[
|
|
218 |
Pi_1000 := FixedPoint pi asLargeFloat
|
|
219 |
].
|
|
220 |
^ Pi_1000.
|
|
221 |
|
|
222 |
"
|
|
223 |
LargeFloat pi
|
|
224 |
"
|
|
225 |
!
|
|
226 |
|
|
227 |
unity
|
|
228 |
^ One
|
|
229 |
|
|
230 |
"
|
|
231 |
LargeFloat unity
|
|
232 |
"
|
|
233 |
!
|
|
234 |
|
|
235 |
zero
|
|
236 |
^ Zero
|
|
237 |
|
|
238 |
"
|
|
239 |
LargeFloat zero
|
|
240 |
"
|
|
241 |
! !
|
|
242 |
|
8634
|
243 |
!LargeFloat class methodsFor:'queries'!
|
|
244 |
|
|
245 |
radix
|
|
246 |
"answer the radix of a LargeFloats exponent"
|
|
247 |
|
|
248 |
^ 2
|
|
249 |
! !
|
|
250 |
|
7445
|
251 |
!LargeFloat methodsFor:'accessing'!
|
|
252 |
|
|
253 |
exponent
|
|
254 |
^ exponent
|
|
255 |
!
|
|
256 |
|
|
257 |
mantissa
|
|
258 |
^ mantissa
|
|
259 |
!
|
|
260 |
|
|
261 |
precision
|
|
262 |
^ precision ? 200
|
|
263 |
! !
|
|
264 |
|
|
265 |
!LargeFloat methodsFor:'arithmetic'!
|
|
266 |
|
|
267 |
* aNumber
|
|
268 |
^ aNumber productFromLargeFloat:self
|
|
269 |
!
|
|
270 |
|
|
271 |
+ aNumber
|
|
272 |
^ aNumber sumFromLargeFloat:self
|
|
273 |
|
|
274 |
"
|
|
275 |
1.0 asLargeFloat
|
|
276 |
"
|
|
277 |
!
|
|
278 |
|
|
279 |
- aNumber
|
|
280 |
^ aNumber differenceFromLargeFloat:self
|
|
281 |
!
|
|
282 |
|
|
283 |
/ aNumber
|
|
284 |
^ aNumber quotientFromLargeFloat:self
|
|
285 |
!
|
|
286 |
|
|
287 |
negated
|
|
288 |
mantissa = 0 ifTrue:[
|
|
289 |
exponent = 0 ifTrue:[ ^ self ].
|
|
290 |
self == NaN ifTrue:[^ self].
|
|
291 |
self == NegativeInfinity ifTrue:[^ PositiveInfinity].
|
|
292 |
^ NegativeInfinity
|
|
293 |
].
|
|
294 |
|
|
295 |
^ self class
|
|
296 |
mantissa:(mantissa negated)
|
|
297 |
exponent:exponent
|
7459
|
298 |
precision:self precision.
|
7445
|
299 |
|
|
300 |
"
|
|
301 |
LargeFloat unity negated
|
|
302 |
"
|
|
303 |
! !
|
|
304 |
|
|
305 |
!LargeFloat methodsFor:'coercing & converting'!
|
|
306 |
|
|
307 |
asInteger
|
|
308 |
"return an integer with same value - might truncate"
|
|
309 |
|
|
310 |
exponent = 0 ifTrue:[^ mantissa].
|
|
311 |
|
|
312 |
mantissa == 0 ifTrue:[
|
|
313 |
"/ INF or NAN
|
|
314 |
^ self class
|
|
315 |
raise:#domainErrorSignal
|
|
316 |
receiver:self
|
|
317 |
selector:#asInteger
|
|
318 |
arguments:#()
|
|
319 |
errorString:'Cannot represent non-finite as integer'.
|
|
320 |
"/ ^ self asMetaNumber.
|
|
321 |
].
|
|
322 |
|
|
323 |
exponent > 0 ifTrue:[
|
|
324 |
^ mantissa * (2 raisedTo:exponent)
|
|
325 |
].
|
|
326 |
^ mantissa // (2 raisedTo:exponent negated)
|
|
327 |
|
|
328 |
"
|
|
329 |
(self new exponent:0 mantissa:100) asInteger
|
|
330 |
(self new exponent:1 mantissa:100) asInteger
|
|
331 |
(self new exponent:-1 mantissa:100) asInteger
|
|
332 |
"
|
|
333 |
!
|
|
334 |
|
|
335 |
asLargeFloat
|
|
336 |
"return a large float with same value - thats me"
|
|
337 |
|
|
338 |
^ self
|
|
339 |
!
|
|
340 |
|
|
341 |
asTrueFraction
|
|
342 |
"Answer a fraction or integer that EXACTLY represents self."
|
|
343 |
|
|
344 |
exponent = 0 ifTrue: [ ^ mantissa].
|
|
345 |
|
|
346 |
mantissa == 0 ifTrue:[
|
|
347 |
"/ INF or NAN
|
|
348 |
^ self class
|
|
349 |
raise:#conversionErrorSignal
|
|
350 |
receiver:self
|
|
351 |
selector:#asTrueFraction
|
|
352 |
arguments:#()
|
|
353 |
errorString:'Cannot represent non-finite float as fraction'.
|
|
354 |
"/ ^ self asMetaNumber.
|
|
355 |
].
|
|
356 |
|
|
357 |
exponent > 0 ifTrue: [
|
7552
|
358 |
^ mantissa bitShift:exponent
|
7445
|
359 |
].
|
7552
|
360 |
^ Fraction
|
|
361 |
numerator: mantissa
|
|
362 |
denominator: (1 bitShift:exponent negated)
|
7445
|
363 |
|
|
364 |
"
|
|
365 |
0.3 asFloat asTrueFraction
|
|
366 |
0.3 asShortFloat asTrueFraction
|
|
367 |
0.3 asLongFloat asTrueFraction
|
|
368 |
0.3 asLargeFloat asTrueFraction
|
|
369 |
|
|
370 |
1 asLargeFloat asTrueFraction
|
|
371 |
2 asLargeFloat asTrueFraction
|
|
372 |
0.5 asLargeFloat asTrueFraction
|
|
373 |
|
|
374 |
0.25 asLargeFloat asTrueFraction
|
|
375 |
-0.25 asLargeFloat asTrueFraction
|
|
376 |
0.125 asLargeFloat asTrueFraction
|
|
377 |
-0.125 asLargeFloat asTrueFraction
|
|
378 |
|
|
379 |
1.25 asLargeFloat asTrueFraction
|
|
380 |
3e37 asLargeFloat asTrueFraction
|
|
381 |
|
|
382 |
LargeFloat NaN asTrueFraction -> error
|
|
383 |
LargeFloat infinity asTrueFraction -> error
|
|
384 |
LargeFloat negativeInfinity asTrueFraction -> error
|
|
385 |
"
|
|
386 |
!
|
|
387 |
|
|
388 |
coerce:aNumber
|
|
389 |
"return the argument as a LargeFloat"
|
|
390 |
|
|
391 |
^ aNumber asLargeFloat
|
|
392 |
!
|
|
393 |
|
|
394 |
generality
|
|
395 |
"return the generality value - see ArithmeticValue>>retry:coercing:"
|
|
396 |
|
|
397 |
^ 100
|
|
398 |
! !
|
|
399 |
|
|
400 |
!LargeFloat methodsFor:'comparing'!
|
|
401 |
|
|
402 |
< aNumber
|
|
403 |
"return true, if the argument is greater"
|
|
404 |
|
|
405 |
^ aNumber lessFromLargeFloat:self
|
|
406 |
!
|
|
407 |
|
|
408 |
= aNumber
|
|
409 |
"return true, if the argument is equal in value"
|
|
410 |
|
|
411 |
^ aNumber equalFromLargeFloat:self
|
|
412 |
|
|
413 |
"
|
|
414 |
LargeFloat unity = LargeFloat zero
|
|
415 |
LargeFloat unity = LargeFloat unity
|
|
416 |
|
|
417 |
LargeFloat unity = nil
|
|
418 |
LargeFloat unity ~= nil
|
|
419 |
"
|
|
420 |
!
|
|
421 |
|
|
422 |
hash
|
|
423 |
"return a number for hashing; redefined, since floats compare
|
|
424 |
by numeric value (i.e. 3.0 = 3), therefore 3.0 hash must be the same
|
|
425 |
as 3 hash."
|
|
426 |
|
|
427 |
exponent == 0 ifTrue:[^ mantissa hash].
|
|
428 |
exponent < 64 ifTrue:[^ (mantissa bitShift:exponent) hash ].
|
|
429 |
^ mantissa hash bitXor:exponent hash
|
|
430 |
|
|
431 |
"
|
|
432 |
LargeFloat unity hash
|
|
433 |
LargeFloat zero hash
|
|
434 |
|
|
435 |
3 hash
|
|
436 |
3.0 hash
|
|
437 |
3.1 hash
|
|
438 |
3.14159 hash
|
|
439 |
31.4159 hash
|
|
440 |
3.141591 hash
|
|
441 |
1.234567890123456 hash
|
|
442 |
1.234567890123457 hash
|
|
443 |
Set withAll:#(3 3.0 99 99.0 3.1415)
|
|
444 |
"
|
|
445 |
! !
|
|
446 |
|
|
447 |
!LargeFloat methodsFor:'double dispatching'!
|
|
448 |
|
|
449 |
differenceFromLargeFloat:aLargeFloat
|
|
450 |
|otherExponent otherMantissa e m|
|
|
451 |
|
|
452 |
otherExponent := aLargeFloat exponent.
|
|
453 |
otherMantissa := aLargeFloat mantissa.
|
|
454 |
|
|
455 |
otherMantissa == 0 ifTrue:[
|
|
456 |
otherExponent = 0 ifTrue:[^ self negated].
|
|
457 |
"/ INF or NaN
|
|
458 |
aLargeFloat isNaN ifTrue:[^ NaN].
|
|
459 |
self isFinite ifTrue:[^ aLargeFloat].
|
|
460 |
aLargeFloat sign ~~ self sign ifTrue:[^ self negated].
|
|
461 |
^ NaN
|
|
462 |
].
|
|
463 |
mantissa == 0 ifTrue:[
|
|
464 |
exponent = 0 ifTrue:[^ aLargeFloat].
|
|
465 |
"/ INF or NaN
|
|
466 |
self isNaN ifTrue:[^ NaN].
|
|
467 |
^ self negated
|
|
468 |
].
|
|
469 |
|
|
470 |
otherExponent = exponent ifTrue:[
|
|
471 |
m := otherMantissa - mantissa.
|
|
472 |
e := exponent
|
|
473 |
] ifFalse:[
|
|
474 |
otherExponent> exponent ifTrue:[
|
|
475 |
m := (otherMantissa bitShift:(otherExponent-exponent)) - mantissa.
|
|
476 |
e := exponent
|
|
477 |
] ifFalse:[
|
|
478 |
m := otherMantissa - (mantissa bitShift:(exponent-otherExponent)).
|
|
479 |
e := otherExponent
|
|
480 |
]
|
|
481 |
].
|
|
482 |
^ self class
|
|
483 |
mantissa:m
|
|
484 |
exponent:e
|
7550
|
485 |
precision:(self precision min:aLargeFloat precision)
|
7445
|
486 |
!
|
|
487 |
|
|
488 |
equalFromLargeFloat:aLargeFloat
|
|
489 |
aLargeFloat exponent = exponent ifTrue:[
|
|
490 |
^ aLargeFloat mantissa = mantissa
|
|
491 |
].
|
|
492 |
"assuming normalized numbers, they cannot be equal then"
|
|
493 |
^ false
|
|
494 |
!
|
|
495 |
|
|
496 |
lessFromLargeFloat:aLargeFloat
|
|
497 |
|otherExponent otherMantissa|
|
|
498 |
|
|
499 |
otherExponent := aLargeFloat exponent.
|
|
500 |
otherMantissa := aLargeFloat mantissa.
|
|
501 |
|
|
502 |
otherExponent > exponent ifTrue:[
|
|
503 |
^ otherMantissa < (mantissa bitShift:(otherExponent-exponent))
|
|
504 |
].
|
|
505 |
otherExponent < exponent ifTrue:[
|
|
506 |
^ (otherMantissa bitShift:(exponent-otherExponent)) < mantissa
|
|
507 |
].
|
|
508 |
^ otherMantissa < mantissa
|
|
509 |
!
|
|
510 |
|
|
511 |
productFromLargeFloat:aLargeFloat
|
|
512 |
|otherMantissa otherExponent|
|
|
513 |
|
|
514 |
otherMantissa := aLargeFloat mantissa.
|
|
515 |
otherExponent := aLargeFloat exponent.
|
|
516 |
|
|
517 |
otherMantissa == 0 ifTrue:[
|
|
518 |
otherExponent ~= 0 ifTrue:[
|
|
519 |
"/ INF or NaN
|
|
520 |
aLargeFloat isNaN ifTrue:[^ NaN].
|
|
521 |
self negative ifTrue:[^ aLargeFloat negated].
|
|
522 |
^ aLargeFloat
|
|
523 |
].
|
|
524 |
].
|
|
525 |
mantissa == 0 ifTrue:[
|
|
526 |
exponent = 0 ifTrue:[^ self].
|
|
527 |
"/ INF or NaN
|
|
528 |
self isNaN ifTrue:[^ NaN].
|
|
529 |
aLargeFloat negative ifTrue:[^ self negated].
|
|
530 |
^ self
|
|
531 |
].
|
|
532 |
|
|
533 |
^ self class
|
|
534 |
mantissa:(mantissa * otherMantissa)
|
|
535 |
exponent:(exponent + otherExponent)
|
7550
|
536 |
precision:(self precision min:aLargeFloat precision)
|
7445
|
537 |
!
|
|
538 |
|
|
539 |
quotientFromLargeFloat:aLargeFloat
|
|
540 |
"Return the quotient of the argument, aLargeFloat and the receiver.
|
|
541 |
Sent when aLargeFloat does not know how to divide by the receiver."
|
|
542 |
|
|
543 |
|otherMantissa otherExponent q|
|
|
544 |
|
|
545 |
otherMantissa := aLargeFloat mantissa.
|
|
546 |
otherExponent := aLargeFloat exponent.
|
|
547 |
|
|
548 |
otherMantissa == 0 ifTrue:[
|
|
549 |
otherExponent = 0 ifTrue:[^ aLargeFloat].
|
|
550 |
"/ INF or NaN
|
|
551 |
aLargeFloat isNaN ifTrue:[^ NaN].
|
|
552 |
self negative ifTrue:[^ aLargeFloat negated].
|
|
553 |
^ aLargeFloat
|
|
554 |
].
|
|
555 |
mantissa == 0 ifTrue:[
|
|
556 |
exponent = 0 ifTrue:[^ self].
|
|
557 |
"/ INF or NaN
|
|
558 |
self isNaN ifTrue:[^ NaN].
|
|
559 |
aLargeFloat negative ifTrue:[^ self negated].
|
|
560 |
^ self
|
|
561 |
].
|
|
562 |
q := (otherMantissa / mantissa).
|
|
563 |
q isInteger ifFalse:[
|
|
564 |
self halt.
|
|
565 |
].
|
|
566 |
^ self class
|
|
567 |
mantissa:q
|
|
568 |
exponent:(otherExponent - exponent)
|
|
569 |
!
|
|
570 |
|
|
571 |
sumFromLargeFloat:aLargeFloat
|
|
572 |
|otherExponent otherMantissa e m|
|
|
573 |
|
|
574 |
otherExponent := aLargeFloat exponent.
|
|
575 |
otherMantissa := aLargeFloat mantissa.
|
|
576 |
|
|
577 |
otherMantissa == 0 ifTrue:[
|
|
578 |
otherExponent = 0 ifTrue:[^ self].
|
|
579 |
"/ INF or NaN
|
|
580 |
aLargeFloat isNaN ifTrue:[^ NaN].
|
|
581 |
self isFinite ifTrue:[^ aLargeFloat].
|
|
582 |
aLargeFloat sign == self sign ifTrue:[^ aLargeFloat].
|
|
583 |
^ NaN
|
|
584 |
].
|
|
585 |
mantissa == 0 ifTrue:[
|
|
586 |
exponent = 0 ifTrue:[^ aLargeFloat].
|
|
587 |
"/ INF or NaN
|
|
588 |
self isNaN ifTrue:[^ NaN].
|
|
589 |
^ self
|
|
590 |
].
|
|
591 |
|
|
592 |
otherExponent = exponent ifTrue:[
|
|
593 |
m := otherMantissa + mantissa.
|
|
594 |
e := exponent
|
|
595 |
] ifFalse:[
|
|
596 |
otherExponent> exponent ifTrue:[
|
|
597 |
m := (otherMantissa bitShift:(otherExponent-exponent)) + mantissa.
|
|
598 |
e := exponent
|
|
599 |
] ifFalse:[
|
|
600 |
m := otherMantissa + (mantissa bitShift:(exponent-otherExponent)).
|
|
601 |
e := otherExponent
|
|
602 |
]
|
|
603 |
].
|
|
604 |
^ self class
|
|
605 |
mantissa:m
|
|
606 |
exponent:e
|
7550
|
607 |
precision:(self precision min:aLargeFloat precision)
|
7445
|
608 |
! !
|
|
609 |
|
|
610 |
!LargeFloat methodsFor:'printing'!
|
|
611 |
|
|
612 |
printOn:aStream
|
|
613 |
exponent == 0 ifTrue:[
|
|
614 |
mantissa printOn:aStream.
|
|
615 |
aStream nextPutAll:'.0'.
|
|
616 |
^ self
|
|
617 |
].
|
|
618 |
mantissa == 0 ifTrue:[
|
|
619 |
self == NaN ifTrue:[ aStream nextPutAll:'NAN'. ^ self ].
|
|
620 |
self == NegativeInfinity ifTrue:[ aStream nextPutAll:'-INF'. ^ self].
|
|
621 |
self == PositiveInfinity ifTrue:[ aStream nextPutAll:'INF'. ^ self].
|
7550
|
622 |
self error:'invalid largeFloat' mayProceed:true.
|
|
623 |
aStream nextPutAll:'Invalid'. ^ self.
|
7445
|
624 |
].
|
|
625 |
|
|
626 |
exponent > 0 ifTrue:[
|
7550
|
627 |
self halt.
|
7445
|
628 |
(mantissa bitShift:exponent) printOn:aStream.
|
|
629 |
aStream nextPutAll:'.0'.
|
|
630 |
^ self
|
|
631 |
].
|
|
632 |
((mantissa / (1 bitShift:exponent negated)) asFixedPoint:6) printOn:aStream.
|
7550
|
633 |
|
|
634 |
"
|
|
635 |
|
|
636 |
"
|
7445
|
637 |
! !
|
|
638 |
|
|
639 |
!LargeFloat methodsFor:'private'!
|
|
640 |
|
|
641 |
mantissa:mantissaArg exponent:exponentArg
|
7546
|
642 |
"set instance variables.
|
|
643 |
Notice, that the floats value is m * 2^e"
|
7445
|
644 |
|
|
645 |
exponent := exponentArg.
|
|
646 |
mantissa := mantissaArg.
|
7550
|
647 |
precision := Infinity positive.
|
7445
|
648 |
self normalize.
|
|
649 |
!
|
|
650 |
|
|
651 |
mantissa:mantissaArg exponent:exponentArg precision:precisionArg
|
7546
|
652 |
"set instance variables.
|
|
653 |
Notice, that the floats value is m * 2^e"
|
|
654 |
|
7445
|
655 |
exponent := exponentArg.
|
|
656 |
mantissa := mantissaArg.
|
|
657 |
precision := precisionArg.
|
|
658 |
self normalize
|
|
659 |
!
|
|
660 |
|
|
661 |
normalize
|
|
662 |
"adjust m & e such that m is the smallest possible
|
7546
|
663 |
(i.e. has no least significant zero bit).
|
|
664 |
Notice, that the floats value is m * 2^e"
|
7445
|
665 |
|
|
666 |
|shift|
|
|
667 |
|
|
668 |
shift := mantissa lowBit - 1.
|
|
669 |
shift > 0 ifTrue:[
|
|
670 |
mantissa := mantissa bitShift:shift negated.
|
|
671 |
exponent := exponent + shift
|
|
672 |
].
|
|
673 |
|
|
674 |
"
|
7546
|
675 |
self mantissa:1 exponent:0
|
|
676 |
self mantissa:2 exponent:0
|
|
677 |
self mantissa:4 exponent:0
|
|
678 |
self mantissa:8 exponent:0
|
|
679 |
self mantissa:10 exponent:-1
|
|
680 |
self mantissa:10 exponent:0
|
|
681 |
self mantissa:10 exponent:1
|
7445
|
682 |
"
|
|
683 |
! !
|
|
684 |
|
8634
|
685 |
!LargeFloat methodsFor:'queries'!
|
|
686 |
|
|
687 |
epsilon
|
|
688 |
"answer the radix of a LargeFloats exponent"
|
|
689 |
|
|
690 |
|p|
|
|
691 |
|
|
692 |
p := precision.
|
|
693 |
p isFinite ifTrue:[
|
|
694 |
^ self class radix asFloat raisedTo:(1 - p)
|
|
695 |
].
|
|
696 |
"/ mhmh - what should we use here ???
|
|
697 |
^ LongFloat epsilon
|
|
698 |
! !
|
|
699 |
|
7445
|
700 |
!LargeFloat methodsFor:'testing'!
|
|
701 |
|
|
702 |
isFinite
|
|
703 |
^ mantissa ~= 0 or:[exponent = 0]
|
|
704 |
!
|
|
705 |
|
|
706 |
isInfinite
|
|
707 |
^ mantissa = 0 and:[exponent ~= 0]
|
|
708 |
!
|
|
709 |
|
|
710 |
isNaN
|
|
711 |
^ self == NaN
|
|
712 |
!
|
|
713 |
|
|
714 |
isZero
|
|
715 |
^ self == Zero
|
|
716 |
!
|
|
717 |
|
|
718 |
negative
|
|
719 |
"return true if the receiver is negative"
|
|
720 |
|
|
721 |
mantissa == 0 ifTrue:[ ^ exponent negative].
|
|
722 |
^ mantissa negative
|
|
723 |
!
|
|
724 |
|
|
725 |
sign
|
|
726 |
"return the sign of the receiver"
|
|
727 |
|
7546
|
728 |
mantissa == 0 ifTrue:[
|
|
729 |
"special value for infinites"
|
|
730 |
^ exponent sign
|
|
731 |
].
|
7445
|
732 |
^ mantissa sign
|
|
733 |
! !
|
|
734 |
|
|
735 |
!LargeFloat class methodsFor:'documentation'!
|
|
736 |
|
|
737 |
version
|
8634
|
738 |
^ '$Header: /cvs/stx/stx/libbasic/LargeFloat.st,v 1.7 2004-11-12 12:23:46 cg Exp $'
|
7445
|
739 |
! !
|
|
740 |
|
|
741 |
LargeFloat initialize!
|