Ex 3 - Study Drill

I thought it was interesting to share how some decimal floats or fractions come up imprecise in binary fractions and there is a difference when you add them up and they don’t equal each other.

I keep restarting the book after getting stuck on an exercise further in book and taking a long break from any coding.

from decimal import Decimal
from fractions import Fraction

x = 1 / 10
y = 3 / 10

print("1/10 =", x)
print("True value =",Decimal.from_float(x))

print("3/10 =", y)
print("True value =",Decimal.from_float(y))

print(f"Does adding 1/10 three times equal the value of 3/10?")

print("1/10 + 1/10 + 1/10 =", x + x + x)

a = x + x + x 

print("The true value is =", Decimal.from_float(a))
print("The true value for 3 / 10 is =", Decimal.from_float(y))

print(f"Is variable a {a} equal to variable y {y}?")

print(a == y)