MATLAB Answers

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

2 views (last 30 days)
Rob Weh
Rob Weh on 21 Mar 2019
Commented: Rob Weh on 22 Mar 2019
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


Sign in to comment.

Accepted Answer

David Goodmanson
David Goodmanson on 21 Mar 2019
Edited: David Goodmanson on 21 Mar 2019
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/1e9 ]
ans = 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/1e9 ]*1e9
ans = 4008000000000000
[ 3e-9;
3/1e9 ]/1e-9
ans = 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 Comment

Rob Weh
Rob Weh on 22 Mar 2019
Great. Thanks for the quick answer!

Sign in to comment.

More Answers (0)




Translated by