sun.misc.DoubleConsts Java Examples

The following examples show how to use sun.misc.DoubleConsts. 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 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 double powerOfTwoD(int n) {
    assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT);
    return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
                                    (DoubleConsts.SIGNIFICAND_WIDTH-1))
                                   & DoubleConsts.EXP_BIT_MASK);
}
 
Example #2
Source File: Math.java    From jdk8u-dev-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 double powerOfTwoD(int n) {
    assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT);
    return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
                                    (DoubleConsts.SIGNIFICAND_WIDTH-1))
                                   & DoubleConsts.EXP_BIT_MASK);
}
 
Example #3
Source File: IeeeRecommendedTests.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static int testDoubleNextUp() {
    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.
     */
    double testCases [][] = {
        {NaNd,                      NaNd},
        {-infinityD,                -Double.MAX_VALUE},
        {-Double.MAX_VALUE,         -Double_MAX_VALUEmm},
        {-DoubleConsts.MIN_NORMAL,  -Double_MAX_SUBNORMAL},
        {-Double_MAX_SUBNORMAL,     -Double_MAX_SUBNORMALmm},
        {-Double.MIN_VALUE,         -0.0d},
        {-0.0d,                     Double.MIN_VALUE},
        {+0.0d,                     Double.MIN_VALUE},
        {Double.MIN_VALUE,          Double.MIN_VALUE*2},
        {Double_MAX_SUBNORMALmm,    Double_MAX_SUBNORMAL},
        {Double_MAX_SUBNORMAL,      DoubleConsts.MIN_NORMAL},
        {DoubleConsts.MIN_NORMAL,   DoubleConsts.MIN_NORMAL+Double.MIN_VALUE},
        {Double_MAX_VALUEmm,        Double.MAX_VALUE},
        {Double.MAX_VALUE,          infinityD},
        {infinityD,                 infinityD}
    };

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

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

    return failures;
}
 
Example #4
Source File: IeeeRecommendedTests.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static int testDoubleNextDown() {
    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.
     */
    double testCases [][] = {
        {NaNd,                      NaNd},
        {-infinityD,                -infinityD},
        {-Double.MAX_VALUE,         -infinityD},
        {-Double_MAX_VALUEmm,       -Double.MAX_VALUE},
        {-Double_MAX_SUBNORMAL,     -DoubleConsts.MIN_NORMAL},
        {-Double_MAX_SUBNORMALmm,   -Double_MAX_SUBNORMAL},
        {-0.0d,                     -Double.MIN_VALUE},
        {+0.0d,                     -Double.MIN_VALUE},
        {Double.MIN_VALUE,          0.0d},
        {Double.MIN_VALUE*2,        Double.MIN_VALUE},
        {Double_MAX_SUBNORMAL,      Double_MAX_SUBNORMALmm},
        {DoubleConsts.MIN_NORMAL,   Double_MAX_SUBNORMAL},
        {DoubleConsts.MIN_NORMAL+
         Double.MIN_VALUE,          DoubleConsts.MIN_NORMAL},
        {Double.MAX_VALUE,          Double_MAX_VALUEmm},
        {infinityD,                 Double.MAX_VALUE},
    };

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

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

    return failures;
}
 
Example #5
Source File: IeeeRecommendedTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static int testDoubleSignum() {
    int failures = 0;
    double testCases [][] = {
        {NaNd,                      NaNd},
        {-infinityD,                -1.0},
        {-Double.MAX_VALUE,         -1.0},
        {-DoubleConsts.MIN_NORMAL,  -1.0},
        {-1.0,                      -1.0},
        {-2.0,                      -1.0},
        {-Double_MAX_SUBNORMAL,     -1.0},
        {-Double.MIN_VALUE,         -1.0d},
        {-0.0d,                     -0.0d},
        {+0.0d,                     +0.0d},
        {Double.MIN_VALUE,           1.0},
        {Double_MAX_SUBNORMALmm,     1.0},
        {Double_MAX_SUBNORMAL,       1.0},
        {DoubleConsts.MIN_NORMAL,    1.0},
        {1.0,                        1.0},
        {2.0,                        1.0},
        {Double_MAX_VALUEmm,         1.0},
        {Double.MAX_VALUE,           1.0},
        {infinityD,                  1.0}
    };

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

    return failures;
}
 
Example #6
Source File: Math.java    From openjdk-jdk8u-backup 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 double powerOfTwoD(int n) {
    assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT);
    return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
                                    (DoubleConsts.SIGNIFICAND_WIDTH-1))
                                   & DoubleConsts.EXP_BIT_MASK);
}
 
Example #7
Source File: FpUtils.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a floating-point power of two in the normal range.
 */
static double powerOfTwoD(int n) {
    assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT);
    return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
                                    (DoubleConsts.SIGNIFICAND_WIDTH-1))
                                   & DoubleConsts.EXP_BIT_MASK);
}
 
Example #8
Source File: IeeeRecommendedTests.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static int testDoubleNextUp() {
    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.
     */
    double testCases [][] = {
        {NaNd,                      NaNd},
        {-infinityD,                -Double.MAX_VALUE},
        {-Double.MAX_VALUE,         -Double_MAX_VALUEmm},
        {-DoubleConsts.MIN_NORMAL,  -Double_MAX_SUBNORMAL},
        {-Double_MAX_SUBNORMAL,     -Double_MAX_SUBNORMALmm},
        {-Double.MIN_VALUE,         -0.0d},
        {-0.0d,                     Double.MIN_VALUE},
        {+0.0d,                     Double.MIN_VALUE},
        {Double.MIN_VALUE,          Double.MIN_VALUE*2},
        {Double_MAX_SUBNORMALmm,    Double_MAX_SUBNORMAL},
        {Double_MAX_SUBNORMAL,      DoubleConsts.MIN_NORMAL},
        {DoubleConsts.MIN_NORMAL,   DoubleConsts.MIN_NORMAL+Double.MIN_VALUE},
        {Double_MAX_VALUEmm,        Double.MAX_VALUE},
        {Double.MAX_VALUE,          infinityD},
        {infinityD,                 infinityD}
    };

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

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

    return failures;
}
 
Example #9
Source File: IeeeRecommendedTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static int testDoubleNextDown() {
    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.
     */
    double testCases [][] = {
        {NaNd,                      NaNd},
        {-infinityD,                -infinityD},
        {-Double.MAX_VALUE,         -infinityD},
        {-Double_MAX_VALUEmm,       -Double.MAX_VALUE},
        {-Double_MAX_SUBNORMAL,     -DoubleConsts.MIN_NORMAL},
        {-Double_MAX_SUBNORMALmm,   -Double_MAX_SUBNORMAL},
        {-0.0d,                     -Double.MIN_VALUE},
        {+0.0d,                     -Double.MIN_VALUE},
        {Double.MIN_VALUE,          0.0d},
        {Double.MIN_VALUE*2,        Double.MIN_VALUE},
        {Double_MAX_SUBNORMAL,      Double_MAX_SUBNORMALmm},
        {DoubleConsts.MIN_NORMAL,   Double_MAX_SUBNORMAL},
        {DoubleConsts.MIN_NORMAL+
         Double.MIN_VALUE,          DoubleConsts.MIN_NORMAL},
        {Double.MAX_VALUE,          Double_MAX_VALUEmm},
        {infinityD,                 Double.MAX_VALUE},
    };

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

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

    return failures;
}
 
Example #10
Source File: IeeeRecommendedTests.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static int testDoubleNextUp() {
    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.
     */
    double testCases [][] = {
        {NaNd,                      NaNd},
        {-infinityD,                -Double.MAX_VALUE},
        {-Double.MAX_VALUE,         -Double_MAX_VALUEmm},
        {-DoubleConsts.MIN_NORMAL,  -Double_MAX_SUBNORMAL},
        {-Double_MAX_SUBNORMAL,     -Double_MAX_SUBNORMALmm},
        {-Double.MIN_VALUE,         -0.0d},
        {-0.0d,                     Double.MIN_VALUE},
        {+0.0d,                     Double.MIN_VALUE},
        {Double.MIN_VALUE,          Double.MIN_VALUE*2},
        {Double_MAX_SUBNORMALmm,    Double_MAX_SUBNORMAL},
        {Double_MAX_SUBNORMAL,      DoubleConsts.MIN_NORMAL},
        {DoubleConsts.MIN_NORMAL,   DoubleConsts.MIN_NORMAL+Double.MIN_VALUE},
        {Double_MAX_VALUEmm,        Double.MAX_VALUE},
        {Double.MAX_VALUE,          infinityD},
        {infinityD,                 infinityD}
    };

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

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

    return failures;
}
 
Example #11
Source File: Math.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code long} 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 Long.MIN_VALUE}, the result is
 * equal to the value of {@code Long.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Long.MAX_VALUE}, the result is
 * equal to the value of {@code Long.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to a
 *          {@code long}.
 * @return  the value of the argument rounded to the nearest
 *          {@code long} value.
 * @see     java.lang.Long#MAX_VALUE
 * @see     java.lang.Long#MIN_VALUE
 */
public static long round(double a) {
    long longBits = Double.doubleToRawLongBits(a);
    long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
            >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
    long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
            + DoubleConsts.EXP_BIAS) - biasedExp;
    if ((shift & -64) == 0) { // shift >= 0 && shift < 64
        // a is a finite number such that pow(2,-64) <= ulp(a) < 1
        long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
                | (DoubleConsts.SIGNIF_BIT_MASK + 1));
        if (longBits < 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,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (long) a;
    }
}
 
Example #12
Source File: Math.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code long} 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 Long.MIN_VALUE}, the result is
 * equal to the value of {@code Long.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Long.MAX_VALUE}, the result is
 * equal to the value of {@code Long.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to a
 *          {@code long}.
 * @return  the value of the argument rounded to the nearest
 *          {@code long} value.
 * @see     java.lang.Long#MAX_VALUE
 * @see     java.lang.Long#MIN_VALUE
 */
public static long round(double a) {
    long longBits = Double.doubleToRawLongBits(a);
    long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
            >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
    long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
            + DoubleConsts.EXP_BIAS) - biasedExp;
    if ((shift & -64) == 0) { // shift >= 0 && shift < 64
        // a is a finite number such that pow(2,-64) <= ulp(a) < 1
        long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
                | (DoubleConsts.SIGNIF_BIT_MASK + 1));
        if (longBits < 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,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (long) a;
    }
}
 
Example #13
Source File: Math.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code long} 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 Long.MIN_VALUE}, the result is
 * equal to the value of {@code Long.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Long.MAX_VALUE}, the result is
 * equal to the value of {@code Long.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to a
 *          {@code long}.
 * @return  the value of the argument rounded to the nearest
 *          {@code long} value.
 * @see     java.lang.Long#MAX_VALUE
 * @see     java.lang.Long#MIN_VALUE
 */
public static long round(double a) {
    long longBits = Double.doubleToRawLongBits(a);
    long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
            >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
    long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
            + DoubleConsts.EXP_BIAS) - biasedExp;
    if ((shift & -64) == 0) { // shift >= 0 && shift < 64
        // a is a finite number such that pow(2,-64) <= ulp(a) < 1
        long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
                | (DoubleConsts.SIGNIF_BIT_MASK + 1));
        if (longBits < 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,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (long) a;
    }
}
 
Example #14
Source File: Math.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code long} 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 Long.MIN_VALUE}, the result is
 * equal to the value of {@code Long.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Long.MAX_VALUE}, the result is
 * equal to the value of {@code Long.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to a
 *          {@code long}.
 * @return  the value of the argument rounded to the nearest
 *          {@code long} value.
 * @see     java.lang.Long#MAX_VALUE
 * @see     java.lang.Long#MIN_VALUE
 */
public static long round(double a) {
    long longBits = Double.doubleToRawLongBits(a);
    long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
            >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
    long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
            + DoubleConsts.EXP_BIAS) - biasedExp;
    if ((shift & -64) == 0) { // shift >= 0 && shift < 64
        // a is a finite number such that pow(2,-64) <= ulp(a) < 1
        long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
                | (DoubleConsts.SIGNIF_BIT_MASK + 1));
        if (longBits < 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,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (long) a;
    }
}
 
Example #15
Source File: IeeeRecommendedTests.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static int testDoubleNextUp() {
    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.
     */
    double testCases [][] = {
        {NaNd,                      NaNd},
        {-infinityD,                -Double.MAX_VALUE},
        {-Double.MAX_VALUE,         -Double_MAX_VALUEmm},
        {-DoubleConsts.MIN_NORMAL,  -Double_MAX_SUBNORMAL},
        {-Double_MAX_SUBNORMAL,     -Double_MAX_SUBNORMALmm},
        {-Double.MIN_VALUE,         -0.0d},
        {-0.0d,                     Double.MIN_VALUE},
        {+0.0d,                     Double.MIN_VALUE},
        {Double.MIN_VALUE,          Double.MIN_VALUE*2},
        {Double_MAX_SUBNORMALmm,    Double_MAX_SUBNORMAL},
        {Double_MAX_SUBNORMAL,      DoubleConsts.MIN_NORMAL},
        {DoubleConsts.MIN_NORMAL,   DoubleConsts.MIN_NORMAL+Double.MIN_VALUE},
        {Double_MAX_VALUEmm,        Double.MAX_VALUE},
        {Double.MAX_VALUE,          infinityD},
        {infinityD,                 infinityD}
    };

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

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

    return failures;
}
 
