/** * Returns the floating-point value adjacent to {@code f} in * the direction of negative infinity. This method is * semantically equivalent to {@code nextAfter(f, * Float.NEGATIVE_INFINITY)}; however, a * {@code nextDown} implementation may run faster than its * equivalent {@code nextAfter} call. * * <p>Special Cases: * <ul> * <li> If the argument is NaN, the result is NaN. * * <li> If the argument is negative infinity, the result is * negative infinity. * * <li> If the argument is zero, the result is * {@code -Float.MIN_VALUE} * * </ul> * * @param f starting floating-point value * @return The adjacent floating-point value closer to negative * infinity. * @author Joseph D. Darcy */ public static double nextDown(float f) { if( isNaN(f) || f == Float.NEGATIVE_INFINITY) return f; else { if (f == 0.0f) return -Float.MIN_VALUE; else return Float.intBitsToFloat(Float.floatToRawIntBits(f) + ((f > 0.0f)?-1:+1)); } } /** * Returns the first floating-point argument with the sign of the * second floating-point argument. For this method, a NaN * {@code sign} argument is always treated as if it were * positive. * * @param magnitude the parameter providing the magnitude of the result * @param sign the parameter providing the sign of the result * @return a value with the magnitude of {@code magnitude} * and the sign of {@code sign}. * @author Joseph D. Darcy * @since 1.5 */ public static double copySign(double magnitude, double sign) { return rawCopySign(magnitude, (isNaN(sign)?1.0d:sign)); } /** * Returns the first floating-point argument with the sign of the * second floating-point argument. For this method, a NaN * {@code sign} argument is always treated as if it were * positive. * * @param magnitude the parameter providing the magnitude of the result * @param sign the parameter providing the sign of the result * @return a value with the magnitude of {@code magnitude} * and the sign of {@code sign}. * @author Joseph D. Darcy */ public static float copySign(float magnitude, float sign) { return rawCopySign(magnitude, (isNaN(sign)?1.0f:sign)); } /** * Returns the size of an ulp of the argument. An ulp of a * {@code double} value is the positive distance between this * floating-point value and the {@code double} value next * larger in magnitude. Note that for non-NaN <i>x</i>, * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. * * <p>Special Cases: * <ul> * <li> If the argument is NaN, then the result is NaN. * <li> If the argument is positive or negative infinity, then the * result is positive infinity. * <li> If the argument is positive or negative zero, then the result is * {@code Double.MIN_VALUE}. * <li> If the argument is ±{@code Double.MAX_VALUE}, then * the result is equal to 2<sup>971</sup>. * </ul> * * @param d the floating-point value whose ulp is to be returned * @return the size of an ulp of the argument * @author Joseph D. Darcy * @since 1.5 */ public static double ulp(double d) { int exp = getExponent(d); switch(exp) { case DoubleConsts.MAX_EXPONENT+1: // NaN or infinity return Math.abs(d); case DoubleConsts.MIN_EXPONENT-1: // zero or subnormal return Double.MIN_VALUE; default: assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT; // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x)) exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1); if (exp >= DoubleConsts.MIN_EXPONENT) { return powerOfTwoD(exp); } else { // return a subnormal result; left shift integer // representation of Double.MIN_VALUE appropriate // number of positions return Double.longBitsToDouble(1L << (exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) )); } } } /** * Returns the size of an ulp of the argument. An ulp of a * {@code float} value is the positive distance between this * floating-point value and the {@code float} value next * larger in magnitude. Note that for non-NaN <i>x</i>, * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. * * <p>Special Cases: * <ul> * <li> If the argument is NaN, then the result is NaN. * <li> If the argument is positive or negative infinity, then the * result is positive infinity. * <li> If the argument is positive or negative zero, then the result is * {@code Float.MIN_VALUE}. * <li> If the argument is ±{@code Float.MAX_VALUE}, then * the result is equal to 2<sup>104</sup>. * </ul> * * @param f the floating-point value whose ulp is to be returned * @return the size of an ulp of the argument * @author Joseph D. Darcy * @since 1.5 */ public static float ulp(float f) { int exp = getExponent(f); switch(exp) { case FloatConsts.MAX_EXPONENT+1: // NaN or infinity return Math.abs(f); case FloatConsts.MIN_EXPONENT-1: // zero or subnormal return FloatConsts.MIN_VALUE; default: assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT; // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x)) exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1); if (exp >= FloatConsts.MIN_EXPONENT) { return powerOfTwoF(exp); } else { // return a subnormal result; left shift integer // representation of FloatConsts.MIN_VALUE appropriate // number of positions return Float.intBitsToFloat(1 << (exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) )); } } } /** * Returns the signum function of the argument; zero if the argument * is zero, 1.0 if the argument is greater than zero, -1.0 if the * argument is less than zero. * * <p>Special Cases: * <ul> * <li> If the argument is NaN, then the result is NaN. * <li> If the argument is positive zero or negative zero, then the * result is the same as the argument. * </ul> * * @param d the floating-point value whose signum is to be returned * @return the signum function of the argument * @author Joseph D. Darcy * @since 1.5 */ public static double signum(double d) { return (d == 0.0 || isNaN(d))?d:copySign(1.0, d); } /** * Returns the signum function of the argument; zero if the argument * is zero, 1.0f if the argument is greater than zero, -1.0f if the * argument is less than zero. * * <p>Special Cases: * <ul> * <li> If the argument is NaN, then the result is NaN. * <li> If the argument is positive zero or negative zero, then the * result is the same as the argument. * </ul> * * @param f the floating-point value whose signum is to be returned * @return the signum function of the argument * @author Joseph D. Darcy * @since 1.5 */ public static float signum(float f) { return (f == 0.0f || isNaN(f))?f:copySign(1.0f, f); } }Lines 1001–1215 of 1215. Previous lines
/** * Returns the floating-point value adjacent to {@code f} in * the direction of negative infinity. This method is * semantically equivalent to {@code nextAfter(f, * Float.NEGATIVE_INFINITY)}; however, a * {@code nextDown} implementation may run faster than its * equivalent {@code nextAfter} call. * * <p>Special Cases: * <ul> * <li> If the argument is NaN, the result is NaN. * * <li> If the argument is negative infinity, the result is * negative infinity. * * <li> If the argument is zero, the result is * {@code -Float.MIN_VALUE} * * </ul> * * @param f starting floating-point value * @return The adjacent floating-point value closer to negative * infinity. * @author Joseph D. Darcy */ public static double nextDown(float f) { if( isNaN(f) || f == Float.NEGATIVE_INFINITY) return f; else { if (f == 0.0f) return -Float.MIN_VALUE; else return Float.intBitsToFloat(Float.floatToRawIntBits(f) + ((f > 0.0f)?-1:+1)); } } /** * Returns the first floating-point argument with the sign of the * second floating-point argument. For this method, a NaN * {@code sign} argument is always treated as if it were * positive. * * @param magnitude the parameter providing the magnitude of the result * @param sign the parameter providing the sign of the result * @return a value with the magnitude of {@code magnitude} * and the sign of {@code sign}. * @author Joseph D. Darcy * @since 1.5 */ public static double copySign(double magnitude, double sign) { return rawCopySign(magnitude, (isNaN(sign)?1.0d:sign)); } /** * Returns the first floating-point argument with the sign of the * second floating-point argument. For this method, a NaN * {@code sign} argument is always treated as if it were * positive. * * @param magnitude the parameter providing the magnitude of the result * @param sign the parameter providing the sign of the result * @return a value with the magnitude of {@code magnitude} * and the sign of {@code sign}. * @author Joseph D. Darcy */ public static float copySign(float magnitude, float sign) { return rawCopySign(magnitude, (isNaN(sign)?1.0f:sign)); } /** * Returns the size of an ulp of the argument. An ulp of a * {@code double} value is the positive distance between this * floating-point value and the {@code double} value next * larger in magnitude. Note that for non-NaN <i>x</i>, * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. * * <p>Special Cases: * <ul> * <li> If the argument is NaN, then the result is NaN. * <li> If the argument is positive or negative infinity, then the * result is positive infinity. * <li> If the argument is positive or negative zero, then the result is * {@code Double.MIN_VALUE}. * <li> If the argument is ±{@code Double.MAX_VALUE}, then * the result is equal to 2<sup>971</sup>. * </ul> * * @param d the floating-point value whose ulp is to be returned * @return the size of an ulp of the argument * @author Joseph D. Darcy * @since 1.5 */ public static double ulp(double d) { int exp = getExponent(d); switch(exp) { case DoubleConsts.MAX_EXPONENT+1: // NaN or infinity return Math.abs(d); case DoubleConsts.MIN_EXPONENT-1: // zero or subnormal return Double.MIN_VALUE; default: assert exp <= DoubleConsts.MAX_EXPONENT && exp >= DoubleConsts.MIN_EXPONENT; // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x)) exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1); if (exp >= DoubleConsts.MIN_EXPONENT) { return powerOfTwoD(exp); } else { // return a subnormal result; left shift integer // representation of Double.MIN_VALUE appropriate // number of positions return Double.longBitsToDouble(1L << (exp - (DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) )); } } } /** * Returns the size of an ulp of the argument. An ulp of a * {@code float} value is the positive distance between this * floating-point value and the {@code float} value next * larger in magnitude. Note that for non-NaN <i>x</i>, * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. * * <p>Special Cases: * <ul> * <li> If the argument is NaN, then the result is NaN. * <li> If the argument is positive or negative infinity, then the * result is positive infinity. * <li> If the argument is positive or negative zero, then the result is * {@code Float.MIN_VALUE}. * <li> If the argument is ±{@code Float.MAX_VALUE}, then * the result is equal to 2<sup>104</sup>. * </ul> * * @param f the floating-point value whose ulp is to be returned * @return the size of an ulp of the argument * @author Joseph D. Darcy * @since 1.5 */ public static float ulp(float f) { int exp = getExponent(f); switch(exp) { case FloatConsts.MAX_EXPONENT+1: // NaN or infinity return Math.abs(f); case FloatConsts.MIN_EXPONENT-1: // zero or subnormal return FloatConsts.MIN_VALUE; default: assert exp <= FloatConsts.MAX_EXPONENT && exp >= FloatConsts.MIN_EXPONENT; // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x)) exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1); if (exp >= FloatConsts.MIN_EXPONENT) { return powerOfTwoF(exp); } else { // return a subnormal result; left shift integer // representation of FloatConsts.MIN_VALUE appropriate // number of positions return Float.intBitsToFloat(1 << (exp - (FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) )); } } } /** * Returns the signum function of the argument; zero if the argument * is zero, 1.0 if the argument is greater than zero, -1.0 if the * argument is less than zero. * * <p>Special Cases: * <ul> * <li> If the argument is NaN, then the result is NaN. * <li> If the argument is positive zero or negative zero, then the * result is the same as the argument. * </ul> * * @param d the floating-point value whose signum is to be returned * @return the signum function of the argument * @author Joseph D. Darcy * @since 1.5 */ public static double signum(double d) { return (d == 0.0 || isNaN(d))?d:copySign(1.0, d); } /** * Returns the signum function of the argument; zero if the argument * is zero, 1.0f if the argument is greater than zero, -1.0f if the * argument is less than zero. * * <p>Special Cases: * <ul> * <li> If the argument is NaN, then the result is NaN. * <li> If the argument is positive zero or negative zero, then the * result is the same as the argument. * </ul> * * @param f the floating-point value whose signum is to be returned * @return the signum function of the argument * @author Joseph D. Darcy * @since 1.5 */ public static float signum(float f) { return (f == 0.0f || isNaN(f))?f:copySign(1.0f, f); } }