Karim Belabas on Fri, 05 Jun 2009 17:15:49 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

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


* John Cremona [2009-06-05 15:42]:
> It was not supposed to be off list ;)
> 
> bestappr() uses continued fractions, and cannot do better than the
> precision you give it, so it is not very clear what the "exact
> fraction" is (unless you specify the decimal expansion in such detail
> that the full period is seen!)
> 
> John
> 
> 2009/6/5 Rick Regan <exploringbinary@gmail.com>:
> > Thanks Bill (and John who contacted me off list). -- bestappr() does the trick!
>>
>> In my case I always want the exact fraction, so I have to make sure I
>> specify a large enough denominator. I am converting long decimals, and
>> I don't want to count decimal places. The easy solution seems to be to
>> pass bestappr() an arbitrarily large power of 10, like
>> bestappr(0.1000000000000000055511151231257827021181583404541015626,10^100).

Actually, in current svn (2.4.3), the second argument to bestappr() was
made optional. If the argument is omitted, we compute the continued fraction
as far as the supplied decimals will let us and return a "best rational
approximation", given the input.

For instance, at default accuracy (\p28),

? bestappr(0.333333)
%1 = 333333/1000000
? bestappr(0.33333333333333333333333333333333333333333)
%2 = 1/3

etc.

Cheers,

    K.B.

P.S: In general, the proposed "solution" bestappr(..., 10^100) doesn't
work, in fact

? bestappr(0.1000000000000000055511151231257827021181583404541015626,10^100)
  ***   at top-level: bestappr(0.100000000
  ***                 ^--------------------
  *** bestappr: precision too low in truncr (precision loss in truncation).

whereas

? bestappr(0.1000000000000000055511151231257827021181583404541015626)
%3 = 3602879701896397/36028797018963968

--
Karim Belabas, IMB (UMR 5251)  Tel: (+33) (0)5 40 00 26 17
Universite Bordeaux 1          Fax: (+33) (0)5 40 00 69 50
351, cours de la Liberation    http://www.math.u-bordeaux1.fr/~belabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux1.fr/  [PARI/GP]
`