Example #16
Source File: IeeeRecommendedTests.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static int testDoubleSignum() {
    int failures = 0;
    double testCases [][] = {
        {NaNd,                      NaNd},
        {-infinityD,                -1.0},
        {-Double.MAX_VALUE,         -1.0},
        {-DoubleConsts.MIN_NORMAL,  -1.0},
        {-1.0,                      -1.0},
        {-2.0,                      -1.0},
        {-Double_MAX_SUBNORMAL,     -1.0},
        {-Double.MIN_VALUE,         -1.0d},
        {-0.0d,                     -0.0d},
        {+0.0d,                     +0.0d},
        {Double.MIN_VALUE,           1.0},
        {Double_MAX_SUBNORMALmm,     1.0},
        {Double_MAX_SUBNORMAL,       1.0},
        {DoubleConsts.MIN_NORMAL,    1.0},
        {1.0,                        1.0},
        {2.0,                        1.0},
        {Double_MAX_VALUEmm,         1.0},
        {Double.MAX_VALUE,           1.0},
        {infinityD,                  1.0}
    };

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

    return failures;
}
 
Example #17
Source File: Math.java    From openjdk-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 double powerOfTwoD(int n) {
    assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT);
    return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
                                    (DoubleConsts.SIGNIFICAND_WIDTH-1))
                                   & DoubleConsts.EXP_BIT_MASK);
}
 
Example #18
Source File: IeeeRecommendedTests.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static int testDoubleSignum() {
    int failures = 0;
    double testCases [][] = {
        {NaNd,                      NaNd},
        {-infinityD,                -1.0},
        {-Double.MAX_VALUE,         -1.0},
        {-DoubleConsts.MIN_NORMAL,  -1.0},
        {-1.0,                      -1.0},
        {-2.0,                      -1.0},
        {-Double_MAX_SUBNORMAL,     -1.0},
        {-Double.MIN_VALUE,         -1.0d},
        {-0.0d,                     -0.0d},
        {+0.0d,                     +0.0d},
        {Double.MIN_VALUE,           1.0},
        {Double_MAX_SUBNORMALmm,     1.0},
        {Double_MAX_SUBNORMAL,       1.0},
        {DoubleConsts.MIN_NORMAL,    1.0},
        {1.0,                        1.0},
        {2.0,                        1.0},
        {Double_MAX_VALUEmm,         1.0},
        {Double.MAX_VALUE,           1.0},
        {infinityD,                  1.0}
    };

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

    return failures;
}
 
Example #19
Source File: FpUtils.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a floating-point power of two in the normal range.
 */
static double powerOfTwoD(int n) {
    assert(n >= DoubleConsts.MIN_EXPONENT && n <= DoubleConsts.MAX_EXPONENT);
    return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
                                    (DoubleConsts.SIGNIFICAND_WIDTH-1))
                                   & DoubleConsts.EXP_BIT_MASK);
}
 
Example #20
Source File: Math.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the closest {@code long} 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 Long.MIN_VALUE}, the result is
 * equal to the value of {@code Long.MIN_VALUE}.
 * <li>If the argument is positive infinity or any value greater than or
 * equal to the value of {@code Long.MAX_VALUE}, the result is
 * equal to the value of {@code Long.MAX_VALUE}.</ul>
 *
 * @param   a   a floating-point value to be rounded to a
 *          {@code long}.
 * @return  the value of the argument rounded to the nearest
 *          {@code long} value.
 * @see     java.lang.Long#MAX_VALUE
 * @see     java.lang.Long#MIN_VALUE
 */
