java.lang.Math—is “within 1 ULP” exclusive or inclusive?
Asked Answered
C

1

6

java.lang.Math docs say for many functions, such as Math.pow:

The computed result must be within 1 ulp of the exact result.

But I haven't been able to find what does this mean precisely. Is it exclusive or inclusive? In other words, if the exact result can be represented with a double, will the returned value contain exact result or it may still be off by 1 ULP?

For example, can we rely on Math.pow(3.0, 2.0) == 9.0? I know using equality comparison is almost always a bad idea for doubles, so I am mostly asking out of curiosity and to be able to point people to their mistakes (or reassure them) when they do something like that.

Chiron answered 26/2, 2016 at 18:2 Comment(0)
M
7

fyi,

The quality of implementation specifications concern two properties, accuracy of the returned result and monotonicity of the method. Accuracy of the floating-point Math methods is measured in terms of ulps, units in the last place. For a given floating-point format, an ulp of a specific real number value is the distance between the two floating-point values bracketing that numerical value. When discussing the accuracy of a method as a whole rather than at a specific argument, the number of ulps cited is for the worst-case error at any argument. If a method always has an error less than 0.5 ulps, the method always returns the floating-point number nearest the exact result; such a method is correctly rounded. A correctly rounded method is generally the best a floating-point approximation can be; however, it is impractical for many floating-point methods to be correctly rounded.

Instead, for the Math class, a larger error bound of 1 or 2 ulps is allowed for certain methods. Informally, with a 1 ulp error bound, when the exact result is a representable number, the exact result should be returned as the computed result; otherwise, either of the two floating-point values which bracket the exact result may be returned.

For exact results large in magnitude, one of the endpoints of the bracket may be infinite. Besides accuracy at individual arguments, maintaining proper relations between the method at different arguments is also important. Therefore, most methods with more than 0.5 ulp errors are required to be semi-monotonic: whenever the mathematical function is non-decreasing, so is the floating-point approximation, likewise, whenever the mathematical function is non-increasing, so is the floating-point approximation. Not all approximations that have 1 ulp accuracy will automatically meet the monotonicity requirements.

Source

Meyer answered 26/2, 2016 at 18:35 Comment(1)
Oh, I have no idea how I missed that part when reading it. Anyway, that answers the question perfectly. I've edited it a little bit.Chiron

© 2022 - 2024 — McMap. All rights reserved.