Strange result from comparison
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sachi
am 17 Nov. 2022
Bearbeitet: Stephen23
am 17 Nov. 2022
Sorry for the naive question. Surely there is a trivial answer. Why does this happen? Thanks.
>> 12*10^-3==0.012
ans =
logical
1
>> 12*10^-4==0.0012
ans =
logical
0
Akzeptierte Antwort
Stephen23
am 17 Nov. 2022
Bearbeitet: Stephen23
am 17 Nov. 2022
"Why does this happen?"
Because of the accumulated floating point error. Lets have a look at the values involved, to a higher precision:
fprintf('%.64f\n',12*10^-3,0.012) % these look the same...
fprintf('%.64f\n',12*10^-4,0.0012) % but are these the same?
Always remember that:
- binary floating point arithmetic has some similarities to the algebra and arithmetic that you learned at school, but it is definitely not the same thing.
- the values you see displayed by default in the command window are to a relatively low precision, in general they do not show numbers with their full precision (see NUM2STREXACT).
Learn more about binary floating point numbers:
This is worth reading as well:
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Naive Bayes finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!