MATLAB: Two equal numbers are not equal

comparisonequalfloating point

Hello everyone,
I had some difficulties comparing some numbers and the problem is simply summarized below :
So can someone tell me why and how this can happen? And how to avoid this kind of problem in the future if possible.
Thank you.

Best Answer

You should never test equality between floating point numbers. There are any number of places you could search online to see the reasons for this. Many floating point numbers can not be represented to 100% accuracy and maths done on them will often introduce small inaccuracies so you should always test equality within a tolerance, not directly. The difference may only be 1e-16 and so basically equivalent to 0 for all sensible usage, but it will still fail an == test.