Discussion of Problem 2 from HW 10
Consider an easier to examine situation:
a = 1.1;
b = 2.2;
c = 3.3;
if (c == a+b)
disp('c = a+b')
else
disp('c not = a+b')
end
>> test
c not = a+b
--------------------------------------------------------------
The problem lies in the nature of floating point computing and
that fact that numbers can only be represented using a fintite
number of digits.
Neither of the 3 numbers can be represented exactly in binary
https://www.rapidtables.com/convert/number/decimal-to-binary.html
1.1 = 1.000110011001100....
2.2 = 10.00110011001100.....
3.3 = 11.0100110011001....
A storage error of 1 digit in the last place can occur
for each of these numbers.
A rounding error can occur when a and b are added.
Turn on hex formatting in MATLAB to see how numbers are stored in
memory. The first 4 digits of the hex representation is the sign
and magnitude. The last 12 digits are the mantissa (digits that
make up the number itself).
>> format hex
>> a = 1.1
a =
3ff199999999999a
>> b = 2.2
b =
400199999999999a
>> c = 3.3
c =
400a666666666666 <------ compare last digit 6 = 0110
>> a+b
ans =
400a666666666667 <------ compare last digit 7 = 0111
a+b is actually greater than c by 1 digit in the last place.
Now use longe formatting to show all the decimal digits.
>> format longe
>> c
c =
3.300000000000000e+00
>> a+b
ans =
3.300000000000000e+00
Note that even though c and a+b are different at the binary level,
they display the same when converted to decimal. This is because there
are many binary numbers between any two consecutive decimal numbers
(i.e., the conversion between binary and decimal is not unique when you
can only store a finite number of digits).
KEY RESULTS:
1) It is never safe to test if two floating point numbers
(numbers that can have digits after the decimal point) are equal.
2) Just because two numbers display the same in decimal does not mean
they are the same number stored in memory.