In computer science and numerical analysis, unit in the last place or unit of least precision (ulp) is the spacing between two consecutive floating-point numbers, i.e., the value the least significant digit (rightmost digit) represents if it is 1. It is used as a measure of accuracy in numeric calculations.[1]
b
p
be\le|x|<be+1
emin
\operatorname{ulp}(x)=be
\operatorname{ulp}(x)=
emin-p+1 | |
b |
Another definition, suggested by John Harrison, is slightly different:
\operatorname{ulp}(x)
a
b
a\lex\leb
a ≠ b
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.[4]
Since the 2010s, advances in floating-point mathematics have allowed correctly rounded functions to be almost as fast in average as these earlier, less accurate functions. A correctly rounded function would also be fully reproducible. which theoretically would only produce one incorrect rounding out of 1000 random floating-point inputs.[5]
Let
x
\operatorname{RN}
\operatorname{ulp}(x)\le1
\operatorname{RN}(x+1)>x
\operatorname{RN}(x+1)=x
\operatorname{RN}(x+1)=x+\operatorname{ulp}(x)
x
Here we start with 0 in single precision (binary32) and repeatedly add 1 until the operation does not change the value. Since the significand for a single-precision number contains 24 bits, the first integer that is not exactly representable is 224+1, and this value rounds to 224 in round to nearest, ties to even. Thus the result is equal to 224.
The following example in Java approximates as a floating point value by finding the two double values bracketing
\pi
p0<\pi<p1
// truncate to a double floating pointdouble p0 = π.doubleValue;// -> 3.141592653589793 (hex: 0x1.921fb54442d18p1)
// p0 is smaller than π, so find next number representable as doubledouble p1 = Math.nextUp(p0);// -> 3.1415926535897936 (hex: 0x1.921fb54442d19p1)
Then
\operatorname{ulp}(\pi)
\operatorname{ulp}(\pi)=p1-p0
// same result when using the standard library functiondouble ulpMath = Math.ulp(p0);// -> 4.440892098500626E-16 (hex: 0x1.0p-51)
Another example, in Python, also typed at an interactive prompt, is:
In this case, we start with x = 1
and repeatedly double it until x = x + 1
. Similarly to Example 1, the result is 253 because the double-precision floating-point format uses a 53-bit significand.
The Boost C++ libraries provides the functions boost::math::float_next
, boost::math::float_prior
, boost::math::nextafter
and boost::math::float_advance
to obtain nearby (and distant) floating-point values,[6] and boost::math::float_distance(a, b)
to calculate the floating-point distance between two doubles.[7]
The C language library provides functions to calculate the next floating-point number in some given direction: nextafterf
and nexttowardf
for float
, nextafter
and nexttoward
for double
, nextafterl
and nexttowardl
for long double
, declared in <math.h>
. It also provides the macros FLT_EPSILON
, DBL_EPSILON
, LDBL_EPSILON
, which represent the positive difference between 1.0 and the next greater representable number in the corresponding type (i.e. the ulp of one).[8]
The Java standard library provides the functions and . They were introduced with Java 1.5.
The Swift standard library provides access to the next floating-point number in some given direction via the instance properties nextDown
and nextUp
. It also provides the instance property ulp
and the type property ulpOfOne
(which corresponds to C macros like FLT_EPSILON
[9]) for Swift's floating-point types.[10]