tpi - C-written, MPFR-based CLI program for calculating pi, licensed by BSD2-clause
Warning
For the accuracy of calculations, a large MPFR library is used, there may be delays and high processor loads on weak systems.
The program calculates the pi number with high accuracy.
In infinite mode, something like AOT compilation is used, first ~10 million decimal places are calculated, and then they are converted to char[] through the same MPFR.
But if you need more, you can change the len variable and recompile the project with build.sh for yourself by setting a different limit. Be careful, the CPU load will be higher.
Calculations are performed using the Gauss method, which is based on the use of integrals and properties of elliptic integrals. This method, also known as the Gauss-Legandre method, makes it possible to calculate pi with high accuracy using an iterative process.
In python it looks like:
def gauss_pi(iterations):
a = 1.0
b = 1.0 / math.sqrt(2)
t = 1.0 / 4.0
p = 1.0
for _ in range(iterations):
a_next = (a + b) / 2
b = math.sqrt(a * b)
t -= p * (a - a_next) ** 2
a = a_next
p *= 2
return (a + b) ** 2 / (4 * t)I'm not giving an example from my code because the gnu MPFR syntax is unreadable without an armed eye.
