-
Notifications
You must be signed in to change notification settings - Fork 37
/
specialnumbers.py
90 lines (73 loc) · 2.5 KB
/
specialnumbers.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from fractions import Fraction
from decimal import Decimal, getcontext
from math import fsum
def demonstrate_imprecision():
# Floating point can represent exactly only terminating series of
# powers of two. All three numbers are unrepresentable.
if 0.1 + 0.2 == 0.3:
print("Mathematics works as it should, no problemo!")
else:
print("Basic arithmetic has gone topsy turvy!")
# Decimal to the rescue!
if Decimal(0.1) + Decimal(0.2) == Decimal(0.3):
print("Decimal works just fine!")
else:
print("The Decimals! They do nothing!")
# Always create a Decimal object from a string, otherwise you get
# an exact representation of a number that is already inexact.
if Decimal("0.1") + Decimal("0.2") == Decimal("0.3"):
print("All is well in the world again!")
# Precision of floating point does not work out well when adding
# numbers of vastly different magnitudes.
print(sum([3e30, 1, -3e30])) # 0.0
# Better.
print(fsum([3e30, 1, -3e30])) # 1.0
def decimal_precision_demo():
a = Decimal("0.2")
b = Decimal("0.3")
print(a * b) # 0.06
a = Decimal("0.2000")
b = Decimal("0.3000")
print(a * b) # 0.06000000
a = Decimal("0.12345")
b = Decimal("0.98765")
print(a * b) # 0.1219253925
getcontext().prec = 5
a = Decimal("0.12345")
b = Decimal("0.98765")
print(a * b) # 0.12193
# Unlike machine floats, Decimal values cannot under- or overflow
a = Decimal("0.1") ** 1000
print(a)
a = Decimal("2") ** 10000
print(a)
def infinity_and_nan_demo():
ninf = float('-inf')
pinf = float('inf')
print(f"Plus infinity minus 7 equals {(pinf - 7)}.")
print(f"Plus infinity plus infinity equals {(pinf + pinf)}.")
print(f"Minus infinity times minus 4 equals {(ninf * -4)}.")
nan = pinf + ninf
print(f"Infinity minus infinity equals {nan}.")
print(f"nan equals nan is {nan == nan}.")
def harmonic(n):
total = Fraction(0)
for i in range(1, n+1):
total += Fraction(1, i)
return total
def estimate_pi(n):
total = Fraction(0)
for i in range(1, n+1):
total += Fraction(1, i*i)
return total * 6
def __demo():
demonstrate_imprecision()
decimal_precision_demo()
infinity_and_nan_demo()
pi = estimate_pi(10000)
getcontext().prec = 10
print("Estimating pi: ", end="")
print((Decimal(str(pi.numerator)).sqrt() /
Decimal(str(pi.denominator)).sqrt()))
if __name__ == "__main__":
__demo()