Java Code Examples for sun.misc.FloatConsts#SIGNIFICAND_WIDTH

The following examples show how to use sun.misc.FloatConsts#SIGNIFICAND_WIDTH . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Math.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 2
Source File: Math.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 3
Source File: Math.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 4
Source File: Math.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 5
Source File: Math.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 6
Source File: FpUtils.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns unbiased exponent of a <code>float</code>.
 */
public static int getExponent(float f){
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
 
Example 7
Source File: Math.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code int} to the argument, with ties
 * rounding to positive infinity.
 *
 * <p>
 * Special cases:
 * <ul><li>If the argument is NaN, the result is 0.
 * <li>If the argument is negative infinity or any value less than or
 * equal to the value of {@code Integer.MIN_VALUE}, the result is
 * equal to the value of {@code Integer.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Integer.MAX_VALUE}, the result is
 * equal to the value of {@code Integer.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to an integer.
 * @return  the value of the argument rounded to the nearest
 *          {@code int} value.
 * @see     java.lang.Integer#MAX_VALUE
 * @see     java.lang.Integer#MIN_VALUE
 */
public static int round(float a) {
    int intBits = Float.floatToRawIntBits(a);
    int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
            >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
    int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
            + FloatConsts.EXP_BIAS) - biasedExp;
    if ((shift & -32) == 0) { // shift >= 0 && shift < 32
        // a is a finite number such that pow(2,-32) <= ulp(a) < 1
        int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                | (FloatConsts.SIGNIF_BIT_MASK + 1));
        if (intBits < 0) {
            r = -r;
        }
        // In the comments below each Java expression evaluates to the value
        // the corresponding mathematical expression:
        // (r) evaluates to a / ulp(a)
        // (r >> shift) evaluates to floor(a * 2)
        // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
        // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
        return ((r >> shift) + 1) >> 1;
    } else {
        // a is either
        // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (int) a;
    }
}
 
Example 8
Source File: Math.java    From Java8CN with Apache License 2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 9
Source File: Math.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 10
Source File: BigInteger.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    if (signum == 0) {
        return 0.0f;
    }

    int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1;

    // exponent == floor(log2(abs(this)))
    if (exponent < Long.SIZE - 1) {
        return longValue();
    } else if (exponent > Float.MAX_EXPONENT) {
        return signum > 0 ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY;
    }

    /*
     * We need the top SIGNIFICAND_WIDTH bits, including the "implicit"
     * one bit. To make rounding easier, we pick out the top
     * SIGNIFICAND_WIDTH + 1 bits, so we have one to help us round up or
     * down. twiceSignifFloor will contain the top SIGNIFICAND_WIDTH + 1
     * bits, and signifFloor the top SIGNIFICAND_WIDTH.
     *
     * It helps to consider the real number signif = abs(this) *
     * 2^(SIGNIFICAND_WIDTH - 1 - exponent).
     */
    int shift = exponent - FloatConsts.SIGNIFICAND_WIDTH;

    int twiceSignifFloor;
    // twiceSignifFloor will be == abs().shiftRight(shift).intValue()
    // We do the shift into an int directly to improve performance.

    int nBits = shift & 0x1f;
    int nBits2 = 32 - nBits;

    if (nBits == 0) {
        twiceSignifFloor = mag[0];
    } else {
        twiceSignifFloor = mag[0] >>> nBits;
        if (twiceSignifFloor == 0) {
            twiceSignifFloor = (mag[0] << nBits2) | (mag[1] >>> nBits);
        }
    }

    int signifFloor = twiceSignifFloor >> 1;
    signifFloor &= FloatConsts.SIGNIF_BIT_MASK; // remove the implied bit

    /*
     * We round up if either the fractional part of signif is strictly
     * greater than 0.5 (which is true if the 0.5 bit is set and any lower
     * bit is set), or if the fractional part of signif is >= 0.5 and
     * signifFloor is odd (which is true if both the 0.5 bit and the 1 bit
     * are set). This is equivalent to the desired HALF_EVEN rounding.
     */
    boolean increment = (twiceSignifFloor & 1) != 0
            && ((signifFloor & 1) != 0 || abs().getLowestSetBit() < shift);
    int signifRounded = increment ? signifFloor + 1 : signifFloor;
    int bits = ((exponent + FloatConsts.EXP_BIAS))
            << (FloatConsts.SIGNIFICAND_WIDTH - 1);
    bits += signifRounded;
    /*
     * If signifRounded == 2^24, we'd need to set all of the significand
     * bits to zero and add 1 to the exponent. This is exactly the behavior
     * we get from just adding signifRounded to bits directly. If the
     * exponent is Float.MAX_EXPONENT, we round up (correctly) to
     * Float.POSITIVE_INFINITY.
     */
    bits |= signum & FloatConsts.SIGN_BIT_MASK;
    return Float.intBitsToFloat(bits);
}
 
