Why does comparing identical doubles result in a logical 0?
이전 댓글 표시
Hi there,
I have the problem that the comparison of doubles which I have exported from another software (COMSOL Multiphysics) with doubles of identical value result in a logical 0. How can I fix this? I've documented the problem above by copying the contents of my Command Window below:
==============
v =
1.0e+03 *
2.5000
2.0000
0.6000
>> v(2)
ans =
2.0000e+03
>> v(2)==2000
ans =
0
>> w=double(2000);
>> v(2)==w
ans =
0
>> whos v
Name Size Bytes Class Attributes
v 3x1 24 double
>> whos w
Name Size Bytes Class Attributes
w 1x1 8 double
>> v(2)==2.0000e+03
ans =
0
>>
==============
Any help would be highly appreciated.
Thanks a lot in advance, Joerg
채택된 답변
추가 답변 (2개)
Wayne King
2013년 3월 19일
편집: Wayne King
2013년 3월 19일
1 개 추천
This is the well-known (and often treated in this forum) problem of floating-point precision. The point is that they are not identical.
You can use a tolerance to compare the numbers like abs(x-y)<lambda
where lambda is some very small number.
See the help for eps()
Shashank Prasanna
2013년 3월 19일
편집: Shashank Prasanna
2013년 3월 19일
If you want to see how they are different, try:
>> format hex
>> v(2),2000
change display format back to default
>> format
카테고리
도움말 센터 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!