Fast Reduced Fractions...
[Prev][Next][Index][Thread]
Fast Reduced Fractions...
Hello, I'm currently hunting for a way to generate a reduced fraction from
a decimal using C (the language doesn't matter, I'm after a formula). I've
found a solution, but as you will see, it is not pretty with large values
for the numerator and denominator:
int x, y;
double MyDec = 99999998/99999999;
y = 2;
x = 1;
while(abs((double)x/(double)y - MyDec) > .00000000000001)
{
x++;
if (x == y)
{
x = 1;
y++;
}
}
printf("%i/%i\n", x, y);
Yes, it should work (untested and for good reason)...but with the above
MyDec value it will take a *LOOONG* time to process (and might print a
close but wrong answer). What I want is a formula to calculate a correct
reduced fraction from a decimal in a single pass without the absolute value
and comparison (I could optimize the above code, but I really want a single
pass formula).
Any suggestions? I'm going to go look through some ancient math books
tomorrow, but I thought I would throw the question out tonight so all of
you could chew on it for a while.
Thomas J. Hruska -- shinelight@crosswinds.net
Shining Light Productions -- "Meeting the needs of fellow programmers"
http://www.shininglightpro.com/
******************************************************************
* To UNSUBSCRIBE, send an email TO: listserv@lists.ppp.ti.com
* with a message (not the subject) that reads SIGNOFF CALC-TI
*
* Archives at http://peach.ease.lsoft.com/archives/calc-ti.html
******************************************************************
Follow-Ups: