Video
Audio: Italiano. You can change or turn off the subtitles.
Left and right rectangles for the quarter circle give bounds for π whose gap is 4/n. Machin takes a different route: two small angles make the error shrink much faster.

Where does 239 come from?
Let a = arctan(1/5). Apply tan(2a) = 2 tan(a)/(1−tan²(a)) twice, then use the tangent of a difference to find how much 4a exceeds π/4.
tan(2a) = (2/5)/(1 − 1/25) = 5/12 tan(4a) = (2·5/12)/(1 − 25/144) = 120/119 tan(4a − π/4) = (120/119 − 1)/(1 + 120/119) = 1/239
Tangent repeats every π. Since 0 < 2a < π/2 and tan(2a) = 5/12 < 1, we have 2a < π/4. Thus 4a < π/2 and 4a − π/4 lies in the principal range of arctangent. We can safely invert tangent.
π = 16 arctan(1/5) − 4 arctan(1/239)
From integral to series
The derivative of arctan(x) is 1/(1+x²). Expand it as a geometric series and integrate from 0 to x. After m terms, the remainder has the sign of the first omitted term and its magnitude is no larger. At x = 1/5 each term is less than one twenty-fifth of the previous one in magnitude.
1/(1+x²) = 1 − x² + x⁴ − x⁶ + … arctan(x) = x − x³/3 + x⁵/5 − x⁷/7 + … |Rₘ| ≤ x^(2m+1)/(2m+1) (0 < x < 1)
How to store every computed digit
A float is not enough. Choose n requested digits and g guard digits. BigInt stores an integer, and D fixes the decimal point: 314159 at scale 10⁵ means 3.14159. Round every term down and up; for negative terms, swap the endpoints before changing the sign.
D = 10^(n+g) q = 5, 239 den = (2k+1)·q^(2k+1) floor = D div den ceil = floor + (D mod den ≠ 0 ? 1 : 0) [L, U] = [16·L₅ − 4·U₂₃₉, 16·U₅ − 4·L₂₃₉]
Add the remainder bound to the appropriate endpoint. When subtracting the second arctangent, swap its bounds. If L and U give the same integer quotient on division by 10ᵍ, the n truncated decimal digits are certified. Otherwise increase g.
Convert the integer to text and insert the decimal point without converting to Number. The TXT file preserves every requested digit, including trailing zeros. It does not contain infinitely many digits: only the requested and certified ones.
Open the Machin lab · Back to the numerical integration lab
How do we extract the i-th decimal digit?
Count decimal digits from 1: in 3.141592… the first is 1, the second is 4 and the fifth is 9. The integer part does not count. The lab removes the first two characters, “3.”, from the certified string, leaving “141592…”. JavaScript indices begin at 0, so the fifth digit is at index 4.
To take n digits starting at position i, use decimali.slice(i - 1, i - 1 + n): the end index is exclusive. For i = 2 and n = 4, this gives “4159”, positions 2 through 5.
Without strings, let P be the integer formed by the 3 followed by N certified digits (P = 3141592 for N = 6). Integer division by 10^(N−i) discards every digit to the right of the target. The remainder modulo 10 gives the target digit. For i = 5, 3141592 div 10 = 314159 and 314159 mod 10 = 9. Either method reads a digit already computed and certified; it does not jump directly to an arbitrary digit of π.
const decimali = risultato.digits.slice(2); const cifra = decimali[i - 1];