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 doubleprecision number 3.14 to single, it is rounded by
double(single(3.14) - 3.14)
ans =
1.0490e-07
The amount that 3.14 is rounded is less than
eps(single(3.14))
ans =
single
2.3842e-07
Avoiding Common Problems with Floating-Point Arithmetic
Almost all operations in MATLAB are performed in double-precision arithmetic conforming to the
IEEE standard 754. Because computers only represent numbers to a finite precision (double precision
calls for 52 mantissa bits), computations sometimes yield mathematically nonintuitive results. It is
important to note that these results are not bugs in MATLAB.
Use the following examples to help you identify these cases:
Example 1 — Round-Off or What You Get Is Not What You Expect
The decimal number 4/3 is not exactly representable as a binary fraction. For this reason, the
following calculation does not give zero, but rather reveals the quantity eps.
e = 1 - 3*(4/3 - 1)
e =
2.2204e-16
Similarly, 0.1 is not exactly representable as a binary number. Thus, you get the following
nonintuitive behavior:
a = 0.0;
for i = 1:10
a = a + 0.1;
end
a == 1
ans =
logical
0
Note that the order of operations can matter in the computation:
b = 1e-16 + 1 - 1e-16;
c = 1e-16 - 1e-16 + 1;
b == c
ans =
Floating-Point Numbers
4-11
rounded when you convert it from double to single. For example, when you convert the doubleprecision number 3.14 to single, it is rounded by
double(single(3.14) - 3.14)
ans =
1.0490e-07
The amount that 3.14 is rounded is less than
eps(single(3.14))
ans =
single
2.3842e-07
Avoiding Common Problems with Floating-Point Arithmetic
Almost all operations in MATLAB are performed in double-precision arithmetic conforming to the
IEEE standard 754. Because computers only represent numbers to a finite precision (double precision
calls for 52 mantissa bits), computations sometimes yield mathematically nonintuitive results. It is
important to note that these results are not bugs in MATLAB.
Use the following examples to help you identify these cases:
Example 1 — Round-Off or What You Get Is Not What You Expect
The decimal number 4/3 is not exactly representable as a binary fraction. For this reason, the
following calculation does not give zero, but rather reveals the quantity eps.
e = 1 - 3*(4/3 - 1)
e =
2.2204e-16
Similarly, 0.1 is not exactly representable as a binary number. Thus, you get the following
nonintuitive behavior:
a = 0.0;
for i = 1:10
a = a + 0.1;
end
a == 1
ans =
logical
0
Note that the order of operations can matter in the computation:
b = 1e-16 + 1 - 1e-16;
c = 1e-16 - 1e-16 + 1;
b == c
ans =
Floating-Point Numbers
4-11
