MATH-480 - Fix ulp(Infinity) to return Infinity rather than NaN

MATH-478 - Adds ulp(float)

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_X@1060911 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2011-01-19 19:17:59 +00:00
parent 839130d338
commit 5ecbfedb2a
1 changed files with 15 additions and 1 deletions

View File

@ -3270,11 +3270,25 @@ public class FastMath {
* @param x number from which ulp is requested
* @return ulp(x)
*/
public static double ulp(double x) {
if (Double.isInfinite(x)) {
return Double.POSITIVE_INFINITY;
}
return abs(x - Double.longBitsToDouble(Double.doubleToLongBits(x) ^ 1));
}
/**
* Compute least significant bit (Unit in Last Position) for a number.
* @param x number from which ulp is requested
* @return ulp(x)
*/
public static float ulp(float x) {
if (Float.isInfinite(x)) {
return Float.POSITIVE_INFINITY;
}
return abs(x - Float.intBitsToFloat(Float.floatToIntBits(x) ^ 1));
}
/**
* Get the next machine representable number after a number, moving
* in the direction of another number.