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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by