Clear Filters
Clear Filters

How come 10^-N does not equal 1e-N for some N ?

22 views (last 30 days)
I've noticed by chance that
isequal(10^-5,1e-5) == 0
and
(10^-5) - (1e-5) == -1.694065894508601e-21
I checked other numbers, such as N = 3,4,6 and found that in these cases
isequal(10^-N,1e-N) == 1
so, I ran this code:
for idx = 1:100
if ~isequal(10^-idx,eval(['1e-',num2str(idx)]))
idx
end
end
and got back the numbers: 5, 11, 17, 20, 21, 24, 29, 32, 39, 63, 67, 76, 86, 88, 98
Any idea what's going on?
I'm working with MATLAB R2014a on a Windows 10 platform
Thanks

Accepted Answer

Adam
Adam on 13 Jul 2016
Edited: Adam on 13 Jul 2016
floating point maths is not exact. There are numerous places on the web where you can read up about this. It isn't specific to Matlab, but as soon as you start doing maths on floating point numbers you start to get very small inaccuracies.
-1.694065894508601e-21
is a tiny number which is generally considered to be less than the tolerance required for two numbers to be considered equal since, with floating point maths equality almost always must be done within a tolerance.
Never rely on absolute equality tests for floating point data unless you know for sure where the values have come from and that they are, for example, integer valued - e.g. everything in Matlab is a double by default, even when you assign integer values so you can obviously do equality amongst these, just not the results of floating point maths.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!