/* * Returns the signum function of the argument. If the argument is less than * zero, it returns -1.0. If the argument is greater than zero, 1.0 is * returned. If the argument is either positive or negative zero, the * argument is returned as result. * <p> * Special cases: * <ul> * <li>{@code signum(+0.0) = +0.0}</li> * <li>{@code signum(-0.0) = -0.0}</li> * <li>{@code signum(+infinity) = +1.0}</li> * <li>{@code signum(-infinity) = -1.0}</li> * <li>{@code signum(NaN) = NaN}</li> * </ul> * * @param f * the value whose signum has to be computed. * @return the value of the signum function. */ public static float signum(float f) { if (Float.isNaN(f)) { return(Float.NaN); } float sig = f; if (f > 0) { sig = 1.0f; } else if (f < 0) { sig = -1.0f; } return(sig); }
/* * Returns the argument's ulp (unit in the last place). The size of a ulp of * a float value is the positive distance between this value and the float * value next larger in magnitude. For non-NaN {@code x}, * {@code ulp(-x) == ulp(x)}. * <p> * Special cases: * <ul> * <li>{@code ulp(+0.0) = Float.MIN_VALUE}</li> * <li>{@code ulp(-0.0) = Float.MIN_VALUE}</li> * <li>{@code ulp(+infintiy) = infinity}</li> * <li>{@code ulp(-infintiy) = infinity}</li> * <li>{@code ulp(NaN) = NaN}</li> * </ul> * * @param f * the floating-point value to compute ulp of. * @return the size of a ulp of the argument. */ public static float ulp(float f) { // special cases if (Float.isNaN(f)) { return(Float.NaN); } else if (float.IsInfinity(f)) { return(Float.POSITIVE_INFINITY); } else if (f == Float.MAX_VALUE || f == -Float.MAX_VALUE) { return((float)pow(2, 104)); } f = Math.abs(f); return(nextafterf(f, Float.MAX_VALUE) - f); }
/* * Answers the next larger float value to d. * * @param f * the float value to start * @return the next larger float value of d. * * @since 1.6 */ public static float nextUp(float f) { if (Float.isNaN(f)) { return(Float.NaN); } if ((f == Float.POSITIVE_INFINITY)) { return(Float.POSITIVE_INFINITY); } if (0 == f) { return(Float.MIN_VALUE); } else if (0 < f) { return(Float.intBitsToFloat(Float.floatToIntBits(f) + 1)); } else { return(Float.intBitsToFloat(Float.floatToIntBits(f) - 1)); } }