Example 1 Let x be a positive floating-point number and assume that the active rounding mode is
round to nearest, ties to even, denoted \operatorname{RN}. If \operatorname{ulp}(x) \le 1, then \operatorname{RN} (x + 1) > x. Otherwise, \operatorname{RN} (x + 1) = x or \operatorname{RN} (x + 1) = x + \operatorname{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
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.
Example 2 The following example in
Java approximates Pi| as a floating-point value by finding the two double values bracketing \pi: p_0 . // π 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 \operatorname{ulp}(\pi) is determined as \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)
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 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. ==Language support==