Karim BELABAS on Wed, 27 Aug 2003 19:07:50 +0200 (MEST)


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

Re: Truncating real numbers


On Mon, 11 Aug 2003, Bill Allombert wrote:
> On Wed, Jul 30, 2003 at 10:38:22PM +0200, Damien Wyart wrote:
>> With default precision at startup, I get :
>>
>> ? a = 2.7646535646854
>> ? b = precision(a,4)
>> %2 = 2.76465356
>>
>> With isn't %2 2.7646 ??
>>
>> Ok, I can do (notice I have to use '5' and not '4' so if the number is >
>> 10, I have to ask for 6 significant digits) :
>>
>> ? \p 5
>> ? a
>> %3 = 2.7646
>>
>> But then, I have to set \p back for the next step if I want to remain
>> precise enough. And a is not containing exactly the number 2.7646, it is
>> only a display effect.
>>
>> I am missing something ?
>
> Probably yes. Precision in PARI is handled by full words not by digits and
> stored in the objects themself.
>
> The 'default precision' serves (unfortunately) two purposes:
> 1) to decide what precision get an exact object demoted to an inexact one:
[...]
> 2) to denote the numbers of digits to be printed.
>
> Unfortunately this is the only way currently to write real numbers
> with a fixed number of digits.

This is almost true. You can also use 'format'

(19:01) gp > default(format,"g0.4")  \\ write floats with 4 significant digits
   format = g0.4
(19:02) gp > 1./3
%1 = 0.3333

Unfortunately, this also needs to be saved then restored as suggested in
Bill's post...

A printf-like command is also on the TODO list and would be much, much, more
flexible,  e.g printf("%.4Z", x).

    Karim.
-- 
Karim Belabas                     Tel: (+33) (0)1 69 15 57 48
Dép. de Mathématiques, Bât. 425   Fax: (+33) (0)1 69 15 60 19
Université Paris-Sud              http://www.math.u-psud.fr/~belabas/
F-91405 Orsay (France)            http://www.parigp-home.de/  [PARI/GP]