Karim Belabas on Fri, 10 Oct 2014 07:43:20 +0200

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

Re: experimental localprec() feature

* Loïc Grenié [2014-10-09 22:41]:
> 2014-10-09 17:37 GMT+02:00 Bill Allombert <Bill.Allombert@math.u-bordeaux.fr>:
>> I have created an experimental git branch bill-localprec that adds a command
>> localprec() which allow to set the real precision locally:
>> Consider the function:
>> f(s)=Pi^s
>> and
>> s=precision(1.,100)
>> Pi is computed using the current precision.
>> If we want the resut to be correct to the precision of s,
>> we could do
>> g(s)=
>> { my(prec=default(realprecision));
>>   default(realprecision,precision(s));
>>   my(r=Pi^s);
>>   default(realprecision,prec);
>>   r;
>> }
>> which is rather cumbersome. Using localprec() it is sufficient to say:
>> g(s)=localprec(precision(s));Pi^s
>> Also it is safer since the value of realprecision is restored even if an
>> error occurs.
>      Nice! Thanks! That's something that I have dreamed for a long time.

BTW, some precisions:

1) as Bill's original save/restore example suggests, it is really a
'localprec' with the semantics of 'local' (dynamic scoping); not a
'myprec' with the semantics of 'my' (lexical scoping). I.e.


Then f() does compute Pi with 100 decimals, even though the call to 'Pi' 
itself occurs outside of the body of f. Note that


returns a result with 38 decimals of accuracy since g in that case is a
constant that was previously computed and not a function evaluated when f() is
called, while localprec is in effect.

2) 'realprecision' itself is unaffected by localprec settings

  ? localprec(100);default(realprecision);
  %1 = 38

Conversely, changing 'realprecision' from within a localprec() scope
is a local no-op. The new accuracy is overriden by localprec settings
in that scope:

  ? \p100
  ? localprec(57); default(realprecision,38); precision(Pi)
  %2 = 57

It does become the new default precision, of course

  ? default(realprecision)
  %3 = 38

Thus one should no longer change default(realprecision,) in new scripts: it
will interact with localprec() in confusing ways. This changes for the better:
'realprecision' becomes a true global "default" corresponding to user's
preferences in a given context.


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