Member-only story
Why (0.302 == 0.302) is False in C++?
A question for everyone to explain issues with floating point comparison — that one needs to be careful about.
The problem above is 5% math, 5% C++ and 90% associated with nuances of floating point computation in major programming languages.
Before going into the theory let’s try this empirically. I wrote a mini program to test this out 10 million times (just to be sure).
You can try running this program here. The output I get is
Counter = 0
You don’t have to go check the math again, as I said, it’s only 5% of a math problem. It’s not even an issue with the specific programming language that I have used in the example above(C++ in this case).
If you try running a similar Python program here, you’d get a similar result.