MATLAB: Isn’t 95.77-95.68 = exactly 0.09

double precisionfaqieee754

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

Best Answer

>> 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