Why isn't 95.77-95.68 = exactly 0.09?

1 vue (au cours des 30 derniers jours)
Mike D.
Mike D. le 7 Mar 2018
I guess I expected 95.77-95.68 to be exactly 0.09, just as I expected 3*(1/3) to be 0.9999999999, but I was wrong on both! More importantly, how can I get it be rounded off to 0.09? I could use round() or mod(diff*100)/100

Réponse acceptée

Walter Roberson
Walter Roberson le 7 Mar 2018
>> fprintf('%.999g\n', 95.77, 95.68, 95.77-95.68)
95.7699999999999960209606797434389591217041015625
95.68000000000000682121026329696178436279296875
0.0899999999999891997504164464771747589111328125
>> fprintf('%.999g\n', 1/3)
0.333333333333333314829616256247390992939472198486328125
>> isAlways(1-sym('0.333333333333333314829616256247390992939472198486328125')*3 < eps)
ans =
logical
1

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by