views:

91

answers:

1

I'm looking for an automated way to turn a decimal value into a fraction in the PARI/GP calculator (gp). For example, I want to turn 0.759765625 into 389/512. I know I can do this manually by typing in '759765625/10^9', but I'd like an easier way, like 'rationalize(0.759765625);' would work in Maxima.

If it can't do this directly, maybe there's a function to count the number of decimal places? Then I could raise 10 to the result of that function. (The function would have to count leading decimal places of 0s to be useful.)

+1  A: 

Use the bestappr() function; for the example given, use bestappr(0.759765625,10^9). (Answer courtesy of the PARI/GP mailing list.)

This still has the disadvantage that the number of decimal digits must be specified manually, but this can be overcome: the second parameter to bestappr() can be made a very large power of 10, larger than the longest decimal you will ever need to "rationalize".

Caution: make sure to set the precision high enough before calling bestappr, using the \p command.

Rick Regan