[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] Conversion decimal into fractional




On Dec 23, 2004, at 1:18 AM, Alberto de Campo wrote:

one more possible speedup:

I'd like to find a general solution to the problem where the value lies very close to an initial value and avoid the slow convergences that can occur.

such as finding a value slightly less than 1.0 you get 1/2 2/3 3/4 4/5 5/6 6/7 7/8 ...

the proportion of the interval moved each time towards 1/1 is not very large.

If lower is a/b and upper is c/d and the value being searched for, x, is greater than the mediant, I'd like to find out the value k for which (a + k*c) / (b + k*d) becomes greater than or equal to x.
and like wise for x < mediant, k in (k*a + c) / (k*b + d) <= x
If I could do that in one step instead of iteratively then the searches could be made faster.



	asFraction {|maxDenominator=100|
		var mediant, lower, upper;
		var n,d;
			// early exit if number is integer.
		if (this.trunc == this, { ^[this, 1] });
	...
	}

and maybe these would be useful too:

I think I'll pass on these.


+ Symbol {
	asFraction { ^this }
}

+ SequenceableCollection {
	asFraction { ^this.collect { |el| el.asFraction } }
}