Search code examples
computer-algebra-systemspari-gp

Is There a Way to Rationalize a Decimal in Pari/GP?


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.)


Solution

  • In pari-2.5, you can use bestappr(x) directly, without specifying the extra number-of-digits argument. It'll use all internally available digits:

    ? bestappr(Pi)
    %1 = 17004845848539028973023/5412810546621363616752