In radix b, if x has exponent E, then ULP(x) =machine epsilon · bE,[1] but alternative definitions exist in the numerics and computing literature for ULP, exponent andmachine epsilon, and they may give different equalities.[citation needed]
Another definition, suggested by John Harrison, is slightly different: ULP(x) is the distance between the two closeststraddling floating-point numbers a and b (i.e., those witha ≤ x ≤ b and a ≠ b), assuming that the exponent range is not upper-bounded.[2][3] These definitions differ only at signed powers of the radix.[citation needed]
The IEEE 754 specification—followed by all modern floating-point hardware—requires that the result of anelementary arithmetic operation (addition, subtraction, multiplication, division, andsquare 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). Reputablenumeric 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 theTable-Maker's Dilemma.[4]
Letx be a nonnegative floating-point number and assume that the active roundingattribute is round to nearest, ties to even, denoted RN. If ULP(x) is less thanor equal to 1, then RN(x + 1) > x. Otherwise, RN(x + 1) = x or RN(x + 1) = x+ ULP(x), depending on the value of theleast significant digit and the exponent of x. This is demonstrated in thefollowing 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
Herewe start with 0 in 32-bit single-precision and repeatedly add 1 until theoperation is idempotent. The result is equal to 224 since the significand for asingle-precision number in this example contains 24 bits.
Anotherexample, 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
Inthis case, we start with x = 1 and repeatedly double it until x = x + 1. Theresult is 253, because the double-precision floating-point format uses a 53-bitsignificand.
点击打开链接