<div dir="ltr"><div><div><div><div>Here is the version that i use in VW (SYSBUG-FloatConversion in cincom public store)<br><br>asApproximateFraction<br> "Answer a Rational number--Integer or Fraction--representing the receiver.<br> This conversion uses the continued fraction method to approximate <br> a floating point number."<br><br> | num1 denom1 num2 denom2 int frac newD temp limit |<br> num1 := self asFraction. "use exact arithmetic"<br> frac := num1 fractionPart. "The fractional part of self"<br> int := num1 truncated. "The integer part of self"<br> num1 := int. "The first of two alternating numerators"<br> denom1 := 1. "The first of two alternating denominators"<br> num2 := 1. "The second numerator"<br> denom2 := 0. "The second denominator--will update"<br> limit := self class precision + 7 // 8 + 1.<br> [frac = 0 or: [denom1 digitLength > limit or: [(self coerce: (Fraction numerator: num1 denominator: denom1)) = self]]]<br> whileFalse: <br> ["repeat while the fractional part is not zero"<br> newD := frac reciprocal. "Take reciprocal of the fractional part"<br> int := newD truncated. "get the integer part of this"<br> frac := newD fractionPart. "and save the fractional part for next time"<br> temp := num2. "Get old numerator and save it"<br> num2 := num1. "Set second numerator to first"<br> num1 := num1 * int + temp. "Update first numerator"<br> temp := denom2. "Get old denominator and save it"<br> denom2 := denom1. "Set second denominator to first"<br> denom1 := int * denom1 + temp. "Update first denominator"].<br> "If fractional part is zero, return the first ratio"<br> denom1 = 1<br> ifTrue: ["Am i really an Integer?"<br> ^num1"Yes, return Integer result"]<br> ifFalse: ["Otherwise return Fraction result"<br> ^Fraction numerator: num1 denominator: denom1]<br><br></div>Note that I use self asFraction (asTrueFraction) and then perform all operations in Integer arithmetic.<br></div>#digitLength is the number of bytes like in Squeak.<br><br>For user-defined controlled precision like aimed by Eliot, we could use #decimalDigitLength and seed it by default with something like #decimalPrecision in Squeak.<br>
#decimalPrecision does not exist yet
but could be (self class precision * 2 ln / 10 ln) rounded - or simply 15 or 16.<br></div><div>Note that it's an absolute precision, not a relative precision which is used.<br></div><div><br></div>Also note that we could rewrite the iteration like this:<br></div> num1 := num2 + ((num2 := num1) * int).<br> den1 := den2 + ((den2 := den1) * int).
<br></div><div class="gmail_extra"><br><div class="gmail_quote">2018-04-26 4:48 GMT+02:00 <span dir="ltr"><<a href="mailto:commits@source.squeak.org" target="_blank">commits@source.squeak.org</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">David T. Lewis uploaded a new version of Kernel to project The Inbox:<br>
<a href="http://source.squeak.org/inbox/Kernel-dtl.1166.mcz" rel="noreferrer" target="_blank">http://source.squeak.org/<wbr>inbox/Kernel-dtl.1166.mcz</a><br>
<br>
==================== Summary ====================<br>
<br>
Name: Kernel-dtl.1166<br>
Author: dtl<br>
Time: 25 April 2018, 10:48:38.572237 pm<br>
UUID: 43d80609-fbc5-40a1-9d49-<wbr>37d2c4f0b306<br>
Ancestors: Kernel-dtl.1165<br>
<br>
In calculation of asApproximateFraction, the digit count for floatPrecision should be<br>
an integer, so do not convert it with asFlloat.<br>
<br>
Allows the following to be resolved without infinite loop on comparison to Float infinity:<br>
((FloatArray new: 1) at: 1 put: 1 / 3; at: 1) asApproximateFractionFloatPrec<wbr>ision: 10000<br>
<br>
=============== Diff against Kernel-dtl.1165 ===============<br>
<br>
Item was changed:<br>
----- Method: Float>><wbr>asApproximateFractionAtOrder:<wbr>floatPrecision: (in category 'converting') -----<br>
asApproximateFractionAtOrder: maxOrder floatPrecision: decimalPlaces<br>
"Answer a Fraction approximating the receiver. This conversion uses the <br>
continued fraction method to approximate a floating point number. If maxOrder<br>
is zero, use maximum order. Assume that I am a float with decimalPlaces of<br>
meaningful accuracy, regardless of the precision of my internal representation."<br>
<br>
| num1 denom1 num2 denom2 int frac newD temp order floatPrecision |<br>
num1 := self asInteger. "The first of two alternating numerators"<br>
denom1 := 1. "The first of two alternating denominators"<br>
num2 := 1. "The second numerator"<br>
denom2 := 0. "The second denominator--will update"<br>
int := num1. "The integer part of self"<br>
frac := self fractionPart. "The fractional part of self"<br>
order := maxOrder = 0 ifTrue: [-1] ifFalse: [maxOrder].<br>
+ floatPrecision := 10 raisedTo: decimalPlaces.<br>
- floatPrecision := (10 raisedTo: decimalPlaces) asFloat.<br>
[frac = 0 or: [order = 0] ]<br>
whileFalse: <br>
["repeat while the fractional part is not zero and max order is not reached"<br>
order := order - 1.<br>
newD := 1.0 / frac. "Take reciprocal of the fractional part"<br>
int := newD asInteger. "get the integer part of this"<br>
frac := newD fractionPart. "and save the fractional part for next time"<br>
temp := num2. "Get old numerator and save it"<br>
num2 := num1. "Set second numerator to first"<br>
num1 := num1 * int + temp. "Update first numerator"<br>
temp := denom2. "Get old denominator and save it"<br>
denom2 := denom1. "Set second denominator to first"<br>
denom1 := int * denom1 + temp. "Update first denominator"<br>
floatPrecision < denom1<br>
ifTrue: <br>
["Is ratio past float precision? If so, pick which <br>
of the two ratios to use"<br>
num2 = 0.0 <br>
ifTrue: ["Is second denominator 0?"<br>
^ Fraction numerator: num1 denominator: denom1].<br>
^ Fraction numerator: num2 denominator: denom2]].<br>
"If fractional part is zero, return the first ratio"<br>
denom1 = 1<br>
ifTrue: ["Am I really an Integer?"<br>
^ num1 "Yes, return Integer result"]<br>
ifFalse: ["Otherwise return Fraction result"<br>
^ Fraction numerator: num1 denominator: denom1]!<br>
<br>
<br>
</blockquote></div><br></div>