One definition is: In radixb with precision p, if be ≤ |x| < be+1, then ULP = bmax−p+1. Another definition, suggested by John Harrison, is slightly different: ULP is the distance between the two closest straddlingfloating-point numbers a and 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 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. 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.
Examples
Example 1
Let x be a positive floating-point number and assume that the active rounding attribute is round to nearest, ties to even, denoted RN. If ULP is less than or equal to 1, then. Otherwise, or, 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 0 :: Float 1.6777216e7 > it-1 1.6777215e7 > it+1 1.6777216e7
Here we start with 0 in single precision 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.
Example 2
The following example in Java approximates Pi| as a floating point value by finding the two double values bracketing : // π with 20 decimal digits BigDecimal π = new BigDecimal; // truncate to a double floating point double p0 = π.doubleValue; // -> 3.141592653589793 // p0 is smaller than π, so find next number representable as double double p1 = Math.nextUp; // -> 3.1415926535897936
Then is determined as // ulp is the difference between p1 and p0 BigDecimal ulp = new BigDecimal.subtract; // -> 4.44089209850062616169452667236328125E-16 // // same result when using the standard library function double ulpMath = Math.ulp; // -> 4.440892098500626E-16
Example 3
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 and repeatedly double it until. Similarly to Example 1, the result is 253 because the double-precision floating-point format uses a 53-bit significand.
Language support
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 floating-point values, and boost::math::float_distance 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: nextafterf and nexttowardf for float, nextafter and nexttoward for double, nextafterl and nexttowardl for long double, declared in . 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. 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 for Swift's floating-point types.