Bill Allombert on Sun, 15 Dec 2024 12:31:56 +0100


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

Re: digits of a float


On Sat, Dec 14, 2024 at 12:07:29PM +0100, Ruud H.G. van Tol wrote:
> 
> How to properly isolate a digit of a float?
> 
> 
> Example:
> 
> ? my(n=19437); localprec(n+2); Pi *10^(n-1) \1 %10
> %4 = 7
> 
> ? my(n=19437); localprec(n+3); Pi *10^(n-1) \1 %10
> %5 = 6
> 
> That type of code depends on the number of 9-digits in the decimal expansion
> of the value.
> 
> See also https://oeis.org/A000796, which uses localprec(n*6\5+29).
> 
> What clean ways are there?

This is difficult. One need to round toward 0 instead of rounding to nearest
as usual.

In this instance the number end by 574699992... which is (correctly) rounded to nearest
by PARI to 5747.....

I wrote this, which increase the accuracy until there is no more ambiguity.
{
  my(n=19437,z); 
  for(m=n+1,oo,
    localprec(m+1); 
    z = round(Pi *10^m)/10^m; 
    if(abs(z-Pi)>10^-(m+1),break));
  z*10^(n-1)\1%10
}

but this need to be checked more carefully

Cheers,
Bill