필터 지우기
필터 지우기

Comparision double values 135 with 135.0000

조회 수: 31 (최근 30일)
Simon
Simon 2021년 11월 30일
편집: John D'Errico 2021년 11월 30일
In my Matlab-Script there are two large vectors, I want to compare with each other at several indexes in a for loop. In one vector the value 135 is stored as the double-value "135" with a size of 8 Bytes. The other vector stores the same value, with the same size as "135.0000". A comparision with a simple if-statement and the == operator does not succeed.
What I did:
  • using class() to verify the datatyp. Both are double
  • parsing the two values to an integer with int64(). That doesnt make sense in my code
  • using double() to force the same datatype. As documented by Mathworks, that has no effect: If X is already a double precision array, double has no effect.
How do I compare these two double-values?
What is the differnece between these two values, if they are both of the same datatype and the same size?

채택된 답변

Stephen23
Stephen23 2021년 11월 30일
편집: Stephen23 2021년 11월 30일
" A comparision with a simple if-statement and the == operator does not succeed."
Yes, it does succeed: their values are different, and so EQ correctly returns FALSE.
This is very easy to demonstrate:
X = pi
X = 3.1416
Y = pi+eps(pi)
Y = 3.1416
X==Y % EQ operator "succeeds", because the values are NOT the same.
ans = logical
0
"How do I compare these two double-values?"
Using EQ compares the values for exact equivalence, which is not robust if you are working with binary floating point numbers. In that case the robust appraoch is to compare the absolute difference against a tolerance:
tol = 1e-5;
abs(X-Y)<tol % how to compare binary floating point numbers
ans = logical
1
"What is the differnece between these two values, if they are both of the same datatype and the same size?"
Their values.
  댓글 수: 2
Stephen23
Stephen23 2021년 11월 30일
Tip if you are interested in learning about MATLAB:
In the default FORMAT a binary floating point number with zero decimal fraction is displayed without any decimal fraction digits. In contrast, as soon as any trailing decimal fraction digits are non-zero then MATLAB always displays the digits (up to the number specified by the FORMAT).
A = 135 % exactly 135
A = 135
B = 135 + eps(200) % not 135
B = 135.0000
This tells us without even having to do any comparisons that your two values given in your title "Comparision double values 135 with 135.0000" are NOT the same. MATLAB really is trying to help you.
Simon
Simon 2021년 11월 30일
Thanks a lot for that detailed information. It really helped me to learn more about Matlab and how it communicates. Now I understand my problem.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Awais Saeed
Awais Saeed 2021년 11월 30일
I had this issue once. I am just suggesting what helped me. It is possible that the actual value is not 135.0000. If 135.0000 is not comming out to be equal to 135, then it means that it is not equal after some decimal points.
The default format for display in matlab is format short. Use format long to see the difference. Here is an example below:
format short
d1 = 135;
d2 = 135.0000000000001 % note what the result is. It just showed you the clipped version
d2 = 135.0000
isequal(d1,d2) % logic 0 would mean that two values are not equal
ans = logical
0
% Now use format long (this will show you the actual number)
format long
d2
d2 =
1.350000000000001e+02
What I did is to keep just 4 digits after the decimal, remove the rest, and then compare.
  댓글 수: 2
Simon
Simon 2021년 11월 30일
Thank you. That solved my problem.
John D'Errico
John D'Errico 2021년 11월 30일
편집: John D'Errico 2021년 11월 30일
It is not that 135.0000 is POSSIBLY not 135. It is very likely not 135. Those extra zeros are a clue that you should recognize.
x = 135 + 1e-12
x = 135.0000
y = 135
y = 135
x == y
ans = logical
0
Do you see the difference? x and y are not equal, even though they look the same. But those spare zeros are a big clue. If x and y are combined in an array, then you can see spare zeros on x too though.
[x,y]
ans = 1×2
135.0000 135.0000
When I created a vector combining the two numbers and one of them is not an integer, MATLAB decides to display both numbers using the same format. So there are some cases when spare zeros are not of any significance.
Finally, will format long ALWAYS show you the complete, actual number? Well, still not true.
z = 1 + eps
z = 1.0000
format long
z
z =
1.000000000000000
Is z identically 1? No. But still, even format long fails to show that tiny variation in the least significant bit of z. The spare zeros shown are again a suggestion that z was not truly 1.
z == 1
ans = logical
0

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by