Difference between x*(1e-n1) == x/(1en2)??

1 次查看(过去 30 天)
What ist the difference between x*(1e-n) == x/(1en)?? it seems both results in slightly different results due to numerical errors. I would like to know which result is correct?
E.g. 3*(1e-9) == 3/(1e9)
ans = 0
5*1e-22 == 5/1e22 ==> ans = 0
In contrast
3*1e-7 == 3/1e7 ==> ans = 1
E.g. 1*1e-9 == 1/1e9 ==> ans = 1

采纳的回答

David Goodmanson
David Goodmanson 2019-3-21
编辑:David Goodmanson 2019-3-21
Hi Rob,
The difference comes down to rounding of the very last digit in binary representation. It's interesting to use 'format hex' in this situation, which produces the IEEE 754 representation of a double precision number in 16 hexidecimal digits:
% in 'format long', all three numbers below are 3.000000000000000e-09
format hex
[ 3e-9;
3*1e-9;
3/1e9 ]
ans = 3e29c511dc3a41df
3e29c511dc3a41e0
3e29c511dc3a41df
So the second number is represented differently in the very last bit. Which is 'correct' is not real definitive. One way to see what might be 'correct' is multiplying / dividing the 1e-9 factor to get back to 3;
[ 3e-9;
3*1e-9;
3/1e9 ]*1e9
ans = 4008000000000000
4008000000000001
4008000000000000
[ 3e-9;
3*1e-9;
3/1e9 ]/1e-9
ans = 4008000000000000
4008000000000000
4008000000000000
By this criterion the 3*1e-9 representation is not as 'correct' as the other two, although when dividing by 1e-9 it's just as good as the other two. This example is just a sample of one, though, and could come out differently in other examples.
I think the real message here is not which one of these is better, but rather the old refrain,
don't make exact equality comparisons of floating point numbers unless they are known to be integers.

更多回答(0 个)

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by