Bill Allombert on Thu, 21 Jan 2016 11:14:58 +0100 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: Using localprec to add guard word(s) |
On Thu, Jan 21, 2016 at 02:22:01AM -0500, Charles Greathouse wrote: > I have what I imagine is a common use case for localprec(): I have a > calculation which is likely to need additional precision so it can return a > result at the current precision. My inclination was to write > > foo()=localprec(default(realprecision)+9); ... > > but I realized that default(realprecision) is really the wrong thing, as it > could be higher or lower than the current precision, as indeed ??localprec > states. Is there a good way to do this? You can use precision(1.) instead of default(realprecision). However in practice consider whether you have some inexact input x: in which case you should use the precision of x instead. Consider this function, which return x modulo Pi: rpi(x)= { my(q=floor(x/Pi)); my(nprec=bitprecision(1.)+logint(q,2)); localbitprec(nprec); x-Pi*q } ? rpi(10^22) %20 = 2.1214152610307062651444413979983384289 which is correct. However, if x is intended to be a t_REAL, then rpi(x)= { my(q=floor(x/Pi)); my(nprec=bitprecision(x)+logint(q,2)); localbitprec(nprec); x-Pi*q } is better since the precision of x can be larger than the default precision. Cheers, Bill.