Re: Fraction>>asFloat (FloatingPoint Overflow)
- From: nicolas cellier <ncellier@xxxxxxxxxxx>
- Date: Fri, 23 Mar 2007 03:00:40 +0100
Hi Howard,
Yes, i see two problems
1) ((1 bitShift: 10000)/3)asFloat should raise an overflow, note a zero
divide
2) your float won't be as precise as it could be
(i mean it likely won't be the closest float to the fraction)
See http://ncellier.ifrance.com/Dolphin_packages.html (via
http://www.dolphinmap.net/wiki/Goodies)
Nicolas
Howard Oh a e'crit :
A very precise but not a big fraction like:.
(4941354647941236926152115130903637082561047305201416295417964660936672448910241499199436867464432378497521953013385156994196064073355718205444063842406844645633777822001794087786934643288359176347668049138450287177670914977920820153695869859050205437477052264104760638348351154610327758358316855246056228125726457933011243259488904319976931378470626978044536189271422830698827951298790996380571297736272436985642542223705097895166027/7128865274665093053166384155714272920668358861885893040452001991154324087581111499476444151913871586911717817019575256512980264067621009251465871004305131072686268143200196609974862745937188343705015434452523739745298963145674982128236956232823794011068809262317708861979540791247754558049326475737829923352751796735248042463638051137034331214781746850878453485678021888075373249921995672056932029099390891687487672697950931603520000)
will raises 'Floating point error: Overflow' error when #asFloat is
sent.
I kinda fixed
Fraction>>asFloat
^[numerator asFloat / denominator asFloat] on:
FloatingPointException
do: [:exception|
(exception printString
includesSubCollection: 'Floating point error: Overflow')
ifTrue: [ |fmax newN newD|
fmax := Float fmax asInteger.
newN := numerator.
newD := denominator.
[newN abs < fmax and: [newD abs < fmax]]
whileFalse: [ newN := newN // 2. newD := newD // 2 ].
(newN / newD) asFloat ]
ifFalse: [exception signal] ]
It is now evaluates;
... asFloat "0.69314743055982"
I a little scaled to modify Fraction.
Is there a potential problem in my solution?
Is there a safer way?
Have a good one
Howard
- References:
- Fraction>>asFloat (FloatingPoint Overflow)
- From: Howard Oh
- Fraction>>asFloat (FloatingPoint Overflow)
- Prev by Date: Fraction>>asFloat (FloatingPoint Overflow)
- Next by Date: Re: GemBuilder for GemStone 5.1.4
- Previous by thread: Fraction>>asFloat (FloatingPoint Overflow)
- Index(es):
Relevant Pages
|