1.1 + 0.1 == 1.2 returns false
Ältere Kommentare anzeigen
Very strange.
Apparently this is related to the precision of the double type as
1.1 + 0.1 - 1.2 = 2.2204e-16
What can I do to counter this behavior? Thanks!
1 Kommentar
James Tursa
am 31 Jan. 2014
As a learning aid you can use this FEX submission to see the exact numbers involved:
E.g., for your example,
>> num2strexact(1.1)
ans =
1.100000000000000088817841970012523233890533447265625
>> num2strexact(0.1)
ans =
0.1000000000000000055511151231257827021181583404541015625
>> num2strexact(1.1+0.1)
ans =
1.20000000000000017763568394002504646778106689453125
>> num2strexact(1.2)
ans =
1.1999999999999999555910790149937383830547332763671875
Akzeptierte Antwort
Weitere Antworten (2)
Azzi Abdelmalek
am 13 Jan. 2014
1 Stimme
1 Kommentar
N/A
am 30 Jan. 2014
Mischa Kim
am 13 Jan. 2014
Bearbeitet: Mischa Kim
am 13 Jan. 2014
0 Stimmen
MATLAB is a numerical software tool, not an algebraic one. What you are seeing there is the numerical accuracy measure called eps. In other words, this is as accurate as your results will get.
1 Kommentar
N/A
am 30 Jan. 2014
Kategorien
Mehr zu Logical 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!