Comparing Floating Point Numbers
Oct 21, 2021 21:27 · 151 words · 1 minute read
Floating point numbers are a chore to work with. And how computers work with them can be more complex. As an example take this example in Python:
print(0.3 * 3 + 0.1)
We get:
0.9999999999999999
This is caused by rounding errors during calculations. Instead of getting a simple “1”, we get: “0.9999999999999999”. Thus, when programming, it’s dangerous to directly compare floating numbers, like say:
print((0.3 * 3 + 0.1) == 1)
Which outputs:
False
Instead, a more plausible way of checking for equality with 2 floating point numbers would be to assume that the 2 floating point numbers are equal if the difference between them is less than some number, like say ϵ.
ϵ = 1e-9
print(abs((0.3 * 3 + 0.1) - 1) < ϵ)
which now correctly outputs
True
Also, a bit off-topic, but it’s worth pointing out that symbols like θ, β, ζ etc are valid names for variables.