Re: Fast Reduced Fractions...
[Prev][Next][Index][Thread]
Re: Fast Reduced Fractions...
Hey excellent man I just finished doing one for my COMPUTER SCIENCE class
here it is And it is in C++.
// Gabe Hughes
// proj8-11.c
#include<iostream.h>
main()
{
int b,c,d,e;
cout << "Enter the numerator: ";
cin >> b;
cout << "Enter the denominator: ";
cin >> c;
if(b>c)
{
d=b;
}
else
{
d=c;
}
for(e=d;e>=2;e--)
{
if(b%e==0 && c%e==0)
{
b=b/e;
c=c/e;
}
}
cout << "The reduced fraction is " << b << "/" << c << ".";
return 0;
}
On Mon, 26 Mar 2001, Thomas J. Hruska wrote:
> 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
> ******************************************************************
>
******************************************************************
* 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
******************************************************************
References: