Rick Regan on Fri, 05 Jun 2009 21:19:46 +0200

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

```I hadn't shown it but I did a \p 500 before
bestappr(0.1000000000000000055511151231257827021181583404541015626,10^100).
I used 10^100, but any power of 10 from 10^55 and higher (the decimal
value to approximate is 55 digits) gives the same answer:
500000000000000027755575615628913510590791702270507813/5000000000000000000000000000000000000000000000000000000.

Thanks for the heads-up on version 2.4.3.

Thanks to all for the help.

On Fri, Jun 5, 2009 at 10:33 AM, Karim Belabas
<Karim.Belabas@math.u-bordeaux1.fr> wrote:
> * 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]
> `
>

--
“There are 10 types of people ... those who understand binary and
those who don't” -- http://www.exploringbinary.com

```