Incorrect value of Pi in Lua after 16th Decimal Place

You can cheat and do this using Metapost via the luamplib package.

enter image description here

\documentclass{article}
\usepackage{luamplib}
\mplibtextextlabel{enable}
\mplibnumbersystem{decimal}
\begin{document}
This is $\pi$:
\begin{mplibcode}
vardef getpi =
numeric lasts, t, s, n, na, d, da;
lasts=0; s=t=3; n=1; na=0; d=0; da=24;
forever:
  exitif lasts=s;
  lasts := s;
  n := n+na; na := na+8;
  d := d+da; da := da+32; 
  t := t*n/d;
  s := s+t;
endfor
s
enddef;
beginfig(1);
    draw textext(decimal getpi);
endfig;
\end{mplibcode}
\end{document}

But I think it might be easier just to write the digits out by hand!


From the Lua manual, section 2.1

Standard Lua uses 64-bit integers and double-precision (64-bit) floats, but you can also compile Lua so that it uses 32-bit integers and/or single-precision (32-bit) floats. The option with 32 bits for both integers and floats is particularly attractive for small machines and embedded systems.

With double precision, you can't expect more than 15 exact decimal digits, see https://en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange_formats