Recent from talks
Nothing was collected or created yet.
Machine epsilon
View on WikipediaMachine epsilon or machine precision is an upper bound on the relative approximation error due to rounding in floating point number systems. 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 and it has the symbols Greek epsilon .
There are two prevailing definitions, denoted here as rounding machine epsilon or the formal definition and interval machine epsilon or mainstream definition.
In the mainstream definition, machine epsilon is independent of rounding method, and is defined simply as the difference between 1 and the next larger floating point number.
In the formal definition, machine epsilon is dependent on the type of rounding used and is also called unit roundoff, which has the symbol bold Roman u.
The two terms can generally be considered to differ by simply a factor of two, with the formal definition yielding an epsilon half the size of the mainstream definition, as summarized in the tables in the next section.
Values for standard hardware arithmetics
[edit]The following table lists machine epsilon values for standard floating-point formats.
| IEEE 754 - 2008 | Common name | C++ data type | Base | Precision | Rounding machine epsilon[a] | Interval machine epsilon[b] |
|---|---|---|---|---|---|---|
| binary16 | half precision | N/A | 2 | 11 (one bit is implicit) | 2−11 ≈ 4.88e-04 | 2−10 ≈ 9.77e-04 |
| binary32 | single precision | float | 2 | 24 (one bit is implicit) | 2−24 ≈ 5.96e-08 | 2−23 ≈ 1.19e-07 |
| binary64 | double precision | double | 2 | 53 (one bit is implicit) | 2−53 ≈ 1.11e-16 | 2−52 ≈ 2.22e-16 |
| extended precision, long double | _float80[1] | 2 | 64 | 2−64 ≈ 5.42e-20 | 2−63 ≈ 1.08e-19 | |
| binary128 | quad(ruple) precision | _float128[1] | 2 | 113 (one bit is implicit) | 2−113 ≈ 9.63e-35 | 2−112 ≈ 1.93e-34 |
| decimal32 | single precision decimal | _Decimal32[2] | 10 | 7 | 5 × 10−7 | 10−6 |
| decimal64 | double precision decimal | _Decimal64[2] | 10 | 16 | 5 × 10−16 | 10−15 |
| decimal128 | quad(ruple) precision decimal | _Decimal128[2] | 10 | 34 | 5 × 10−34 | 10−33 |
- ^ According to formal definition — used by Prof. Demmel, LAPACK and Scilab. It represents the largest relative rounding error in round-to-nearest mode. The rationale is that the rounding error is half the interval upwards to the next representable number in finite-precision. Thus, the relative rounding error for number is . In this context, the largest relative error occurs when , and is equal to , because real numbers in the lower half of the interval 1.0 ~ 1.0+ULP(1) are rounded down to 1.0, and numbers in the upper half of the interval are rounded up to 1.0+ULP(1). Here we use the definition of ULP(1) (Unit in Last Place) as the positive difference between 1.0 (which can be represented exactly in finite-precision) and the next greater number representable in finite-precision.
- ^ According to the mainstream definition — used by Prof. Higham; applied in language constants in Ada, C, C++, Fortran, MATLAB, Mathematica, Octave, Pascal, Python and Rust etc., and defined in textbooks like «Numerical Recipes» by Press et al. It represents the largest relative interval between two nearest numbers in finite-precision, or the largest rounding error in round-by-chop mode. The rationale is that the relative interval for number is where is the distance to upwards the next representable number in finite-precision. In this context, the largest relative interval occurs when , and is the interval between 1.0 (which can be represented exactly in finite-precision) and the next larger representable floating-point number. This interval is equal to ULP(1).
Alternative definitions for epsilon
[edit]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 two terms differ by simply a factor of two. The more-widely used term (referred to as the mainstream definition in this article), is used in most modern programming languages and is simply defined as machine epsilon is the difference between 1 and the next larger floating point number. The formal definition can generally be considered to yield an epsilon half the size of the mainstream definition, although its definition does vary depending on the form of rounding used.
The two terms are described at length in the next two subsections.
Formal definition (Rounding machine epsilon)
[edit]The formal definition for machine epsilon is the one used by Prof. James Demmel in lecture scripts,[3] the LAPACK linear algebra package,[4] numerics research papers[5] and some scientific computing software.[6] Most numerical analysts use the words machine epsilon and unit roundoff interchangeably with this meaning, which is explored in depth throughout this subsection.
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, , and by the precision , i.e. the number of radix digits of the significand (including any leading implicit bit). All the numbers with the same exponent, , have the spacing, . The spacing changes at the numbers that are perfect powers of ; the spacing on the side of larger magnitude is 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 . 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 . 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 where is between and . All these numbers round to with relative error . The maximum occurs when is at the upper end of its range. The in the denominator is negligible compared to the numerator, so it is left off for expediency, and just is taken as machine epsilon. As has been shown here, the relative error is worst for numbers that round to , 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, , and an adjacent normalised number is .[7]
Arithmetic model
[edit]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) , are floating-point numbers, (2) is an arithmetic operation on floating-point numbers such as addition or multiplication, and (3) 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 in absolute magnitude is at most 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.
Mainstream definition (Interval machine epsilon)
[edit]This alternative definition is significantly more widespread: machine epsilon is the difference between 1 and the next larger floating point number. This definition is used in language constants in Ada, C, C++, Fortran, MATLAB, Mathematica, Octave, Pascal, Python and Rust etc., and defined in textbooks like «Numerical Recipes» by Press et al.
By this definition, ε equals the value of the unit in the last place relative to 1, i.e. (where b is the base of the floating point system and p is the precision) and the unit roundoff is u = ε / 2, assuming round-to-nearest mode, and u = ε, assuming round-by-chop.
The prevalence of this definition is rooted in its use in the ISO C Standard for constants relating to floating-point types[8][9] and corresponding constants in other programming languages.[10][11][12] It is also widely used in scientific computing software[13][14][15] and in the numerics and computing literature.[16][17][18][19]
How to determine machine epsilon
[edit]Where standard libraries do not provide precomputed values (as <float.h> does with FLT_EPSILON, DBL_EPSILON and LDBL_EPSILON for C and <limits> does with std::numeric_limits<T>::epsilon() 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 interval 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 accuracy directly supported by the processor (or coprocessor), not some 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 the binary representation of 32 bit floats). They also have the property that , and (where is the aforementioned integer reinterpretation of ). 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);
Example in Python:
def machineEpsilon(func=float):
machine_epsilon = func(1)
while func(1) + machine_epsilon != func(1):
machine_epsilon_last = machine_epsilon
machine_epsilon = func(machine_epsilon) / func(2)
return machine_epsilon_last
64-bit doubles give 2.220446e-16, which is 2−52 as expected.
Approximation
[edit]The following simple algorithm can be used to approximate[clarification needed] the machine epsilon, to within a factor of two of its true value, using a linear search.
epsilon = 1.0;
while (1.0 + 0.5 * epsilon) ≠ 1.0:
epsilon = 0.5 * epsilon
The machine epsilon, can also simply be calculated as two to the negative power of the number of bits used for the mantissa.
Relationship to absolute relative error
[edit]If is the machine representation of a number then the absolute relative error in the representation is [20]
Proof
[edit]The following proof is limited to positive numbers and machine representations using round-by-chop.
If is a positive number we want to represent, it will be between a machine number below and a machine number above .
If , where is the number of bits used for the magnitude of the significand, then:
Since the representation of will be either or ,
Although this proof is limited to positive numbers and round-by-chop, the same method can be used to prove the inequality in relation to negative numbers and round-to-nearest machine representations.
See also
[edit]- Floating point, general discussion of accuracy issues in floating point arithmetic
- Unit in the last place (ULP)
Notes and references
[edit]- ^ a b Floating Types - Using the GNU Compiler Collection (GCC)
- ^ a b c Decimal Float - Using the GNU Compiler Collection (GCC)
- ^ "Basic Issues in Floating Point Arithmetic and Error Analysis". 21 Oct 1999. Retrieved 11 Apr 2013.
- ^ "LAPACK Users' Guide Third Edition". 22 August 1999. Retrieved 9 March 2012.
- ^ "David Goldberg: What Every Computer Scientist Should Know About Floating-Point Arithmetic, ACM Computing Surveys, Vol 23, No 1, March 1991" (PDF). Archived from the original (PDF) on 25 March 2016. Retrieved 11 Apr 2013.
- ^ "Scilab documentation - number_properties - determine floating-point parameters". Retrieved 11 Apr 2013.
- ^ "Basic Issues in Floating Point Arithmetic and Error Analysis". University of California, Berkeley. 21 October 1999. Retrieved 11 June 2022.
The distance between 1 and the next larger floating point number is 2*macheps.
- ^ Jones, Derek M. (2009). The New C Standard - An Economic and Cultural Commentary (PDF). p. 377.
- ^ "float.h reference at cplusplus.com". Retrieved 11 Apr 2013.
- ^ "std::numeric_limits reference at cplusplus.com". Retrieved 11 Apr 2013.
- ^ "Python documentation - System-specific parameters and functions". Retrieved 11 Apr 2013.
- ^ Extended Pascal ISO 10206:1990 (Technical report).
The value of epsreal shall be the result of subtracting 1.0 from the smallest value of real-type that is greater than 1.0.
- ^ "Mathematica documentation: $MachineEpsilon". Retrieved 11 Apr 2013.
- ^ "Matlab documentation - eps - Floating-point relative accuracy". Archived from the original on 2013-08-07. Retrieved 11 Apr 2013.
- ^ "Octave documentation - eps function". Retrieved 11 Apr 2013.
- ^ Higham, Nicholas (2002). Accuracy and Stability of Numerical Algorithms (2 ed). SIAM. pp. 27–28.
- ^ Quarteroni, Alfio; Sacco, Riccardo; Saleri, Fausto (2000). Numerical Mathematics (PDF). Springer. p. 49. ISBN 0-387-98959-5. Archived from the original (PDF) on 2017-11-14. Retrieved 2013-04-11.
- ^ Press, William H.; Teukolsky, Saul A.; Vetterling, William T.; Flannery, Brian P. Numerical Recipes. p. 890.
- ^ Engeln-Müllges, Gisela; Reutter, Fritz (1996). Numerik-Algorithmen. p. 6. ISBN 3-18-401539-4.
- ^ "Machine Epsilon Value for IEEE Double Precision Standard Alternative Proof Using Relative Error". 12 October 2020. Retrieved 5 May 2022.
- Anderson, E.; LAPACK Users' Guide, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, PA, third edition, 1999.
- Cody, William J.; MACHAR: A Soubroutine to Dynamically Determine Machine Parameters, ACM Transactions on Mathematical Software, Vol. 14(4), 1988, 303–311.
- Besset, Didier H.; Object-Oriented Implementation of Numerical Methods, Morgan & Kaufmann, San Francisco, CA, 2000.
- Demmel, James W., Applied Numerical Linear Algebra, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, PA, 1997.
- Higham, Nicholas J.; Accuracy and Stability of Numerical Algorithms, Society for Industrial and Applied Mathematics (SIAM), Philadelphia, PA, second edition, 2002.
- Press, William H.; Teukolsky, Saul A.; Vetterling, William T.; and Flannery, Brian P.; Numerical Recipes in Fortran 77, 2nd ed., Chap. 20.2, pp. 881–886
- Forsythe, George E.; Malcolm, Michael A.; Moler, Cleve B.; "Computer Methods for Mathematical Computations", Prentice-Hall, ISBN 0-13-165332-6, 1977
External links
[edit]- MACHAR, a routine (in C and Fortran) to "dynamically compute machine constants" (ACM algorithm 722)
- Diagnosing floating point calculations precision, Implementation of MACHAR in Component Pascal and Oberon based on the Fortran 77 version of MACHAR published in Numerical Recipes (Press et al., 1992).
Machine epsilon
View on GrokipediaFloating-Point Arithmetic Basics
Binary Floating-Point Representation
Binary floating-point numbers represent real numbers in computer systems using a fixed number of bits divided into three components: a sign bit, an exponent field, and a significand (also known as the mantissa).[4] The sign bit, which is a single bit, indicates whether the number is positive (0) or negative (1).[4] The exponent field consists of several bits that encode the scale of the number as a power of 2, while the significand field stores the fractional part of the number in binary form.[4] In normalized binary floating-point representation, as defined by the IEEE 754 standard, the value of a finite non-zero number is given by the formula where is the sign bit (0 or 1), is the significand interpreted as a value between 1 (inclusive) and 2 (exclusive) with an implied leading 1 followed by the stored fraction bits, is the stored biased exponent, and bias is a constant specific to the format (e.g., 127 for single precision).[4][5] This normalization ensures that the significand always starts with a leading 1 in binary, maximizing precision by avoiding leading zeros.[4] The unit in the last place (ulp) refers to the spacing or gap between two consecutive representable floating-point numbers in the same binade (range between powers of 2), equivalent to the value of the least significant bit in the significand for a given exponent.[6] It provides a measure of the precision limits inherent in the representation, as the ulp varies with the magnitude of the number: larger exponents result in larger ulps, reflecting coarser spacing for bigger values.[6] For example, the number 1.0 in single-precision binary floating-point (32 bits total) is represented with sign bit 0, biased exponent 127 (actual exponent 0), and significand fraction bits all 0 (implied leading 1, so ).[4] In binary, this is0 01111111 00000000000000000000000, or in hexadecimal 0x3F800000.[4] The ulp at 1.0 is , the distance to the next representable number 1 + ulp.[6]
Rounding in Arithmetic Operations
In floating-point arithmetic, basic operations such as addition, subtraction, multiplication, and division are performed by first computing an exact intermediate result, which is then rounded to the nearest representable value in the floating-point format to fit within the available precision.[7] This rounding step is necessary because most real numbers and operation outcomes cannot be exactly represented due to the finite number of bits allocated for the significand.[8] The IEEE 754 standard mandates correct rounding for these operations, ensuring the computed result is as close as possible to the mathematically exact value under the selected mode.[7] The standard specifies five rounding modes to control how inexact results are approximated, with round-to-nearest (ties to even) as the default for most implementations.[5] In round-to-nearest, the exact result is rounded to the closest representable floating-point number; if it lies exactly midway between two candidates, the tie is broken by rounding to the one with an even least significant bit in the significand.[8] Alternative directed modes include round toward zero (truncation, which discards excess magnitude), round toward positive infinity (rounding up for positive excesses), and round toward negative infinity (rounding down for negative excesses); a fifth mode, round to nearest with ties away from zero, was added in the 2019 revision for specific use cases like statistical computations.[5] These modes allow programmers to influence error behavior, such as in interval arithmetic or when preserving sign in financial calculations.[7] Rounding introduces an error when the exact result falls between two consecutive representable numbers, known as the unit in the last place (ulp), which is the spacing between those numbers at the given magnitude.[7] In round-to-nearest mode, this rounding error is bounded by 0.5 ulp of the result, meaning the computed value deviates from the exact value by at most half the local spacing between representable points.[7] The ulp itself varies with the exponent, being larger for bigger numbers since the significand's fixed bit length scales the precision dynamically across magnitudes.[8] A clear illustration of rounding error occurs in addition, where a small value added to a larger one may be effectively ignored if it falls below the resolution threshold. For instance, adding a tiny positive number ε to 1.0 yields exactly 1.0 + ε, but if ε is smaller than 0.5 ulp at 1.0, the sum rounds back to 1.0 in round-to-nearest mode, losing the small contribution entirely.[7] This effect arises because the binary floating-point representation around 1.0 has a fixed ulp determined by the significand's precision, as covered in the structure of binary floating-point numbers.[7] Such losses highlight the limitations of finite precision and can propagate in chained operations, emphasizing the need for awareness in numerical algorithms. The unit roundoff quantifies the maximum relative rounding error possible in these operations, providing a bound on how much the computed result can deviate from the exact one relative to the result's scale.[7] In the context of round-to-nearest, this relative error is at most 0.5 ulp normalized by the result's magnitude, serving as a fundamental measure of the arithmetic's faithfulness to real-number mathematics.[7]Core Definitions
Rounding Machine Epsilon
The rounding machine epsilon, also known as the unit roundoff and denoted , represents the maximum relative rounding error introduced when representing a real number in a floating-point system or performing arithmetic operations that round to the nearest representable value.[9] In binary floating-point arithmetic, it is formally defined as , where is the precision, corresponding to the number of bits in the significand (including the implicit leading bit). This is half the machine epsilon .[9] This definition arises within the standard model of floating-point arithmetic, which assumes that every operation rounds the exact result to the nearest representable number, with ties broken in a specified manner (such as round-to-nearest-even). Under this model, the computed result of an operation satisfies , where , bounding the relative error.[9] The value of can be derived directly from the unit in the last place (ulp) at 1, which is ; thus, . In binary floating-point representation, the representable numbers near 1 have a spacing of , so the maximum rounding error is half this spacing relative to the number.[9] The concept of rounding machine epsilon (unit roundoff) originated in early numerical analysis literature for bounding errors in computational processes, notably in Wilkinson's seminal work on rounding error analysis.[10] Note that terminology varies; while some sources use "rounding machine epsilon" for , the more common term is "unit roundoff," with "machine epsilon" referring to the full spacing .Interval Machine Epsilon
The interval machine epsilon, denoted as , is defined as the smallest positive floating-point number such that the floating-point representation , where denotes the floating-point mapping function.[11] This value captures the smallest distinguishable addition to 1 in the floating-point system, independent of the specific rounding mode, and is also known as the standard machine epsilon.[12] In binary floating-point formats with a -bit mantissa (including the implicit leading 1), the interval machine epsilon equals the unit in the last place (ulp) at 1.0, expressed as .[13] This spacing arises because representable numbers near 1 are separated by increments of , reflecting the granularity of the mantissa in the normalized binary representation.[14] It differs from the rounding machine epsilon (unit roundoff ), which bounds the maximum relative error in rounding operations and equals half the interval machine epsilon in round-to-nearest mode ().[12] The interval variant emphasizes the representable gap for detection purposes, while the rounding variant focuses on error propagation in computations.[15] Note that "machine epsilon" typically refers to this interval value in most literature and programming languages (e.g., DBL_EPSILON in C). One practical method to approximate the interval machine epsilon empirically involves iteratively halving an initial value until the addition to 1 becomes indistinguishable:eps = 1.0
while (1.0 + eps != 1.0):
eps = eps / 2.0
eps = eps * 2.0 // Adjust to the last distinguishable value
eps = 1.0
while (1.0 + eps != 1.0):
eps = eps / 2.0
eps = eps * 2.0 // Adjust to the last distinguishable value
Computation Techniques
Theoretical Derivation
In binary floating-point arithmetic, a normalized number is represented as , where is the fractional part consisting of bits, is the exponent, and the leading bit is implicitly 1, making the significand have bits in total.[7][5] For numbers in the interval , the exponent , and the significand ranges from (which is 1) to . The representable values are thus for integer . The spacing between consecutive representable numbers in this binade, known as the unit in the last place (ulp), is therefore .[7] The interval machine epsilon, denoted , is defined as this ulp value at 1, representing the smallest positive floating-point number such that in the arithmetic. Thus, .[7][13] This derivation assumes an IEEE 754-like model with binary base (), normalized representations (no denormals, which affect subnormal numbers far from 1), and no overflow or underflow near 1.0.[5][7] The formula holds specifically for binary floating-point but generalizes to for base- systems with -digit significands, such as decimal formats where the spacing differs accordingly.[7]Practical Approximation
A practical method for approximating machine epsilon in software involves an iterative algorithm that empirically determines the smallest positive floating-point number such that in the given arithmetic environment. This approach begins with an initial value of and repeatedly halves it until the condition holds true in floating-point evaluation, at which point is doubled to recover the boundary value.[7] The algorithm can be expressed in pseudocode as follows:eps = 1.0
while (1.0 + eps > 1.0):
eps = eps / 2.0
eps = eps * 2.0
eps = 1.0
while (1.0 + eps > 1.0):
eps = eps / 2.0
eps = eps * 2.0
Standard Values and Formats
IEEE 754 Single and Double Precision
The IEEE 754 standard for binary floating-point arithmetic, initially published in 1985 and subsequently revised in 2008 and 2019, specifies formats that dominate modern computing hardware, including x86 and ARM processor architectures.[18][19][5][20] These formats ensure consistent representation and operations across systems, with single and double precision being the most prevalent for general-purpose computations. In IEEE 754 single precision (binary32), the 32-bit format allocates 1 bit for the sign, 8 bits for the biased exponent, and 23 bits for the significand, yielding an effective precision of 24 bits due to the implicit leading 1 in normalized numbers. The machine epsilon, defined as the smallest positive value such that in floating-point representation, is .[21] This value represents the unit in the last place (ulp) at 1.0, bounding the spacing between representable numbers in the vicinity of 1. Double precision (binary64) employs 64 bits: 1 sign bit, 11 exponent bits, and 52 significand bits, for an effective precision of 53 bits including the implicit 1. Here, the machine epsilon is , again corresponding to the ulp at 1.0.[21] The unit roundoff , which quantifies the maximum relative rounding error in round-to-nearest mode, is half the machine epsilon, or . The following table summarizes these key parameters for comparison:| Format | Total Bits | Precision | Machine Epsilon | Unit Roundoff | ULP at 1.0 |
|---|---|---|---|---|---|
| Single (binary32) | 32 | 24 | |||
| Double (binary64) | 64 | 53 |
