If you see the number in MATLAB displayyed as 1.000 in the command window, then almost always you don't have the number 1. Instead, it LOOKS like 1, but has been rounded to that value for display purposes.
x = [1, 1.000001 0.999999]
Are they all 1? Exactly? No. In fact, the last two have been rounded to 1. I told MATLAB to display only a few digits. And since that is the default, it is what most people seem to use. And even though the first element of that vector is indeed exactly 1, MATLAB still shows the zeros.
MATLAB does clearly know that one of them is exactly 1, as you can see. And if I show only that first element, MATLAB tries to display the number as 1, as opposed to 1 with some zeros appended.
For x(2) it still needs to round off, so it displays those spare zeros. Of course, we can change the default format to not round to only a few digits. Now we will see longer versions those numbers, and now we might realize that only the first element exactly 1.
Remember this clue in MATLAB: If a number shows extra zeros beyond the end of the non-zero digits, that number probably is not stored exactly as what you see.
But since 0.95 is not representable exactly in binary, 0.95 is stored as more like the number:
sprintf('%0.17f',y)
ans = '0.94999999999999996'
0 Comments
Sign in to comment.