public static long round(double a) {
    long longBits = Double.doubleToRawLongBits(a);
    long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
            >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
    long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
            + DoubleConsts.EXP_BIAS) - biasedExp;
    if ((shift & -64) == 0) { // shift >= 0 && shift < 64
        // a is a finite number such that pow(2,-64) <= ulp(a) < 1
        long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
                | (DoubleConsts.SIGNIF_BIT_MASK + 1));
        if (longBits < 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,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
        // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
        // - an infinity or NaN
        return (long) a;
    }
}
 
Example #21
Source File: ToHexString.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example #22
Source File: IeeeRecommendedTests.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static int testDoubleNextAfter() {
    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.
     */
    double [][] testCases  = {
        {NaNd,              NaNd,                   NaNd},
        {NaNd,              0.0d,                   NaNd},
        {0.0d,              NaNd,                   NaNd},
        {NaNd,              infinityD,              NaNd},
        {infinityD,         NaNd,                   NaNd},

        {infinityD,         infinityD,              infinityD},
        {infinityD,         -infinityD,             Double.MAX_VALUE},
        {infinityD,         0.0d,                   Double.MAX_VALUE},

        {Double.MAX_VALUE,  infinityD,              infinityD},
        {Double.MAX_VALUE,  -infinityD,             Double_MAX_VALUEmm},
        {Double.MAX_VALUE,  Double.MAX_VALUE,       Double.MAX_VALUE},
        {Double.MAX_VALUE,  0.0d,                   Double_MAX_VALUEmm},

        {Double_MAX_VALUEmm,        Double.MAX_VALUE,       Double.MAX_VALUE},
        {Double_MAX_VALUEmm,        infinityD,              Double.MAX_VALUE},
        {Double_MAX_VALUEmm,        Double_MAX_VALUEmm,     Double_MAX_VALUEmm},

        {DoubleConsts.MIN_NORMAL,   infinityD,              DoubleConsts.MIN_NORMAL+
                                                            Double.MIN_VALUE},
        {DoubleConsts.MIN_NORMAL,   -infinityD,             Double_MAX_SUBNORMAL},
        {DoubleConsts.MIN_NORMAL,   1.0f,                   DoubleConsts.MIN_NORMAL+
                                                            Double.MIN_VALUE},
        {DoubleConsts.MIN_NORMAL,   -1.0f,                  Double_MAX_SUBNORMAL},
        {DoubleConsts.MIN_NORMAL,   DoubleConsts.MIN_NORMAL,DoubleConsts.MIN_NORMAL},

        {Double_MAX_SUBNORMAL,      DoubleConsts.MIN_NORMAL,DoubleConsts.MIN_NORMAL},
        {Double_MAX_SUBNORMAL,      Double_MAX_SUBNORMAL,   Double_MAX_SUBNORMAL},
        {Double_MAX_SUBNORMAL,      0.0d,                   Double_MAX_SUBNORMALmm},

        {Double_MAX_SUBNORMALmm,    Double_MAX_SUBNORMAL,   Double_MAX_SUBNORMAL},
        {Double_MAX_SUBNORMALmm,    0.0d,                   Double_MAX_SUBNORMALmm-Double.MIN_VALUE},
        {Double_MAX_SUBNORMALmm,    Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMALmm},

        {Double.MIN_VALUE,  0.0d,                   0.0d},
        {-Double.MIN_VALUE, 0.0d,                   -0.0d},
        {Double.MIN_VALUE,  Double.MIN_VALUE,       Double.MIN_VALUE},
        {Double.MIN_VALUE,  1.0f,                   2*Double.MIN_VALUE},

        // Make sure zero behavior is tested
        {0.0d,              0.0d,                   0.0d},
        {0.0d,              -0.0d,                  -0.0d},
        {-0.0d,             0.0d,                   0.0d},
        {-0.0d,             -0.0d,                  -0.0d},
        {0.0d,              infinityD,              Double.MIN_VALUE},
        {0.0d,              -infinityD,             -Double.MIN_VALUE},
        {-0.0d,             infinityD,              Double.MIN_VALUE},
        {-0.0d,             -infinityD,             -Double.MIN_VALUE},
        {0.0d,              Double.MIN_VALUE,       Double.MIN_VALUE},
        {0.0d,              -Double.MIN_VALUE,      -Double.MIN_VALUE},
        {-0.0d,             Double.MIN_VALUE,       Double.MIN_VALUE},
        {-0.0d,             -Double.MIN_VALUE,      -Double.MIN_VALUE}
    };

    for(int i = 0; i < testCases.length; i++) {
        failures += testNextAfterCase(testCases[i][0], testCases[i][1],
                                      testCases[i][2]);
    }
    return failures;
}
 
