Karim Belabas on Tue, 27 Sep 2005 20:01:49 +0200


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

Re: setting output precision


* Bill Allombert [2005-09-27 19:02]:
> On Tue, Sep 27, 2005 at 05:42:31PM +0100, Prof. J. E. Cremona wrote:
> > Is there an easy way in gp to do computations to high precision (say, 
> > 100) but only output in low precision (say, 10)?  I know that I can 
> > achieve this by adding lots of lines like
> > default(realprecision,100);
> > default(realprecision,10);
> > in my script, but thought there might be a better way of just turning 
> > down the precision for output only.
> 
> I think the "format" default is what you need:
> ? default("realprecision",100);
>    realprecision = 105 significant digits (100 digits displayed)
> ? default("format","g0.10")
>    format = g0.10
> ? Pi
> %1 = 3.141592654
> ? round(%*10^50)
> %2 = 314159265358979323846264338327950288419716939937511
> 
> 
> The only problem I see is that you need to set it _after_ realprecision
> which seems strange.

This is the historical behaviour, unfortunately not really documented:
* changing 'realprecision' automatically updates the (maximal) number of
significant digits printed to current 'realprecision'.

* changing 'format' doesn't affect 'realprecision'.

The apparent idea is that one should always print as many digits as are
available. Trivial to change, but...

In my opinion, 'format' is difficult to use, so we should implement a
decent 'printf' command 

e.g.

  printf("%.3Z + %.5Z", Pi, log(2))

would yield

  3.141 + 0.69315

A prototype implementation expanding "PARI formats" (for formatted output
of t_INTs and t_REALs) was sent to me around July by Alain Smejkal, but I
didn't have time to properly check it yet. [ His format follows standard
specifications and supports left justification, 0 padding, maximal width and
# of decimals. It's not exactly trivial to check. ]

Cheers,

    Karim.
-- 
Karim Belabas                  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-bordeaux.fr/~belabas/
F-33405 Talence (France)       http://pari.math.u-bordeaux.fr/  [PARI/GP]