[BUG] Fraction>>asFloat returns infinity if numerator and denominator both very large
Tim Olson
tim at jumpnet.com
Mon Oct 9 23:49:27 UTC 2000
>In any case, getting Infinity out of this is bogus and is definately a
>bug.
Yes, I agree. The scale factor code is there to allow creation of
IEEE-754 denormalized numbers with the use of fractions. However, it
does cause the wrong result (Infinity) to be returned when both the
numerator and denominator are represented by Float infinity.
Here's one solution:
----
'From Squeak2.8 of 13 June 2000 [latest update: #2359] on 9 October 2000
at 6:45:29 pm'!
!Fraction methodsFor: 'converting' stamp: 'tao 10/9/2000 18:39'!
asFloat
"Answer a Float that represents the same value as does the receiver."
| nf df scaleFactor |
nf _ numerator asFloat.
df _ denominator asFloat.
nf isInfinite ifTrue:
["scale to possibly remove infinities"
scaleFactor _ 2 raisedToInteger: ((denominator digitLength * 8 - 52)
max: 0).
nf _ (numerator / scaleFactor) rounded asFloat.
df _ (denominator / scaleFactor) rounded asFloat.
^ nf / df].
df isInfinite ifTrue:
["scale to possibly represent as denorm"
scaleFactor _ 2 raisedToInteger: 53.
df _ (denominator / scaleFactor) rounded asFloat.
^ nf / scaleFactor asFloat / df].
^ nf / df
! !
----
-- tim
More information about the Squeak-dev
mailing list
|