A problem with floating point precision!
1 view (last 30 days)
I'm very new to MATLAB, so please forgive this very basic question!
I have run up against an issue with floating-point precision and hope that someone can help!
Clearly the second answer is what I was expecting, and the first answer is not giving the same result because of floating point precision (I assume).
Any advice as to how to overcome this (since my code needs to multiply numbers by powers of 10) is much appreciated.
Rik on 30 Apr 2020
Don't compare to 0, but compare the absolute difference to a tolerance.
If you want to be safe you can do 2*eps.
There are several functions that will allow you to use a tolerance: ismembertol and uniquetol are two of the more usefull ones.