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]
The most common definition is: In radix b {\displaystyle b} with precision p {\displaystyle p} , if b e ≤ ≤ --> | x | < b e + 1 {\displaystyle b^{e}\leq |x|<b^{e+1}} , then ulp --> ( x ) = b max { e , e min } − − --> p + 1 {\displaystyle \operatorname {ulp} (x)=b^{\max\{e,\,e_{\min }\}-p+1}} ,[2] where e min {\displaystyle e_{\min }} is the minimal exponent of the normal numbers. In particular, ulp --> ( x ) = b e − − --> p + 1 {\displaystyle \operatorname {ulp} (x)=b^{e-p+1}} for normal numbers, and ulp --> ( x ) = b e min − − --> p + 1 {\displaystyle \operatorname {ulp} (x)=b^{e_{\min }-p+1}} for subnormals.
Another definition, suggested by John Harrison, is slightly different: ulp --> ( x ) {\displaystyle \operatorname {ulp} (x)} is the distance between the two closest straddling floating-point numbers a {\displaystyle a} and b {\displaystyle b} (i.e., satisfying a ≤ ≤ --> x ≤ ≤ --> b {\displaystyle a\leq x\leq b} and a ≠ ≠ --> b {\displaystyle a\neq b} ), assuming that the exponent range is not upper-bounded.[3][4] These definitions differ only at signed powers of the radix.[2]
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.[5]
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. An earlier, intermediate milestone was the 0.501 ulp functions,[clarification needed] which theoretically would only produce one incorrect rounding out of 1000 random floating-point inputs.[6]
Let x {\displaystyle x} be a positive floating-point number and assume that the active rounding mode is round to nearest, ties to even, denoted RN {\displaystyle \operatorname {RN} } . If ulp --> ( x ) ≤ ≤ --> 1 {\displaystyle \operatorname {ulp} (x)\leq 1} , then RN --> ( x + 1 ) > x {\displaystyle \operatorname {RN} (x+1)>x} . Otherwise, RN --> ( x + 1 ) = x {\displaystyle \operatorname {RN} (x+1)=x} or RN --> ( x + 1 ) = x + ulp --> ( x ) {\displaystyle \operatorname {RN} (x+1)=x+\operatorname {ulp} (x)} , depending on the value of the least significant digit and the exponent of x {\displaystyle x} . This is demonstrated in the following Haskell code typed at an interactive prompt:[citation needed]
> until (\x -> x == x+1) (+1) 0 :: Float 1.6777216e7 > it-1 1.6777215e7 > it+1 1.6777216e7
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 π π --> {\displaystyle \pi } : p 0 < π π --> < p 1 {\displaystyle p_{0}<\pi <p_{1}} .
// π with 20 decimal digits BigDecimal π = new BigDecimal("3.14159265358979323846"); // truncate to a double floating point double p0 = π.doubleValue(); // -> 3.141592653589793 (hex: 0x1.921fb54442d18p1) // p0 is smaller than π, so find next number representable as double double p1 = Math.nextUp(p0); // -> 3.1415926535897936 (hex: 0x1.921fb54442d19p1)
Then ulp --> ( π π --> ) {\displaystyle \operatorname {ulp} (\pi )} is determined as ulp --> ( π π --> ) = p 1 − − --> p 0 {\displaystyle \operatorname {ulp} (\pi )=p_{1}-p_{0}} .
// ulp(π) is the difference between p1 and p0 BigDecimal ulp = new BigDecimal(p1).subtract(new BigDecimal(p0)); // -> 4.44089209850062616169452667236328125E-16 // (this is precisely 2**(-51)) // same result when using the standard library function double ulpMath = Math.ulp(p0); // -> 4.440892098500626E-16 (hex: 0x1.0p-51)
Another example, in Python, also typed at an interactive prompt, is:
>>> 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
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.
x = 1
x = x + 1
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,[7] and boost::math::float_distance(a, b) to calculate the floating-point distance between two doubles.[8]
boost::math::float_next
boost::math::float_prior
boost::math::nextafter
boost::math::float_advance
boost::math::float_distance(a, b)
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).[9]
nextafterf
nexttowardf
float
nextafter
nexttoward
double
nextafterl
nexttowardl
long double
<math.h>
FLT_EPSILON
DBL_EPSILON
LDBL_EPSILON
The Java standard library provides the functions Math.ulp(double) and Math.ulp(float). They were introduced with Java 1.5.
Math.ulp(double)
Math.ulp(float)
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[10]) for Swift's floating-point types.[11]
nextDown
nextUp
ulp
ulpOfOne
Lokasi Pengunjung: 3.143.22.194