Hi everyone,
I am trying to convert decimal numbers to fractions. I did manage to get something working but then hit the problem of handling negative decimals.
Does anyone have a way of converting decimals to fractions? I need something to convert the following
decimal convert to fraction
1.2 1/5
1.36 4/11
2.25 5/4
etc etc
If you have anything I could use or better still point me towards an article that would be great. I have tried google but found nothing suitable :-(
Many thanks in advance
Colin
Loading
Mike GoldPosted May 17, 2007, 11:15 AM
Colin AdanPosted May 15, 2007, 2:48 PM
I think I'm missing something.
I think I understand what the function is doing and I understand the need to multiply to get rid of the decimalisation in the original number. But the fuction takes two parameters. The first is surely the normalised decimal, but what is the second parameter?
Many thanks again,
Colin
Colin AdanPosted May 15, 2007, 4:27 AM
Very many thanks indeed
Regards,
Colin
Mike GoldPosted May 14, 2007, 3:18 PM
2) Track the multiple
3) Then use Euclid's gcd (greatest common denominator) algorithm on the numerator and denominator (see below) to determine what to divide the numerator and denominator by.
function extended_gcd(a, b) if a mod b = 0 return {0, 1} else temp := extended_gcd(b, a mod b) x := first(temp) y := last(temp) return {y, x-y*(a div b)}