MATLAB: Weird rounding error

errormathematicsrounding

Hello. I'm getting the following odd "error" which I hope is just a display thing but really doesn't seem to be. I'm using R2016a on redhat linux.
>> format long
>> 177.5 – 170.6
ans = 6.900000000000006
>> 177.5 – 170.6 -6.9
ans = 5.329070518200751e-15
>> 1.5 – 0.6
ans = 0.900000000000000
>> 10.5 – 9.6
ans = 0.900000000000000
>> 100.5 – 99.6
ans = 0.900000000000006
>> (100.5 – 99.6) > 0.9
ans = 1

Best Answer

Hi Jason, this is just Computer Science 101. Since computers have only so many bits to describe floating point numbers, there are going to be differences like this (not errors), down at the last-bit level. Good that you had enough interest in the details to recognize this effect. See 'help eps'.
In IEEE 754 standard which Matlab uses, 64 bit floating point numbers have 53 bits allotted for the significant figure part. It's worthwhile to go to Wikipedia for a summary of IEEE 754.