If I type this into octave:
a = 8*10^-5;
b = 8.0e-005;
a==b
I get the answer:
ans = 1
But if I type this into octave:
a = 3*10^-5;
b = 3.0e-005;
a==b
I get this answer
ans = 0
Am I missing something? Is this not the right way to test for equality?
This is a common problem with the way floating point numbers are represented in computer systems.
Many numbers which are ’round’ in decimal become repeating fractions in the binary representation used by the computer, and they have to be truncated at some point, for a very slight loss of precision. If two floating point numbers are arrived at differently, you have to compare them with a little bit of slop, e.g.
abs(a-b) < EPSILONwhereEPSILONis an appropriate constant like 1e-12.