-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_continued_fractions.py
48 lines (36 loc) · 1.28 KB
/
gen_continued_fractions.py
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
#!/usr/bin/env python3
"""gen_continued_fractions.py"""
import math
MAX_TERMS: int = 20
def decode_gencf(form: tuple[int, ...]) -> float:
"""Evaluates a generalized continued fraction of the given form"""
a0, b0, Ai, Bi, Ci, Di, Ei = form
an, bn = a0, b0
hn, kn = 0, 0
b_1, h_1, k_1 = 1, 1, 0
h_2, k_2 = 0, 1
for n in range(1, MAX_TERMS):
hn = an * h_1 + b_1 * h_2
kn = an * k_1 + b_1 * k_2
b_1 = bn
h_1, h_2 = hn, h_1
k_1, k_2 = kn, k_1
an = Di * n + Ei
bn = Ai * n * n + Bi * n + Ci
return hn / kn
def print_rel_error(estimated: float, actual: float) -> None:
print(f"Est. : {estimated}")
print(f"Act. : {actual}")
print(f"Rel. % Err : {(actual - estimated)/actual:.14%}\n")
def main() -> None:
print("Euler's Generalized Continued Fraction for Pi")
x: float = decode_gencf((3, 1, 4, 4, 1, 0, 6))
print_rel_error(x, math.pi)
print("Euler's Generalized Continued Fraction for Pi, GCF #1")
y: float = decode_gencf((3, 1, 8, 0, -7, 8, -1))
print_rel_error(y, math.pi)
print("Euler's Generalized Continued Fraction for Pi, GCF #2")
z: float = decode_gencf((2, 8, 4, 8, 0, 4, 2))
print_rel_error(z, math.pi)
if __name__ == "__main__":
main()