Example #23
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 double}.  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 double}, it will be converted to
 * {@link Double#NEGATIVE_INFINITY} or {@link
 * Double#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 double}.
 */
public double doubleValue() {
    if (signum == 0) {
        return 0.0;
    }

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

    // exponent == floor(log2(abs(this))Double)
    if (exponent < Long.SIZE - 1) {
        return longValue();
    } else if (exponent > Double.MAX_EXPONENT) {
        return signum > 0 ? Double.POSITIVE_INFINITY : Double.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 - DoubleConsts.SIGNIFICAND_WIDTH;

    long twiceSignifFloor;
    // twiceSignifFloor will be == abs().shiftRight(shift).longValue()
    // We do the shift into a long directly to improve performance.

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

    int highBits;
    int lowBits;
    if (nBits == 0) {
        highBits = mag[0];
        lowBits = mag[1];
    } else {
        highBits = mag[0] >>> nBits;
        lowBits = (mag[0] << nBits2) | (mag[1] >>> nBits);
        if (highBits == 0) {
            highBits = lowBits;
            lowBits = (mag[1] << nBits2) | (mag[2] >>> nBits);
        }
    }

    twiceSignifFloor = ((highBits & LONG_MASK) << 32)
            | (lowBits & LONG_MASK);

    long signifFloor = twiceSignifFloor >> 1;
    signifFloor &= DoubleConsts.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);
    long signifRounded = increment ? signifFloor + 1 : signifFloor;
    long bits = (long) ((exponent + DoubleConsts.EXP_BIAS))
            << (DoubleConsts.SIGNIFICAND_WIDTH - 1);
    bits += signifRounded;
    /*
     * If signifRounded == 2^53, 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 Double.MAX_EXPONENT, we round up (correctly) to
     * Double.POSITIVE_INFINITY.
     */
    bits |= signum & DoubleConsts.SIGN_BIT_MASK;
    return Double.longBitsToDouble(bits);
}
 
Example #24
Source File: ToHexString.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example #25
Source File: BigInteger.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Converts this BigInteger to a {@code double}.  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 double}, it will be converted to
 * {@link Double#NEGATIVE_INFINITY} or {@link
 * Double#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 double}.
 */
public double doubleValue() {
    if (signum == 0) {
        return 0.0;
    }

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

    // exponent == floor(log2(abs(this))Double)
    if (exponent < Long.SIZE - 1) {
        return longValue();
    } else if (exponent > Double.MAX_EXPONENT) {
        return signum > 0 ? Double.POSITIVE_INFINITY : Double.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 - DoubleConsts.SIGNIFICAND_WIDTH;

    long twiceSignifFloor;
    // twiceSignifFloor will be == abs().shiftRight(shift).longValue()
    // We do the shift into a long directly to improve performance.

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

    int highBits;
    int lowBits;
    if (nBits == 0) {
        highBits = mag[0];
        lowBits = mag[1];
    } else {
        highBits = mag[0] >>> nBits;
        lowBits = (mag[0] << nBits2) | (mag[1] >>> nBits);
        if (highBits == 0) {
            highBits = lowBits;
            lowBits = (mag[1] << nBits2) | (mag[2] >>> nBits);
        }
    }

    twiceSignifFloor = ((highBits & LONG_MASK) << 32)
            | (lowBits & LONG_MASK);

    long signifFloor = twiceSignifFloor >> 1;
    signifFloor &= DoubleConsts.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);
    long signifRounded = increment ? signifFloor + 1 : signifFloor;
    long bits = (long) ((exponent + DoubleConsts.EXP_BIAS))
            << (DoubleConsts.SIGNIFICAND_WIDTH - 1);
    bits += signifRounded;
    /*
     * If signifRounded == 2^53, 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 Double.MAX_EXPONENT, we round up (correctly) to
     * Double.POSITIVE_INFINITY.
     */
    bits |= signum & DoubleConsts.SIGN_BIT_MASK;
    return Double.longBitsToDouble(bits);
}
 
Example #26
Source File: Float.java    From hottub 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 #27
Source File: IeeeRecommendedTests.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static int testDoubleCopySign() {
    int failures = 0;

    // testCases[0] are logically positive numbers;
    // testCases[1] are negative numbers.
    double testCases [][] = {
        {+0.0d,
         Double.MIN_VALUE,
         Double_MAX_SUBNORMALmm,
         Double_MAX_SUBNORMAL,
         DoubleConsts.MIN_NORMAL,
         1.0d,
         3.0d,
         Double_MAX_VALUEmm,
         Double.MAX_VALUE,
         infinityD,
        },
        {-infinityD,
         -Double.MAX_VALUE,
         -3.0d,
         -1.0d,
         -DoubleConsts.MIN_NORMAL,
         -Double_MAX_SUBNORMALmm,
         -Double_MAX_SUBNORMAL,
         -Double.MIN_VALUE,
         -0.0d}
    };

    double NaNs[] = {Double.longBitsToDouble(0x7ff8000000000000L),  // "positive" NaN
                     Double.longBitsToDouble(0xfff8000000000000L),  // "negative" NaN
                     Double.longBitsToDouble(0x7FF0000000000001L),
                     Double.longBitsToDouble(0xFFF0000000000001L),
                     Double.longBitsToDouble(0x7FF8555555555555L),
                     Double.longBitsToDouble(0xFFF8555555555555L),
                     Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),
                     Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),
                     Double.longBitsToDouble(0x7FFDeadBeef00000L),
                     Double.longBitsToDouble(0xFFFDeadBeef00000L),
                     Double.longBitsToDouble(0x7FFCafeBabe00000L),
                     Double.longBitsToDouble(0xFFFCafeBabe00000L)};

    // Tests shared between Math and StrictMath 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("MathcopySign(double,double)",
                                         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(double,double)",
                                         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 Math.copySign, NaN may effectively have either sign bit
    // while for StrictMath.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(double,double)",
                                     testCases[i][m], NaNs[j],
                                     StrictMath.copySign(testCases[i][m], NaNs[j]),
                                     Math.abs(testCases[i][m]) );
            }
        }
    }


    return failures;
}
 