Example 11
Source File: Math.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 12
Source File: Math.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 13
Source File: BigInteger.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    if (signum == 0) {
        return 0.0f;
    }

    int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1;

    // exponent == floor(log2(abs(this)))
    if (exponent < Long.SIZE - 1) {
        return longValue();
    } else if (exponent > Float.MAX_EXPONENT) {
        return signum > 0 ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY;
    }

    /*
     * We need the top SIGNIFICAND_WIDTH bits, including the "implicit"
     * one bit. To make rounding easier, we pick out the top
     * SIGNIFICAND_WIDTH + 1 bits, so we have one to help us round up or
     * down. twiceSignifFloor will contain the top SIGNIFICAND_WIDTH + 1
     * bits, and signifFloor the top SIGNIFICAND_WIDTH.
     *
     * It helps to consider the real number signif = abs(this) *
     * 2^(SIGNIFICAND_WIDTH - 1 - exponent).
     */
    int shift = exponent - FloatConsts.SIGNIFICAND_WIDTH;

    int twiceSignifFloor;
    // twiceSignifFloor will be == abs().shiftRight(shift).intValue()
    // We do the shift into an int directly to improve performance.

    int nBits = shift & 0x1f;
    int nBits2 = 32 - nBits;

    if (nBits == 0) {
        twiceSignifFloor = mag[0];
    } else {
        twiceSignifFloor = mag[0] >>> nBits;
        if (twiceSignifFloor == 0) {
            twiceSignifFloor = (mag[0] << nBits2) | (mag[1] >>> nBits);
        }
    }

    int signifFloor = twiceSignifFloor >> 1;
    signifFloor &= FloatConsts.SIGNIF_BIT_MASK; // remove the implied bit

    /*
     * We round up if either the fractional part of signif is strictly
     * greater than 0.5 (which is true if the 0.5 bit is set and any lower
     * bit is set), or if the fractional part of signif is >= 0.5 and
     * signifFloor is odd (which is true if both the 0.5 bit and the 1 bit
     * are set). This is equivalent to the desired HALF_EVEN rounding.
     */
    boolean increment = (twiceSignifFloor & 1) != 0
            && ((signifFloor & 1) != 0 || abs().getLowestSetBit() < shift);
    int signifRounded = increment ? signifFloor + 1 : signifFloor;
    int bits = ((exponent + FloatConsts.EXP_BIAS))
            << (FloatConsts.SIGNIFICAND_WIDTH - 1);
    bits += signifRounded;
    /*
     * If signifRounded == 2^24, we'd need to set all of the significand
     * bits to zero and add 1 to the exponent. This is exactly the behavior
     * we get from just adding signifRounded to bits directly. If the
     * exponent is Float.MAX_EXPONENT, we round up (correctly) to
     * Float.POSITIVE_INFINITY.
     */
    bits |= signum & FloatConsts.SIGN_BIT_MASK;
    return Float.intBitsToFloat(bits);
}
 
Example 14
Source File: Math.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 15
Source File: FpUtils.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a {@code float}; for
 * subnormal values, the number is treated as if it were
 * normalized.  That is for all finite, non-zero, positive numbers
 * <i>x</i>, <code>scalb(<i>x</i>, -ilogb(<i>x</i>))</code> is
 * always in the range [1, 2).
 * <p>
 * Special cases:
 * <ul>
 * <li> If the argument is NaN, then the result is 2<sup>30</sup>.
 * <li> If the argument is infinite, then the result is 2<sup>28</sup>.
 * <li> If the argument is zero, then the result is -(2<sup>28</sup>).
 * </ul>
 *
 * @param f floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
 public static int ilogb(float f) {
    int exponent = getExponent(f);

    switch (exponent) {
    case FloatConsts.MAX_EXPONENT+1:        // NaN or infinity
        if( isNaN(f) )
            return (1<<30);         // 2^30
        else // infinite value
            return (1<<28);         // 2^28

    case FloatConsts.MIN_EXPONENT-1:        // zero or subnormal
        if(f == 0.0f) {
            return -(1<<28);        // -(2^28)
        }
        else {
            int transducer = Float.floatToRawIntBits(f);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when f's significand
             * is normalized is done in integer arithmetic.
             * (there must be at least one "1" bit in the
             * significand since zero has been screened out.
             */

            // isolate significand bits
            transducer &= FloatConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0);

            // This loop is simple and functional. We might be
            // able to do something more clever that was faster;
            // e.g. number of leading zero detection on
            // (transducer << (# exponent and sign bits).
            while (transducer <
                   (1 << (FloatConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    FloatConsts.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < FloatConsts.MIN_EXPONENT);
            return exponent;
        }

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    }
}
 
Example 16
Source File: Math.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Returns {@code f} &times;
 * 2<sup>{@code scaleFactor}</sup> rounded as if performed
 * by a single correctly rounded floating-point multiply to a
 * member of the float value set.  See the Java
 * Language Specification for a discussion of floating-point
 * value sets.  If the exponent of the result is between {@link
 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
 * answer is calculated exactly.  If the exponent of the result
 * would be larger than {@code Float.MAX_EXPONENT}, an
 * infinity is returned.  Note that if the result is subnormal,
 * precision may be lost; that is, when {@code scalb(x, n)}
 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
 * <i>x</i>.  When the result is non-NaN, the result has the same
 * sign as {@code f}.
 *
 * <p>Special cases:
 * <ul>
 * <li> If the first argument is NaN, NaN is returned.
 * <li> If the first argument is infinite, then an infinity of the
 * same sign is returned.
 * <li> If the first argument is zero, then a zero of the same
 * sign is returned.
 * </ul>
 *
 * @param f number to be scaled by a power of two.
 * @param scaleFactor power of 2 used to scale {@code f}
 * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
 * @since 1.6
 */
public static float scalb(float f, int scaleFactor) {
    // magnitude of a power of two so large that scaling a finite
    // nonzero value by it would be guaranteed to over or
    // underflow; due to rounding, scaling down takes takes an
    // additional power of two which is reflected here
    final int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT +
                          FloatConsts.SIGNIFICAND_WIDTH + 1;

    // Make sure scaling factor is in a reasonable range
    scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);

    /*
     * Since + MAX_SCALE for float fits well within the double
     * exponent range and + float -> double conversion is exact
     * the multiplication below will be exact. Therefore, the
     * rounding that occurs when the double product is cast to
     * float will be the correctly rounded float result.  Since
     * all operations other than the final multiply will be exact,
     * it is not necessary to declare this method strictfp.
     */
    return (float)((double)f*powerOfTwoD(scaleFactor));
}
 
Example 17
Source File: BigInteger.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code float}.  This
 * conversion is similar to the
 * <i>narrowing primitive conversion</i> from {@code double} to
 * {@code float} as defined in section 5.1.3 of
 * <cite>The Java&trade; Language Specification</cite>:
 * if this BigInteger has too great a magnitude
 * to represent as a {@code float}, it will be converted to
 * {@link Float#NEGATIVE_INFINITY} or {@link
 * Float#POSITIVE_INFINITY} as appropriate.  Note that even when
 * the return value is finite, this conversion can lose
 * information about the precision of the BigInteger value.
 *
 * @return this BigInteger converted to a {@code float}.
 */
public float floatValue() {
    if (signum == 0) {
        return 0.0f;
    }

    int exponent = ((mag.length - 1) << 5) + bitLengthForInt(mag[0]) - 1;

    // exponent == floor(log2(abs(this)))
    if (exponent < Long.SIZE - 1) {
        return longValue();
    } else if (exponent > Float.MAX_EXPONENT) {
        return signum > 0 ? Float.POSITIVE_INFINITY : Float.NEGATIVE_INFINITY;
    }

    /*
     * We need the top SIGNIFICAND_WIDTH bits, including the "implicit"
     * one bit. To make rounding easier, we pick out the top
     * SIGNIFICAND_WIDTH + 1 bits, so we have one to help us round up or
     * down. twiceSignifFloor will contain the top SIGNIFICAND_WIDTH + 1
     * bits, and signifFloor the top SIGNIFICAND_WIDTH.
     *
     * It helps to consider the real number signif = abs(this) *
     * 2^(SIGNIFICAND_WIDTH - 1 - exponent).
     */
    int shift = exponent - FloatConsts.SIGNIFICAND_WIDTH;

    int twiceSignifFloor;
    // twiceSignifFloor will be == abs().shiftRight(shift).intValue()
    // We do the shift into an int directly to improve performance.

    int nBits = shift & 0x1f;
    int nBits2 = 32 - nBits;

    if (nBits == 0) {
        twiceSignifFloor = mag[0];
    } else {
        twiceSignifFloor = mag[0] >>> nBits;
        if (twiceSignifFloor == 0) {
            twiceSignifFloor = (mag[0] << nBits2) | (mag[1] >>> nBits);
        }
    }

    int signifFloor = twiceSignifFloor >> 1;
    signifFloor &= FloatConsts.SIGNIF_BIT_MASK; // remove the implied bit

    /*
     * We round up if either the fractional part of signif is strictly
     * greater than 0.5 (which is true if the 0.5 bit is set and any lower
     * bit is set), or if the fractional part of signif is >= 0.5 and
     * signifFloor is odd (which is true if both the 0.5 bit and the 1 bit
     * are set). This is equivalent to the desired HALF_EVEN rounding.
     */
    boolean increment = (twiceSignifFloor & 1) != 0
            && ((signifFloor & 1) != 0 || abs().getLowestSetBit() < shift);
    int signifRounded = increment ? signifFloor + 1 : signifFloor;
    int bits = ((exponent + FloatConsts.EXP_BIAS))
            << (FloatConsts.SIGNIFICAND_WIDTH - 1);
    bits += signifRounded;
    /*
     * If signifRounded == 2^24, we'd need to set all of the significand
     * bits to zero and add 1 to the exponent. This is exactly the behavior
     * we get from just adding signifRounded to bits directly. If the
     * exponent is Float.MAX_EXPONENT, we round up (correctly) to
     * Float.POSITIVE_INFINITY.
     */
    bits |= signum & FloatConsts.SIGN_BIT_MASK;
    return Float.intBitsToFloat(bits);
}
 
Example 18
Source File: Math.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unbiased exponent used in the representation of a
 * {@code float}.  Special cases:
 *
 * <ul>
 * <li>If the argument is NaN or infinite, then the result is
 * {@link Float#MAX_EXPONENT} + 1.
 * <li>If the argument is zero or subnormal, then the result is
 * {@link Float#MIN_EXPONENT} -1.
 * </ul>
 * @param f a {@code float} value
 * @return the unbiased exponent of the argument
 * @since 1.6
 */
public static int getExponent(float f) {
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
 
Example 19
Source File: Math.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Returns the unbiased exponent used in the representation of a
 * {@code float}.  Special cases:
 *
 * <ul>
 * <li>If the argument is NaN or infinite, then the result is
 * {@link Float#MAX_EXPONENT} + 1.
 * <li>If the argument is zero or subnormal, then the result is
 * {@link Float#MIN_EXPONENT} -1.
 * </ul>
 * @param f a {@code float} value
 * @return the unbiased exponent of the argument
 * @since 1.6
 */
public static int getExponent(float f) {
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}
 
Example 20
Source File: Math.java    From jdk8u-dev-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Returns the unbiased exponent used in the representation of a
 * {@code float}.  Special cases:
 *
 * <ul>
 * <li>If the argument is NaN or infinite, then the result is
 * {@link Float#MAX_EXPONENT} + 1.
 * <li>If the argument is zero or subnormal, then the result is
 * {@link Float#MIN_EXPONENT} -1.
 * </ul>
 * @param f a {@code float} value
 * @return the unbiased exponent of the argument
 * @since 1.6
 */
public static int getExponent(float f) {
    /*
     * Bitwise convert f to integer, mask out exponent bits, shift
     * to the right and then subtract out float's bias adjust to
     * get true exponent value
     */
    return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
            (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
}