Why does comparing identical doubles result in a logical 0?
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (2)
Wayne King
am 19 Mär. 2013
Bearbeitet: Wayne King
am 19 Mär. 2013
1 Stimme
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()
1 Kommentar
Joerg
am 19 Mär. 2013
Shashank Prasanna
am 19 Mär. 2013
Bearbeitet: Shashank Prasanna
am 19 Mär. 2013
If you want to see how they are different, try:
>> format hex
>> v(2),2000
change display format back to default
>> format
Kategorien
Mehr zu Other Formats finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!