| Ruud H.G. van Tol on Fri, 27 Dec 2024 15:11:28 +0100 | 
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| Re: digits of a float | 
On 2024-12-26 19:21, Loïc Grenié wrote:
On Thu 26 Dec, 2024, at 17:21, Ruud H.G. van Tol wrote:
    [...] For Pi-digits, I am now using:
    pi_digits(n, p=20)= {
       my(u=10^(n+p-1), f(x, u)=my(n=1, q=u\x, r=q, s=1, t);
    while(t=(q\=(x*x))\(n+=2), r+=(s=-s)*t); r*4);
       digits((4*f(5, u) - f(239, u))\10^p);
    } \\ Machin-like, with p > the maximal number of consecutive
    9-digits to
    be expected (A048940)
Wouldn't floor be more adapted than round?
The code has an off-by-1 error. Corrected:
pi_digits(n, p=20)= {
  my(u=10^(n+p), f(x, u)=my(n=1, q=u\x, r=q, s=1, t); 
while(t=(q\=(x*x))\(n+=2), r+=(s=-s)*t); r*4);
digits((4*f(5, u) - f(239, u))\10^(p+1)); } With a low value of p, the rounding shows: ? pi_digits( 769, 10 )[-10..-1] % [1, 3, 4, 9, 9, 9, 9, 9, 9, 8] :OK ? pi_digits( 763, 6 )[-4..-1] % [1, 3, 5, 0] :BAD ? pi_digits( 762, 7 )[-3..-1] % [1, 3, 5] :BAD (also invalidates the comment about what p is needed) -- Ruud