Example #28
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>double</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 d floating-point number whose exponent is to be extracted
 * @return unbiased exponent of the argument.
 * @author Joseph D. Darcy
 */
public static int ilogb(double d) {
    int exponent = getExponent(d);

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

    case DoubleConsts.MIN_EXPONENT-1:       // zero or subnormal
        if(d == 0.0) {
            return -(1<<28);        // -(2^28)
        }
        else {
            long transducer = Double.doubleToRawLongBits(d);

            /*
             * To avoid causing slow arithmetic on subnormals,
             * the scaling to determine when d'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 &= DoubleConsts.SIGNIF_BIT_MASK;
            assert(transducer != 0L);

            // 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 <
                   (1L << (DoubleConsts.SIGNIFICAND_WIDTH - 1))) {
                transducer *= 2;
                exponent--;
            }
            exponent++;
            assert( exponent >=
                    DoubleConsts.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1) &&
                    exponent < DoubleConsts.MIN_EXPONENT);
            return exponent;
        }
    // break;

    default:
        assert( exponent >= DoubleConsts.MIN_EXPONENT &&
                exponent <= DoubleConsts.MAX_EXPONENT);
        return exponent;
    // break;
    }
}
 
Example #29
Source File: ToHexString.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
static String hexLongStringtoHexDoubleString(String transString) {
    transString = transString.toLowerCase();

    String zeros = "";
    StringBuffer result = new StringBuffer(24);

    for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
    transString = zeros + transString;

    // assert transString.length == 16;

        char topChar;
        // Extract sign
        if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
            result.append("-");
            // clear sign bit
            transString =
                Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
                transString.substring(1,16);
        }

        // check for NaN and infinity
        String signifString = transString.substring(3,16);

        if( transString.substring(0,3).equals("7ff") ) {
            if(signifString.equals("0000000000000")) {
                result.append("Infinity");
            }
            else
                result.append("NaN");
        }
        else { // finite value
            // Extract exponent
            int exponent = Integer.parseInt(transString.substring(0,3), 16) -
                DoubleConsts.EXP_BIAS;
            result.append("0x");

            if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
                if(signifString.equals("0000000000000")) {
                    result.append("0.0p0");
                }
                else {
                    result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                                  "p-1022");
                }
            }
            else {  // normal value
                result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
                              "p" + exponent);
            }
        }
        return result.toString();
}
 
Example #30
Source File: Float.java    From JDKSourceCode1.8 with MIT License 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);
}