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.
The most common definition is: In radix with precision , if , then where is the minimal exponent of the normal numbers. In particular, for normal numbers, and for subnormals.
Another definition, suggested by John Harrison, is slightly different: is the distance between the two closest straddling floating-point numbers and (i.e., satisfying and ), 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.
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.
Let be a positive floating-point number and assume that the active rounding mode is round to nearest, ties to even, denoted . If , then . Otherwise, or , depending on the value of the least significant digit and the exponent of . This is demonstrated in the following Haskell code typed at an interactive prompt:
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 2<sup>24</sup>+1, and this value rounds to 2<sup>24</sup> in round to nearest, ties to even. Thus the result is equal to 2<sup>24</sup>.
The following example in Java approximates as a floating-point value by finding the two double values bracketing : .
Then is determined as .
Another example, in Python, also typed at an interactive prompt, is:
In this case, we start with <code>x = 1</code> and repeatedly double it until <code>x = x + 1</code>. Similarly to Example 1, the result is 2<sup>53</sup> because the double-precision floating-point format uses a 53-bit significand.
The Boost C++ libraries provides the functions <code>boost::math::float_next</code>, <code>boost::math::float_prior</code>, <code>boost::math::nextafter</code> and <code>boost::math::float_advance</code> to obtain nearby (and distant) floating-point values, and <code>boost::math::float_distance(a, b)</code> to calculate the floating-point distance between two doubles.
The C language library provides functions to calculate the next floating-point number in some given direction: <code>nextafterf</code> and <code>nexttowardf</code> for <code>float</code>, <code>nextafter</code> and <code>nexttoward</code> for <code>double</code>, <code>nextafterl</code> and <code>nexttowardl</code> for <code>long double</code>, declared in <code><math.h></code>. It also provides the macros <code>FLT_EPSILON</code>, <code>DBL_EPSILON</code>, <code>LDBL_EPSILON</code>, which represent the positive difference between 1.0 and the next greater representable number in the corresponding type (i.e. the ulp of one).
The Go standard library provides the functions <code>math.Nextafter</code> (for 64 bit floats) and <code>math.Nextafter32</code> (for 32 bit floats) both of which return the next representable floating-point value towards another provided floating-point value.
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 <code>nextDown</code> and <code>nextUp</code>. It also provides the instance property <code>ulp</code> and the type property <code>ulpOfOne</code> (which corresponds to C macros like <code>FLT_EPSILON</code>) for Swift's floating-point types.