Mathematics |
Because there are only finitely many double-precision numbers, you cannot represent all numbers in double-precision storage. On any computer, there is a small gap between each double-precision number and the next larger double-precision number. You can determine the size of this gap, which limits the precision of your results, using the eps
function. For example, to find the distance between 5
and the next larger double-precision number, enter
This tells you that there are no double-precision numbers between 5 and 5 + eps(5)
. If a double-precision computation returns the answer 5, the result is only accurate to within eps(5)
.
The value of eps(x)
depends on x
: as x
gets larger, so does eps(x)
. For example,
so that eps(50)
is larger than eps(5)
.
If you enter eps
with no input argument, MATLAB returns the value of eps(1)
, the distance from 1
to the next larger double-precision number.
Similarly, there are gaps between any two single-precision numbers. If x
has type single
, eps(x)
returns the distance between x
and the next larger single-precision number.
For example,
Note that this result is larger than eps(5)
. Because there are fewer single-precision numbers than double-precision numbers, the gaps between the single-precision numbers are larger than the gaps between double-precision numbers. This means that results in single-precision arithmetic are less precise than in double-precision.
For a number x
of type double
, eps(single(x))
gives you an upper bound for the amount that x
is rounded when you convert it from double
to single
. For example, when you convert the double-precision number 3.14
to single
, it is rounded by
The amount that 3.14
is rounded is less than
Single-Precision Arithmetic | Example -- Writing M-Files for Different Data Types |
© 1994-2005 The MathWorks, Inc.