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

1 回表示 (過去 30 日間)
Rob Weh
Rob Weh 2019 年 3 月 21 日
コメント済み: Rob Weh 2019 年 3 月 22 日
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.
  1 件のコメント
Rob Weh
Rob Weh 2019 年 3 月 22 日
Great. Thanks for the quick answer!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBartlett についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by