Discussion of Problem 1 from HW 10
The problem lies in the nature of floating point computing.
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. First 4 digits of the hex representation is the sign
and magnitude. 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 0110
>> a+b
ans =
400a666666666667 <------ compare last digit 0111
a+b is actually greater than c by 1 digit in the last place.
Now use long e formatting to show all the decimal digits.
>> format long e
>> 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.
KEY RESULTS:
1) It is never safe to test if two floating point numbers
(numbers with digits after the decimal point) are equal.
2) Just because two numbers display the same does not mean
they are the same number at the memory level.