Unit in the last place
|This article needs additional citations for verification. (March 2015)|
In computer science and numerical analysis, unit in the last place or unit of least precision (ULP) is the spacing between floating-point numbers, i.e., the value the least significant digit represents if it is 1. It is used as a measure of accuracy in numeric calculations.
In radix b, if x has exponent E, then ULP(x) = machine epsilon · bE, but alternative definitions exist in the numerics and computing literature for ULP, exponent and machine epsilon, and they may give different equalities.
Another definition, suggested by John Harrison, is slightly different: ULP(x) is the distance between the two closest straddling floating-point numbers a and b (i.e., those with a ≤ x ≤ b and a ≠ b), assuming that the exponent range is not upper-bounded. These definitions differ only at signed powers of the radix.
The IEEE 754 specification—followed by all modern floating-point hardware—requires that the result of an elementary arithmetic operation (addition, subtraction, multiplication, division, and square root since 1985, and FMA since 2008) be correctly rounded, which implies that in rounding to nearest, the rounded result is within 0.5 ULP of the mathematically exact result, using John Harrison's definition; conversely, this property implies that the distance between the rounded result and the mathematically exact result is minimized (but for the halfway cases, it is satisfied by two consecutive floating-point numbers). Reputable numeric libraries compute the basic transcendental functions to between 0.5 and about 1 ULP. Only a few libraries compute them within 0.5 ULP, this problem being complex due to the Table-Maker's Dilemma.
Let x be a nonnegative floating-point number and assume that the active rounding attribute is round to nearest, ties to even, denoted RN. If ULP(x) is less than or equal to 1, then RN(x + 1) > x. Otherwise, RN(x + 1) = x or RN(x + 1) = x + ULP(x), depending on the value of the least significant digit and the exponent of x. This is demonstrated in the following Haskell code typed at an interactive prompt:
> until (\x -> x == x+1) (+1) 0 :: Float 1.6777216e7 > it-1 1.6777215e7 > it+1 1.6777216e7
Here we start with 0 in 32-bit single-precision and repeatedly add 1 until the operation is idempotent. The result is equal to 224 since the significand for a single-precision number in this example contains 24 bits.
>>> x = 1.0 >>> p = 0 >>> while x != x + 1: ... x = x * 2 ... p = p + 1 ... >>> x 9007199254740992.0 >>> p 53 >>> x + 2 + 1 9007199254740996.0
The C language library provides functions to calculate the next floating-point number in some given direction:
long double, declared in
The Boost C++ Libraries offer boost::math::float_next, boost::math::float_prior, boost::math::nextafter and boost::math::float_advance functions to obtain nearby (and distant) floating-point values,  and boost::math::float_distance(a, b) to calculate the floating-point distance between two doubles. 
- Higham, Nicholas (2002). Accuracy and Stability of Numerical Algorithms (2 ed). SIAM.
- Harrison, John. "A Machine-Checked Theory of Floating Point Arithmetic". Retrieved 2013-07-17.
- Muller, Jean-Michel (2005-11). "On the definition of ulp(x)". INRIA Technical Report 5504. ACM Transactions on Mathematical Software, Vol. V, No. N, November 2005. Retrieved in 2012-03 from http://ljk.imag.fr/membres/Carine.Lucas/TPScilab/JMMuller/ulp-toms.pdf.
- Kahan, William. "A Logarithm Too Clever by Half". Retrieved 2008-11-14.
- ISO/IEC 9899:1999 specification (PDF). p. 237, §18.104.22.168 The nextafter functions and §22.214.171.124 The nexttoward functions.
- Boost float_advance.
- Boost float_distance.
- Goldberg, David (1991-03). "Rounding Error" in "What Every Computer Scientist Should Know About Floating-Point Arithmetic". Computing Surveys, ACM, March 1991. Retrieved from http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html#689.