简体   繁体   中英

a == b, exponential notation. Octave weirdness?

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 result of loss of precision during computations involving floating point numbers, for example in Java 3 * Math.pow(10, -5) would give you '2.9999999999999997E-5' which no longer matches b in your second example. This is linked to how floating point values are represented and evolve during the evaluation of an expression. For more elaborate perspectives, you can google 'loss of precision' suffixing that with the context / environment you are using to evaluate these expressions. Hope this helps.

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, eg abs(ab) < EPSILON where EPSILON is an appropriate constant like 1e-12.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM