Why does 7*(0.1) - 0.7 = 1.1102e-16 whereas 9*(0.1)-0.9 =0 in MATLAB?

9 views (last 30 days)
Why does
7*(0.1)-0.7 = 1.1102e-16,
3*(0.1)-0.3 = 5.55112e-17,
6*(0.1)-0.6 = 1.1102e-16
whereas
9*(0.1)-0.9 = 0,
2*(0.1)-0.2 = 0,
4*(0.1)-0.4 = 0 in MATLAB?

Answers (3)

Stephen23
Stephen23 on 5 Dec 2018
Edited: Stephen23 on 5 Dec 2018
Those values that you are computing with cannot be exactly represented using binary floating point numbers. What you see printed in the command window is the closest representation to 5 or 16 significant digits, depending on your current format setting. To see the "real" value download James Tursa's FEX submission:
Use James Tursa's num2strexact and you will see that none of those values really have the exact value 0.1, or whatever value you use. All you are looking at is a representation of those floating point numbers displayed in the command window, to the precision defined by your format setting. Just because you see 0.1 displayed tells you nothing about the "real" floating point number's value.
Note that you can change how the value is displayed by changing the format.
You need to learn about the limits of floating point numbers. Start by reading these:
This is worth reading as well:

madhan ravi
madhan ravi on 5 Dec 2018

the cyclist
the cyclist on 5 Dec 2018
See the section "Accuracy of Floating-Point Data" in this documentation page.
Some of those combinations are represented by the same floating point value, and some are not.

Categories

Find more on Numeric Types 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!