Machine epsilon

Machine epsilon gives an upper bound on the relative error due to rounding in floating point arithmetic. This value characterizes computer arithmetic in the field of numerical analysis, and by extension in the subject of computational science. The quantity is also called macheps or unit roundoff, and it has the symbols Greek epsilon formula_1 or bold Roman u, respectively.
Values for standard hardware floating point arithmetics.
The following values of machine epsilon apply to standard floating point formats:
Formal definition.
"Rounding" is a procedure for choosing the representation of a real number in a floating point number system. For a number system and a rounding procedure, machine epsilon is the maximum relative error of the chosen rounding procedure.
Some background is needed to determine a value from this definition. A floating point number system is characterized by a radix which is also called the base, formula_2, and by the precision formula_3, i.e. the number of radix formula_2 digits of the significand (including any leading implicit bit). All the numbers with the same exponent, formula_5, have the spacing, formula_6. The spacing changes at the numbers that are perfect powers of formula_2; the spacing on the side of larger magnitude is formula_2 times larger than the spacing on the side of smaller magnitude.
Since machine epsilon is a bound for relative error, it suffices to consider numbers with exponent formula_9. It also suffices to consider positive numbers. For the usual round-to-nearest kind of rounding, the absolute rounding error is at most half the spacing, or formula_10. This value is the biggest possible numerator for the relative error. The denominator in the relative error is the number being rounded, which should be as small as possible to make the relative error large. The worst relative error therefore happens when rounding is applied to numbers of the form formula_11 where formula_12 is between formula_13 and formula_10. All these numbers round to formula_15 with relative error formula_16. The maximum occurs when formula_12 is at the upper end of its range. The formula_11 in the denominator is negligible compared to the numerator, so it is left off for expediency, and just formula_10 is taken as machine epsilon. As has been shown here, the relative error is worst for numbers that round to formula_15, so machine epsilon also is called "unit roundoff" meaning roughly "the maximum error that can occur when rounding to the unit value".
Thus, the maximum spacing between a normalised floating point number, formula_21, and an adjacent normalised number is formula_22 x formula_23.
Arithmetic model.
Numerical analysis uses machine epsilon to study the effects of rounding error. The actual errors of machine arithmetic are far too complicated to be studied directly, so instead, the following simple model is used. The IEEE arithmetic standard says all floating point operations are done as if it were possible to perform the infinite-precision operation, and then, the result is rounded to a floating point number. Suppose (1) formula_21, formula_25 are floating point numbers, (2) formula_26 is an arithmetic operation on floating point numbers such as addition or multiplication, and (3) formula_27 is the infinite precision operation. According to the standard, the computer calculates:
By the meaning of machine epsilon, the relative error of the rounding is at most machine epsilon in magnitude, so:
where formula_30 in absolute magnitude is at most formula_1 or u. The books by Demmel and Higham in the references can be consulted to see how this model is used to analyze the errors of, say, Gaussian elimination.
Variant definitions.
The IEEE standard does not define the terms "machine epsilon" and "unit roundoff", so differing definitions of these terms are in use, which can cause some confusion.
The definition given here for "machine epsilon" is the one used by Prof. James Demmel in lecture scripts, the "LAPACK" linear algebra package, numerics research papers and some scientific computing software. Most numerical analysts use the words "machine epsilon" and "unit roundoff" interchangeably with this meaning.
The following different definition is much more widespread outside academia: "Machine epsilon is defined as the difference between 1 and the next larger floating point number." By this definition, formula_1 equals the value of the unit in the last place relative to 1, i.e. formula_33, and the unit roundoff is uformula_34, assuming round-to-nearest mode. The prevalence of this definition is rooted in its use in the ISO C Standard for constants relating to floating-point types and corresponding constants in other programming languages. It is also widely used in scientific computing software, and in the numerics and computing literature.
How to determine machine epsilon.
Where standard libraries do not provide precomputed values (as <float.h> does with codice_1, codice_2 and codice_3 for C and <limits> does with codice_4 in C++), the best way to determine machine epsilon is to refer to the table, above, and use the appropriate power formula. Computing machine epsilon is often given as a textbook exercise. The following examples compute machine epsilon in the sense of the spacing of the floating point numbers at 1 rather than in the sense of the unit roundoff.
Note that results depend on the particular floating-point format used, such as float, double, long double, or similar as supported by the programming language, the compiler, and the runtime library for the actual platform.
Some formats supported by the processor might not be supported by the chosen compiler and operating system. Other formats might be emulated by the runtime library, including arbitrary-precision arithmetic available in some languages and libraries.
In a strict sense the term "machine epsilon" means the 1+eps accuracy directly supported by the processor (or coprocessor), not some 1+eps accuracy supported by a specific compiler for a specific operating system, unless it's known to use the best format.
IEEE 754 floating-point formats have the property that, when reinterpreted as a two's complement integer of the same width, they monotonically increase over positive values and monotonically decrease over negative values (see ). They also have the property that 0 < |"f"("x")| < ∞, and |"f"("x"+1) − "f"("x")| ≥ |"f"("x") − "f"("x"−1)| (where "f"("x") is the aforementioned integer reinterpretation of "x"). In languages that allow type punning and always use IEEE 754-1985, we can exploit this to compute a machine epsilon in constant time. For example, in C:
typedef union {
 long long i64;
 double d64;
} dbl_64;
double machine_eps (double value)
 dbl_64 s;
 s.d64 = value;
 s.i64++;
 return s.d64 - value;
This will give a result of the same sign as value. If a positive result is always desired, the return statement of machine_eps can be replaced with:
 return (s.i64 < 0 ? value - s.d64 : s.d64 - value);
64-bit doubles give 2.220446e-16, which is 2−52 as expected.
Approximation.
The following simple algorithm can be used to approximate the machine epsilon, to within a factor of two (one order of magnitude) of its true value, using a linear search.
 epsilon = 1.0;
 while (1.0 + 0.5 * epsilon) ≠ 1.0:
 epsilon = 0.5 * epsilon