Re: Fast Reduced Fractions...
[Prev][Next][Index][Thread]
Re: Fast Reduced Fractions...
just a suggestion - instead of trying to approximate the fraction, why not do
this:
suppose that
float x = .0024
.0024 = 24/10000 = 12/5000 = 6/2500 = 3/1250
since any number you get in c++ is going to have a limited number of decimal
places (even doubles, with their huge ranges (how big do they get? something
like 1.96e+380?), have a limited number of significant figures...so any real
number you're presented with can be expressed as an integer divided by a power
of 10. After that, all you have to do is reduce the fractions, which should be a
lot easier/faster than what you were trying - i'm too sleepy to think about it
now (hey, any other day i'd have tried to write the program my self - spent
today drawing screen layout diagrams for my computing project...yeeech...), but
i think you could just try sucessive division of numerator and denominator by
increasingly large prime numbers...i'll try it tomorrow.
i really think this should work...
here's some quick c++ code that should do the decimal to fraction conversion -
probably better ways, but like i said, i'm sleepy.
i = 0;
cout << "Enter real number: ";
cin >> x;
num = x;
denom = 1;
while (num != floor(num)) {
num *= 10;
i++;
}
denom = pow10(i);
cout << num << " / " << denom << endl;
now you need to reduce the fraction - hey, i guess that means dividig num and
denom by their HCF...can't for the life of me remember how to calculate an
HCF...
===== === == = = ==== = = = == = = = = = == = = =
...from the aspiring geek, the $al-Man
(AKA NineTurningMirrors, AKA ByteChild)
alien-angel@the-pentagon.com - smoke_jaguar@email.com
ICQ #: 58895095 - http://members.nbci.com/ZeroMaster/
====== = = = ====== = ===== = === == =
Things that never happen in Star Trek (Classic & Next Gen)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1) The Enterprise runs into a mysterious energy field of a type
that it has encountered several times before.
----- Original Message -----
From: Thomas J. Hruska <shinelight@CROSSWINDS.NET>
To: <CALC-TI@LISTS.PPP.TI.COM>
Sent: Monday, March 26, 2001 11:37 A
Subject: 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
> ******************************************************************
>
******************************************************************
* 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: