sun.misc.FloatConsts Java Examples

The following examples show how to use sun.misc.FloatConsts. 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 JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Returns a floating-point power of two in the normal range.
 */
static float powerOfTwoF(int n) {
    assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT);
    return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
                                 (FloatConsts.SIGNIFICAND_WIDTH-1))
                                & FloatConsts.EXP_BIT_MASK);
}
 
Example #2
Source File: Math.java    From AndroidComponentPlugin 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 #3
Source File: IeeeRecommendedTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static int testFloatNextDown() {
    int failures=0;

    /*
     * Each row of testCases represents one test case for nextDown;
     * the first column is the input and the second column is the
     * expected result.
     */
    float testCases [][] = {
        {NaNf,                      NaNf},
        {-infinityF,                -infinityF},
        {-Float.MAX_VALUE,          -infinityF},
        {-Float_MAX_VALUEmm,        -Float.MAX_VALUE},
        {-Float_MAX_SUBNORMAL,      -FloatConsts.MIN_NORMAL},
        {-Float_MAX_SUBNORMALmm,    -Float_MAX_SUBNORMAL},
        {-0.0f,                     -Float.MIN_VALUE},
        {+0.0f,                     -Float.MIN_VALUE},
        {Float.MIN_VALUE,           0.0f},
        {Float.MIN_VALUE*2,         Float.MIN_VALUE},
        {Float_MAX_SUBNORMAL,       Float_MAX_SUBNORMALmm},
        {FloatConsts.MIN_NORMAL,    Float_MAX_SUBNORMAL},
        {FloatConsts.MIN_NORMAL+
         Float.MIN_VALUE,           FloatConsts.MIN_NORMAL},
        {Float.MAX_VALUE,           Float_MAX_VALUEmm},
        {infinityF,                 Float.MAX_VALUE},
    };

    for(int i = 0; i < testCases.length; i++) {
        failures+=Tests.test("Math.nextDown(float)",
                             testCases[i][0], Math.nextDown(testCases[i][0]), testCases[i][1]);

        failures+=Tests.test("StrictMath.nextDown(float)",
                             testCases[i][0], StrictMath.nextDown(testCases[i][0]), testCases[i][1]);
    }

    return failures;
}
 
Example #4
Source File: IeeeRecommendedTests.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static int testFloatNextUp() {
    int failures=0;

    /*
     * Each row of testCases represents one test case for nextUp;
     * the first column is the input and the second column is the
     * expected result.
     */
    float testCases [][] = {
        {NaNf,                      NaNf},
        {-infinityF,                -Float.MAX_VALUE},
        {-Float.MAX_VALUE,          -Float_MAX_VALUEmm},
        {-FloatConsts.MIN_NORMAL,   -Float_MAX_SUBNORMAL},
        {-Float_MAX_SUBNORMAL,      -Float_MAX_SUBNORMALmm},
        {-Float.MIN_VALUE,          -0.0f},
        {-0.0f,                     Float.MIN_VALUE},
        {+0.0f,                     Float.MIN_VALUE},
        {Float.MIN_VALUE,           Float.MIN_VALUE*2},
        {Float_MAX_SUBNORMALmm,     Float_MAX_SUBNORMAL},
        {Float_MAX_SUBNORMAL,       FloatConsts.MIN_NORMAL},
        {FloatConsts.MIN_NORMAL,    FloatConsts.MIN_NORMAL+Float.MIN_VALUE},
        {Float_MAX_VALUEmm,         Float.MAX_VALUE},
        {Float.MAX_VALUE,           infinityF},
        {infinityF,                 infinityF}
    };

    for(int i = 0; i < testCases.length; i++) {
        failures+=Tests.test("Math.nextUp(float)",
                             testCases[i][0], Math.nextUp(testCases[i][0]), testCases[i][1]);

        failures+=Tests.test("StrictMath.nextUp(float)",
                             testCases[i][0], StrictMath.nextUp(testCases[i][0]), testCases[i][1]);
    }

    return failures;
}
 
Example #5
Source File: Math.java    From jdk1.8-source-analysis 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 #6
Source File: IeeeRecommendedTests.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static int testFloatSignum() {
    int failures = 0;
    float testCases [][] = {
        {NaNf,                      NaNf},
        {-infinityF,                -1.0f},
        {-Float.MAX_VALUE,          -1.0f},
        {-FloatConsts.MIN_NORMAL,   -1.0f},
        {-1.0f,                     -1.0f},
        {-2.0f,                     -1.0f},
        {-Float_MAX_SUBNORMAL,      -1.0f},
        {-Float.MIN_VALUE,          -1.0f},
        {-0.0f,                     -0.0f},
        {+0.0f,                     +0.0f},
        {Float.MIN_VALUE,            1.0f},
        {Float_MAX_SUBNORMALmm,      1.0f},
        {Float_MAX_SUBNORMAL,        1.0f},
        {FloatConsts.MIN_NORMAL,     1.0f},
        {1.0f,                       1.0f},
        {2.0f,                       1.0f},
        {Float_MAX_VALUEmm,          1.0f},
        {Float.MAX_VALUE,            1.0f},
        {infinityF,                  1.0f}
    };

    for(int i = 0; i < testCases.length; i++) {
        failures+=Tests.test("Math.signum(float)",
                             testCases[i][0], Math.signum(testCases[i][0]), testCases[i][1]);
        failures+=Tests.test("StrictMath.signum(float)",
                             testCases[i][0], StrictMath.signum(testCases[i][0]), testCases[i][1]);
    }

    return failures;
}
 
Example #7
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 #8
Source File: IeeeRecommendedTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static int testFloatNextUp() {
    int failures=0;

    /*
     * Each row of testCases represents one test case for nextUp;
     * the first column is the input and the second column is the
     * expected result.
     */
    float testCases [][] = {
        {NaNf,                      NaNf},
        {-infinityF,                -Float.MAX_VALUE},
        {-Float.MAX_VALUE,          -Float_MAX_VALUEmm},
        {-FloatConsts.MIN_NORMAL,   -Float_MAX_SUBNORMAL},
        {-Float_MAX_SUBNORMAL,      -Float_MAX_SUBNORMALmm},
        {-Float.MIN_VALUE,          -0.0f},
        {-0.0f,                     Float.MIN_VALUE},
        {+0.0f,                     Float.MIN_VALUE},
        {Float.MIN_VALUE,           Float.MIN_VALUE*2},
        {Float_MAX_SUBNORMALmm,     Float_MAX_SUBNORMAL},
        {Float_MAX_SUBNORMAL,       FloatConsts.MIN_NORMAL},
        {FloatConsts.MIN_NORMAL,    FloatConsts.MIN_NORMAL+Float.MIN_VALUE},
        {Float_MAX_VALUEmm,         Float.MAX_VALUE},
        {Float.MAX_VALUE,           infinityF},
        {infinityF,                 infinityF}
    };

    for(int i = 0; i < testCases.length; i++) {
        failures+=Tests.test("Math.nextUp(float)",
                             testCases[i][0], Math.nextUp(testCases[i][0]), testCases[i][1]);

        failures+=Tests.test("StrictMath.nextUp(float)",
                             testCases[i][0], StrictMath.nextUp(testCases[i][0]), testCases[i][1]);
    }

    return failures;
}
 
Example #9
Source File: Math.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a floating-point power of two in the normal range.
 */
static float powerOfTwoF(int n) {
    assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT);
    return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
                                 (FloatConsts.SIGNIFICAND_WIDTH-1))
                                & FloatConsts.EXP_BIT_MASK);
}
 
Example #10
Source File: Math.java    From openjdk-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 #11
Source File: IeeeRecommendedTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static int testFloatNextUp() {
    int failures=0;

    /*
     * Each row of testCases represents one test case for nextUp;
     * the first column is the input and the second column is the
     * expected result.
     */
    float testCases [][] = {
        {NaNf,                      NaNf},
        {-infinityF,                -Float.MAX_VALUE},
        {-Float.MAX_VALUE,          -Float_MAX_VALUEmm},
        {-FloatConsts.MIN_NORMAL,   -Float_MAX_SUBNORMAL},
        {-Float_MAX_SUBNORMAL,      -Float_MAX_SUBNORMALmm},
        {-Float.MIN_VALUE,          -0.0f},
        {-0.0f,                     Float.MIN_VALUE},
        {+0.0f,                     Float.MIN_VALUE},
        {Float.MIN_VALUE,           Float.MIN_VALUE*2},
        {Float_MAX_SUBNORMALmm,     Float_MAX_SUBNORMAL},
        {Float_MAX_SUBNORMAL,       FloatConsts.MIN_NORMAL},
        {FloatConsts.MIN_NORMAL,    FloatConsts.MIN_NORMAL+Float.MIN_VALUE},
        {Float_MAX_VALUEmm,         Float.MAX_VALUE},
        {Float.MAX_VALUE,           infinityF},
        {infinityF,                 infinityF}
    };

    for(int i = 0; i < testCases.length; i++) {
        failures+=Tests.test("Math.nextUp(float)",
                             testCases[i][0], Math.nextUp(testCases[i][0]), testCases[i][1]);

        failures+=Tests.test("StrictMath.nextUp(float)",
                             testCases[i][0], StrictMath.nextUp(testCases[i][0]), testCases[i][1]);
    }

    return failures;
}
 
Example #12
Source File: Math.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a floating-point power of two in the normal range.
 */
static float powerOfTwoF(int n) {
    assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT);
    return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
                                 (FloatConsts.SIGNIFICAND_WIDTH-1))
                                & FloatConsts.EXP_BIT_MASK);
}
 
Example #13
Source File: Math.java    From openjdk-jdk8u 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 #14
Source File: IeeeRecommendedTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static int testFloatSignum() {
    int failures = 0;
    float testCases [][] = {
        {NaNf,                      NaNf},
        {-infinityF,                -1.0f},
        {-Float.MAX_VALUE,          -1.0f},
        {-FloatConsts.MIN_NORMAL,   -1.0f},
        {-1.0f,                     -1.0f},
        {-2.0f,                     -1.0f},
        {-Float_MAX_SUBNORMAL,      -1.0f},
        {-Float.MIN_VALUE,          -1.0f},
        {-0.0f,                     -0.0f},
        {+0.0f,                     +0.0f},
        {Float.MIN_VALUE,            1.0f},
        {Float_MAX_SUBNORMALmm,      1.0f},
        {Float_MAX_SUBNORMAL,        1.0f},
        {FloatConsts.MIN_NORMAL,     1.0f},
        {1.0f,                       1.0f},
        {2.0f,                       1.0f},
        {Float_MAX_VALUEmm,          1.0f},
        {Float.MAX_VALUE,            1.0f},
        {infinityF,                  1.0f}
    };

    for(int i = 0; i < testCases.length; i++) {
        failures+=Tests.test("Math.signum(float)",
                             testCases[i][0], Math.signum(testCases[i][0]), testCases[i][1]);
        failures+=Tests.test("StrictMath.signum(float)",
                             testCases[i][0], StrictMath.signum(testCases[i][0]), testCases[i][1]);
    }

    return failures;
}
 
Example #15
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 #16
Source File: Math.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a floating-point power of two in the normal range.
 */
static float powerOfTwoF(int n) {
    assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT);
    return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
                                 (FloatConsts.SIGNIFICAND_WIDTH-1))
                                & FloatConsts.EXP_BIT_MASK);
}
 
Example #17
Source File: Math.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a floating-point power of two in the normal range.
 */
static float powerOfTwoF(int n) {
    assert(n >= FloatConsts.MIN_EXPONENT && n <= FloatConsts.MAX_EXPONENT);
    return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
                                 (FloatConsts.SIGNIFICAND_WIDTH-1))
                                & FloatConsts.EXP_BIT_MASK);
}
 
Example #18
Source File: BigInteger.java    From jdk8u60 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 #19
Source File: IeeeRecommendedTests.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public static int testFloatNextAfter() {
    int failures=0;

    /*
     * Each row of the testCases matrix represents one test case
     * for nexAfter; given the input of the first two columns, the
     * result in the last column is expected.
     */
    float [][] testCases  = {
        {NaNf,              NaNf,                   NaNf},
        {NaNf,              0.0f,                   NaNf},
        {0.0f,              NaNf,                   NaNf},
        {NaNf,              infinityF,              NaNf},
        {infinityF,         NaNf,                   NaNf},

        {infinityF,         infinityF,              infinityF},
        {infinityF,         -infinityF,             Float.MAX_VALUE},
        {infinityF,         0.0f,                   Float.MAX_VALUE},

        {Float.MAX_VALUE,   infinityF,              infinityF},
        {Float.MAX_VALUE,   -infinityF,             Float_MAX_VALUEmm},
        {Float.MAX_VALUE,   Float.MAX_VALUE,        Float.MAX_VALUE},
        {Float.MAX_VALUE,   0.0f,                   Float_MAX_VALUEmm},

        {Float_MAX_VALUEmm, Float.MAX_VALUE,        Float.MAX_VALUE},
        {Float_MAX_VALUEmm, infinityF,              Float.MAX_VALUE},
        {Float_MAX_VALUEmm, Float_MAX_VALUEmm,      Float_MAX_VALUEmm},

        {FloatConsts.MIN_NORMAL,    infinityF,              FloatConsts.MIN_NORMAL+
                                                            Float.MIN_VALUE},
        {FloatConsts.MIN_NORMAL,    -infinityF,             Float_MAX_SUBNORMAL},
        {FloatConsts.MIN_NORMAL,    1.0f,                   FloatConsts.MIN_NORMAL+
                                                            Float.MIN_VALUE},
        {FloatConsts.MIN_NORMAL,    -1.0f,                  Float_MAX_SUBNORMAL},
        {FloatConsts.MIN_NORMAL,    FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL},

        {Float_MAX_SUBNORMAL,       FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL},
        {Float_MAX_SUBNORMAL,       Float_MAX_SUBNORMAL,    Float_MAX_SUBNORMAL},
        {Float_MAX_SUBNORMAL,       0.0f,                   Float_MAX_SUBNORMALmm},

        {Float_MAX_SUBNORMALmm,     Float_MAX_SUBNORMAL,    Float_MAX_SUBNORMAL},
        {Float_MAX_SUBNORMALmm,     0.0f,                   Float_MAX_SUBNORMALmm-Float.MIN_VALUE},
        {Float_MAX_SUBNORMALmm,     Float_MAX_SUBNORMALmm,  Float_MAX_SUBNORMALmm},

        {Float.MIN_VALUE,   0.0f,                   0.0f},
        {-Float.MIN_VALUE,  0.0f,                   -0.0f},
        {Float.MIN_VALUE,   Float.MIN_VALUE,        Float.MIN_VALUE},
        {Float.MIN_VALUE,   1.0f,                   2*Float.MIN_VALUE},

        // Make sure zero behavior is tested
        {0.0f,              0.0f,                   0.0f},
        {0.0f,              -0.0f,                  -0.0f},
        {-0.0f,             0.0f,                   0.0f},
        {-0.0f,             -0.0f,                  -0.0f},
        {0.0f,              infinityF,              Float.MIN_VALUE},
        {0.0f,              -infinityF,             -Float.MIN_VALUE},
        {-0.0f,             infinityF,              Float.MIN_VALUE},
        {-0.0f,             -infinityF,             -Float.MIN_VALUE},
        {0.0f,              Float.MIN_VALUE,        Float.MIN_VALUE},
        {0.0f,              -Float.MIN_VALUE,       -Float.MIN_VALUE},
        {-0.0f,             Float.MIN_VALUE,        Float.MIN_VALUE},
        {-0.0f,             -Float.MIN_VALUE,       -Float.MIN_VALUE}
    };

    for(int i = 0; i < testCases.length; i++) {
        failures += testNextAfterCase(testCases[i][0], testCases[i][1],
                                      testCases[i][2]);
    }

    return failures;
}
 
Example #20
Source File: FpUtils.java    From openjdk-jdk8u-backup 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 #21
Source File: IeeeRecommendedTests.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static int testFloatBooleanMethods() {
    int failures = 0;

    float testCases [] = {
        NaNf,
        -infinityF,
        infinityF,
        -Float.MAX_VALUE,
        -3.0f,
        -1.0f,
        -FloatConsts.MIN_NORMAL,
        -Float_MAX_SUBNORMALmm,
        -Float_MAX_SUBNORMAL,
        -Float.MIN_VALUE,
        -0.0f,
        +0.0f,
        Float.MIN_VALUE,
        Float_MAX_SUBNORMALmm,
        Float_MAX_SUBNORMAL,
        FloatConsts.MIN_NORMAL,
        1.0f,
        3.0f,
        Float_MAX_VALUEmm,
        Float.MAX_VALUE
    };

    for(int i = 0; i < testCases.length; i++) {
        // isNaN
        failures+=Tests.test("FpUtils.isNaN(float)", testCases[i],
                             FpUtils.isNaN(testCases[i]), (i ==0));

        // isFinite
        failures+=Tests.test("Float.isFinite(float)", testCases[i],
                             Float.isFinite(testCases[i]), (i >= 3));

        // isInfinite
        failures+=Tests.test("FpUtils.isInfinite(float)", testCases[i],
                             FpUtils.isInfinite(testCases[i]), (i==1 || i==2));

        // isUnorderd
        for(int j = 0; j < testCases.length; j++) {
            failures+=Tests.test("FpUtils.isUnordered(float, float)", testCases[i],testCases[j],
                                 FpUtils.isUnordered(testCases[i],testCases[j]), (i==0 || j==0));
        }
    }

    return failures;
}
 
Example #22
Source File: IeeeRecommendedTests.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static int testFloatBooleanMethods() {
    int failures = 0;

    float testCases [] = {
        NaNf,
        -infinityF,
        infinityF,
        -Float.MAX_VALUE,
        -3.0f,
        -1.0f,
        -FloatConsts.MIN_NORMAL,
        -Float_MAX_SUBNORMALmm,
        -Float_MAX_SUBNORMAL,
        -Float.MIN_VALUE,
        -0.0f,
        +0.0f,
        Float.MIN_VALUE,
        Float_MAX_SUBNORMALmm,
        Float_MAX_SUBNORMAL,
        FloatConsts.MIN_NORMAL,
        1.0f,
        3.0f,
        Float_MAX_VALUEmm,
        Float.MAX_VALUE
    };

    for(int i = 0; i < testCases.length; i++) {
        // isNaN
        failures+=Tests.test("FpUtils.isNaN(float)", testCases[i],
                             FpUtils.isNaN(testCases[i]), (i ==0));

        // isFinite
        failures+=Tests.test("Float.isFinite(float)", testCases[i],
                             Float.isFinite(testCases[i]), (i >= 3));

        // isInfinite
        failures+=Tests.test("FpUtils.isInfinite(float)", testCases[i],
                             FpUtils.isInfinite(testCases[i]), (i==1 || i==2));

        // isUnorderd
        for(int j = 0; j < testCases.length; j++) {
            failures+=Tests.test("FpUtils.isUnordered(float, float)", testCases[i],testCases[j],
                                 FpUtils.isUnordered(testCases[i],testCases[j]), (i==0 || j==0));
        }
    }

    return failures;
}
 
Example #23
Source File: FpUtils.java    From jdk8u60 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 #24
Source File: IeeeRecommendedTests.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public static int testFloatBooleanMethods() {
    int failures = 0;

    float testCases [] = {
        NaNf,
        -infinityF,
        infinityF,
        -Float.MAX_VALUE,
        -3.0f,
        -1.0f,
        -FloatConsts.MIN_NORMAL,
        -Float_MAX_SUBNORMALmm,
        -Float_MAX_SUBNORMAL,
        -Float.MIN_VALUE,
        -0.0f,
        +0.0f,
        Float.MIN_VALUE,
        Float_MAX_SUBNORMALmm,
        Float_MAX_SUBNORMAL,
        FloatConsts.MIN_NORMAL,
        1.0f,
        3.0f,
        Float_MAX_VALUEmm,
        Float.MAX_VALUE
    };

    for(int i = 0; i < testCases.length; i++) {
        // isNaN
        failures+=Tests.test("FpUtils.isNaN(float)", testCases[i],
                             FpUtils.isNaN(testCases[i]), (i ==0));

        // isFinite
        failures+=Tests.test("Float.isFinite(float)", testCases[i],
                             Float.isFinite(testCases[i]), (i >= 3));

        // isInfinite
        failures+=Tests.test("FpUtils.isInfinite(float)", testCases[i],
                             FpUtils.isInfinite(testCases[i]), (i==1 || i==2));

        // isUnorderd
        for(int j = 0; j < testCases.length; j++) {
            failures+=Tests.test("FpUtils.isUnordered(float, float)", testCases[i],testCases[j],
                                 FpUtils.isUnordered(testCases[i],testCases[j]), (i==0 || j==0));
        }
    }

    return failures;
}
 
Example #25
Source File: FpUtils.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns unbiased exponent of a <code>float</code>; 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
    // break;

    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;
        }
    // break;

    default:
        assert( exponent >= FloatConsts.MIN_EXPONENT &&
                exponent <= FloatConsts.MAX_EXPONENT);
        return exponent;
    // break;
    }
}
 
Example #26
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 #27
Source File: BigInteger.java    From jdk1.8-source-analysis with Apache License 2.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 #28
Source File: IeeeRecommendedTests.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static int testFloatCopySign() {
    int failures = 0;

    // testCases[0] are logically positive numbers;
    // testCases[1] are negative numbers.
    float testCases [][] = {
        {+0.0f,
         Float.MIN_VALUE,
         Float_MAX_SUBNORMALmm,
         Float_MAX_SUBNORMAL,
         FloatConsts.MIN_NORMAL,
         1.0f,
         3.0f,
         Float_MAX_VALUEmm,
         Float.MAX_VALUE,
         infinityF,
        },
        {-infinityF,
         -Float.MAX_VALUE,
         -3.0f,
         -1.0f,
         -FloatConsts.MIN_NORMAL,
         -Float_MAX_SUBNORMALmm,
         -Float_MAX_SUBNORMAL,
         -Float.MIN_VALUE,
         -0.0f}
    };

    float NaNs[] = {Float.intBitsToFloat(0x7fc00000),       // "positive" NaN
                    Float.intBitsToFloat(0xFfc00000)};      // "negative" NaN

    // Tests shared between raw and non-raw versions
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
            for(int m = 0; m < testCases[i].length; m++) {
                for(int n = 0; n < testCases[j].length; n++) {
                    // copySign(magnitude, sign)
                    failures+=Tests.test("Math.copySign(float,float)",
                                         testCases[i][m],testCases[j][n],
                                         Math.copySign(testCases[i][m], testCases[j][n]),
                                         (j==0?1.0f:-1.0f)*Math.abs(testCases[i][m]) );

                    failures+=Tests.test("StrictMath.copySign(float,float)",
                                         testCases[i][m],testCases[j][n],
                                         StrictMath.copySign(testCases[i][m], testCases[j][n]),
                                         (j==0?1.0f:-1.0f)*Math.abs(testCases[i][m]) );
                }
            }
        }
    }

    // For rawCopySign, NaN may effectively have either sign bit
    // while for copySign NaNs are treated as if they always have
    // a zero sign bit (i.e. as positive numbers)
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < NaNs.length; j++) {
            for(int m = 0; m < testCases[i].length; m++) {
                // copySign(magnitude, sign)

                failures += (Math.abs(Math.copySign(testCases[i][m], NaNs[j])) ==
                             Math.abs(testCases[i][m])) ? 0:1;


                failures+=Tests.test("StrictMath.copySign(float,float)",
                                     testCases[i][m], NaNs[j],
                                     StrictMath.copySign(testCases[i][m], NaNs[j]),
                                     Math.abs(testCases[i][m]) );
            }
        }
    }

    return failures;
}
 
Example #29
Source File: Float.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns a hexadecimal string representation of the
 * {@code float} argument. All characters mentioned below are
 * ASCII characters.
 *
 * <ul>
 * <li>If the argument is NaN, the result is the string
 *     "{@code NaN}".
 * <li>Otherwise, the result is a string that represents the sign and
 * magnitude (absolute value) of the argument. If the sign is negative,
 * the first character of the result is '{@code -}'
 * ({@code '\u005Cu002D'}); if the sign is positive, no sign character
 * appears in the result. As for the magnitude <i>m</i>:
 *
 * <ul>
 * <li>If <i>m</i> is infinity, it is represented by the string
 * {@code "Infinity"}; thus, positive infinity produces the
 * result {@code "Infinity"} and negative infinity produces
 * the result {@code "-Infinity"}.
 *
 * <li>If <i>m</i> is zero, it is represented by the string
 * {@code "0x0.0p0"}; thus, negative zero produces the result
 * {@code "-0x0.0p0"} and positive zero produces the result
 * {@code "0x0.0p0"}.
 *
 * <li>If <i>m</i> is a {@code float} value with a
 * normalized representation, substrings are used to represent the
 * significand and exponent fields.  The significand is
 * represented by the characters {@code "0x1."}
 * followed by a lowercase hexadecimal representation of the rest
 * of the significand as a fraction.  Trailing zeros in the
 * hexadecimal representation are removed unless all the digits
 * are zero, in which case a single zero is used. Next, the
 * exponent is represented by {@code "p"} followed
 * by a decimal string of the unbiased exponent as if produced by
 * a call to {@link Integer#toString(int) Integer.toString} on the
 * exponent value.
 *
 * <li>If <i>m</i> is a {@code float} value with a subnormal
 * representation, the significand is represented by the
 * characters {@code "0x0."} followed by a
 * hexadecimal representation of the rest of the significand as a
 * fraction.  Trailing zeros in the hexadecimal representation are
 * removed. Next, the exponent is represented by
 * {@code "p-126"}.  Note that there must be at
 * least one nonzero digit in a subnormal significand.
 *
 * </ul>
 *
 * </ul>
 *
 * <table border>
 * <caption>Examples</caption>
 * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
 * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
 * <tr><td>{@code -1.0}</td>        <td>{@code -0x1.0p0}</td>
 * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
 * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
 * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
 * <tr><td>{@code 0.25}</td>        <td>{@code 0x1.0p-2}</td>
 * <tr><td>{@code Float.MAX_VALUE}</td>
 *     <td>{@code 0x1.fffffep127}</td>
 * <tr><td>{@code Minimum Normal Value}</td>
 *     <td>{@code 0x1.0p-126}</td>
 * <tr><td>{@code Maximum Subnormal Value}</td>
 *     <td>{@code 0x0.fffffep-126}</td>
 * <tr><td>{@code Float.MIN_VALUE}</td>
 *     <td>{@code 0x0.000002p-126}</td>
 * </table>
 * @param   f   the {@code float} to be converted.
 * @return a hex string representation of the argument.
 * @since 1.5
 * @author Joseph D. Darcy
 */
public static String toHexString(float f) {
    if (Math.abs(f) < FloatConsts.MIN_NORMAL
        &&  f != 0.0f ) {// float subnormal
        // Adjust exponent to create subnormal double, then
        // replace subnormal double exponent with subnormal float
        // exponent
        String s = Double.toHexString(Math.scalb((double)f,
                                                 /* -1022+126 */
                                                 DoubleConsts.MIN_EXPONENT-
                                                 FloatConsts.MIN_EXPONENT));
        return s.replaceFirst("p-1022$", "p-126");
    }
    else // double string will be the same as float string
        return Double.toHexString(f);
}
 
Example #30
Source File: IeeeRecommendedTests.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static int testFloatNextAfter() {
    int failures=0;

    /*
     * Each row of the testCases matrix represents one test case
     * for nexAfter; given the input of the first two columns, the
     * result in the last column is expected.
     */
    float [][] testCases  = {
        {NaNf,              NaNf,                   NaNf},
        {NaNf,              0.0f,                   NaNf},
        {0.0f,              NaNf,                   NaNf},
        {NaNf,              infinityF,              NaNf},
        {infinityF,         NaNf,                   NaNf},

        {infinityF,         infinityF,              infinityF},
        {infinityF,         -infinityF,             Float.MAX_VALUE},
        {infinityF,         0.0f,                   Float.MAX_VALUE},

        {Float.MAX_VALUE,   infinityF,              infinityF},
        {Float.MAX_VALUE,   -infinityF,             Float_MAX_VALUEmm},
        {Float.MAX_VALUE,   Float.MAX_VALUE,        Float.MAX_VALUE},
        {Float.MAX_VALUE,   0.0f,                   Float_MAX_VALUEmm},

        {Float_MAX_VALUEmm, Float.MAX_VALUE,        Float.MAX_VALUE},
        {Float_MAX_VALUEmm, infinityF,              Float.MAX_VALUE},
        {Float_MAX_VALUEmm, Float_MAX_VALUEmm,      Float_MAX_VALUEmm},

        {FloatConsts.MIN_NORMAL,    infinityF,              FloatConsts.MIN_NORMAL+
                                                            Float.MIN_VALUE},
        {FloatConsts.MIN_NORMAL,    -infinityF,             Float_MAX_SUBNORMAL},
        {FloatConsts.MIN_NORMAL,    1.0f,                   FloatConsts.MIN_NORMAL+
                                                            Float.MIN_VALUE},
        {FloatConsts.MIN_NORMAL,    -1.0f,                  Float_MAX_SUBNORMAL},
        {FloatConsts.MIN_NORMAL,    FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL},

        {Float_MAX_SUBNORMAL,       FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL},
        {Float_MAX_SUBNORMAL,       Float_MAX_SUBNORMAL,    Float_MAX_SUBNORMAL},
        {Float_MAX_SUBNORMAL,       0.0f,                   Float_MAX_SUBNORMALmm},

        {Float_MAX_SUBNORMALmm,     Float_MAX_SUBNORMAL,    Float_MAX_SUBNORMAL},
        {Float_MAX_SUBNORMALmm,     0.0f,                   Float_MAX_SUBNORMALmm-Float.MIN_VALUE},
        {Float_MAX_SUBNORMALmm,     Float_MAX_SUBNORMALmm,  Float_MAX_SUBNORMALmm},

        {Float.MIN_VALUE,   0.0f,                   0.0f},
        {-Float.MIN_VALUE,  0.0f,                   -0.0f},
        {Float.MIN_VALUE,   Float.MIN_VALUE,        Float.MIN_VALUE},
        {Float.MIN_VALUE,   1.0f,                   2*Float.MIN_VALUE},

        // Make sure zero behavior is tested
        {0.0f,              0.0f,                   0.0f},
        {0.0f,              -0.0f,                  -0.0f},
        {-0.0f,             0.0f,                   0.0f},
        {-0.0f,             -0.0f,                  -0.0f},
        {0.0f,              infinityF,              Float.MIN_VALUE},
        {0.0f,              -infinityF,             -Float.MIN_VALUE},
        {-0.0f,             infinityF,              Float.MIN_VALUE},
        {-0.0f,             -infinityF,             -Float.MIN_VALUE},
        {0.0f,              Float.MIN_VALUE,        Float.MIN_VALUE},
        {0.0f,              -Float.MIN_VALUE,       -Float.MIN_VALUE},
        {-0.0f,             Float.MIN_VALUE,        Float.MIN_VALUE},
        {-0.0f,             -Float.MIN_VALUE,       -Float.MIN_VALUE}
    };

    for(int i = 0; i < testCases.length; i++) {
        failures += testNextAfterCase(testCases[i][0], testCases[i][1],
                                      testCases[i][2]);
    }

    return failures;
}