-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi.py
More file actions
52 lines (39 loc) · 1.32 KB
/
pi.py
File metadata and controls
52 lines (39 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from decimal import Decimal, getcontext
import typer
app = typer.Typer(name="π")
@app.command()
def calculate_pi_gauss_legendre(digits: int):
"""
This implements the Gauss-Legendre algorithm to calculate the
value of Pi. Because it is memory intensive, practitioners typically
do not implement this algorithm for actually calculating Pi.
https://en.wikipedia.org/wiki/Gauss%E2%80%93Legendre_algorithm
"""
getcontext().prec = digits + 2
a = Decimal(1) # a_0
b = Decimal(1) / Decimal(2).sqrt() # b_0
t = Decimal(1) / Decimal(4) # t_0
p = Decimal(1) # p_0
for _ in range(digits):
a_next = (a + b) / 2
b = (a * b).sqrt()
t -= p * (a - a_next) ** 2
a = a_next
p *= 2
return (a + b) ** 2 / (4 * t)
@app.command()
def truncate_decimal(value, places: int):
"""Truncates a Decimal value to a specified number of decimal places."""
value_str = f"{value}"
dot_position = value_str.find(".")
return Decimal(
value_str[: dot_position + places + 1]
if dot_position != -1
else value_str
)
@app.command()
def calculate_pi(digits: int):
"""Returns the correct value of Pi up to the given digits."""
return truncate_decimal(calculate_pi_gauss_legendre(digits), digits)
if __name__ == "__main__":
app()