General question about the precision of computations

1 view (last 30 days)
Hi all,
There is something basic that I don't seem to be getting about how computations are carried out in Matlab. What is the physical limit that is imposed on Matlab as far as the precision of calculations is concerned? If I enter 1-(1-10^(-x)) for different values of x, I get a nice answer for positive whole number until I enter x=17, at which point Matlab returns 0. Could someone explain why Matlab cannot recognize that this is not quite zero, and is there a way to improve the precision of Matlab? And one last question, what does "digits()" do in Matlab, exactly? From the documentation, I thought it could change the working precision, but apparently that's not what it does.
Thank you very much!

Answers (1)

Roger Stafford
Roger Stafford on 24 Apr 2015
First, you should understand that your computer uses binary digits, not decimal digits. If you are using double precision floating point numbers (double,) there are 53 binary bits available to its significand (mantissa.) When you write 10(-17) the computer will use the closest binary fraction to this value that is possible with a 53-bit significand, namely
0.10111000011101111010101000110010001101101010010010111*2^(-56)
where the 2^(-56) exponent means that the binary point is to be shifted left by 56 binary places. If you subtract this number from 1, the exact result would have 109 binary places after the binary point, so it must be rounded to the nearest 53-bit value. As you can see if you play with it, the closest one would be the original exact 1 itself, so the final result of the subtraction would still be that 1 unchanged. When you subtract that result from 1, you will of course get an exact zero. That should explain your puzzling result.
You can read about such numbers and their general properties at the site:
http://www.mathworks.com/help/matlab/matlab_prog/floating-point-numbers.html
  4 Comments
DavidS
DavidS on 24 Apr 2015
I'm guessing if I want to do something like solve a differential equation numerically then I'm stuck with my computer chip precision. Now, is this 53 number a fundamental restriction that ALL computers have?
James Tursa
James Tursa on 24 Apr 2015
Edited: James Tursa on 24 Apr 2015
All computers that use IEEE double precision have this limitation. Computers are not required to use this convention, but a large percentage do these days. E.g., the old VAX computers had a double precision floating point format called D_FLOAT that had more than 53 bits of mantissa, but it did so by reducing the number of bits available for the exponent. So it had more precision than an IEEE double but less range (the range being almost exactly the same as their single precision format F_FLOAT).

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!