Why isn't 95.77-95.68 = exactly 0.09?

1 view (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 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

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!