jdk/test/java/lang/Math/IeeeRecommendedTests.java
changeset 10598 efd29b4b3e67
parent 5506 202f599c92aa
child 10608 7cfca36fc79b
equal deleted inserted replaced
10597:dac7cfce953a 10598:efd29b4b3e67
   175 
   175 
   176                 failures += testGetExponentCase(randFloat, i);
   176                 failures += testGetExponentCase(randFloat, i);
   177             }
   177             }
   178 
   178 
   179             if (i > FloatConsts.MIN_EXPONENT) {
   179             if (i > FloatConsts.MIN_EXPONENT) {
   180                 float po2minus = FpUtils.nextAfter(po2,
   180                 float po2minus = Math.nextAfter(po2,
   181                                                  Float.NEGATIVE_INFINITY);
   181                                                  Float.NEGATIVE_INFINITY);
   182                 failures += testGetExponentCase(po2minus, i-1);
   182                 failures += testGetExponentCase(po2minus, i-1);
   183             }
   183             }
   184         }
   184         }
   185 
   185 
   203                                             FloatConsts.MIN_EXPONENT - 1);
   203                                             FloatConsts.MIN_EXPONENT - 1);
   204 
   204 
   205             // Test largest value in next smaller binade
   205             // Test largest value in next smaller binade
   206             if (i >= 3) {// (i == 1) would test 0.0;
   206             if (i >= 3) {// (i == 1) would test 0.0;
   207                          // (i == 2) would just retest MIN_VALUE
   207                          // (i == 2) would just retest MIN_VALUE
   208                 testGetExponentCase(FpUtils.nextAfter(top, 0.0f),
   208                 testGetExponentCase(Math.nextAfter(top, 0.0f),
   209                                     FloatConsts.MIN_EXPONENT - 1);
   209                                     FloatConsts.MIN_EXPONENT - 1);
   210 
   210 
   211                 if( i >= 10) {
   211                 if( i >= 10) {
   212                     // create a bit mask with (i-1) 1's in the low order
   212                     // create a bit mask with (i-1) 1's in the low order
   213                     // bits
   213                     // bits
   282 
   282 
   283                 failures += testGetExponentCase(randFloat, i);
   283                 failures += testGetExponentCase(randFloat, i);
   284             }
   284             }
   285 
   285 
   286             if (i > DoubleConsts.MIN_EXPONENT) {
   286             if (i > DoubleConsts.MIN_EXPONENT) {
   287                 double po2minus = FpUtils.nextAfter(po2,
   287                 double po2minus = Math.nextAfter(po2,
   288                                                     Double.NEGATIVE_INFINITY);
   288                                                     Double.NEGATIVE_INFINITY);
   289                 failures += testGetExponentCase(po2minus, i-1);
   289                 failures += testGetExponentCase(po2minus, i-1);
   290             }
   290             }
   291         }
   291         }
   292 
   292 
   310                                             DoubleConsts.MIN_EXPONENT - 1);
   310                                             DoubleConsts.MIN_EXPONENT - 1);
   311 
   311 
   312             // Test largest value in next smaller binade
   312             // Test largest value in next smaller binade
   313             if (i >= 3) {// (i == 1) would test 0.0;
   313             if (i >= 3) {// (i == 1) would test 0.0;
   314                          // (i == 2) would just retest MIN_VALUE
   314                          // (i == 2) would just retest MIN_VALUE
   315                 testGetExponentCase(FpUtils.nextAfter(top, 0.0),
   315                 testGetExponentCase(Math.nextAfter(top, 0.0),
   316                                     DoubleConsts.MIN_EXPONENT - 1);
   316                                     DoubleConsts.MIN_EXPONENT - 1);
   317 
   317 
   318                 if( i >= 10) {
   318                 if( i >= 10) {
   319                     // create a bit mask with (i-1) 1's in the low order
   319                     // create a bit mask with (i-1) 1's in the low order
   320                     // bits
   320                     // bits
  1059                 int scaleFactor = manyScalingFactors[j];
  1059                 int scaleFactor = manyScalingFactors[j];
  1060                 if (Math.abs(scaleFactor) >= MAX_SCALE) {
  1060                 if (Math.abs(scaleFactor) >= MAX_SCALE) {
  1061                     float value = someTestCases[i];
  1061                     float value = someTestCases[i];
  1062                     failures+=testScalbCase(value,
  1062                     failures+=testScalbCase(value,
  1063                                             scaleFactor,
  1063                                             scaleFactor,
  1064                                             FpUtils.copySign( (scaleFactor>0?infinityF:0.0f), value) );
  1064                                             Math.copySign( (scaleFactor>0?infinityF:0.0f), value) );
  1065                 }
  1065                 }
  1066             }
  1066             }
  1067         }
  1067         }
  1068 
  1068 
  1069         // Test cases that could be done with one floating-point
  1069         // Test cases that could be done with one floating-point
  1093                 int scaleFactor = j;
  1093                 int scaleFactor = j;
  1094 
  1094 
  1095                 failures+=testScalbCase(value,
  1095                 failures+=testScalbCase(value,
  1096                                         scaleFactor,
  1096                                         scaleFactor,
  1097                                         (FpUtils.ilogb(value) +j > FloatConsts.MAX_EXPONENT ) ?
  1097                                         (FpUtils.ilogb(value) +j > FloatConsts.MAX_EXPONENT ) ?
  1098                                         FpUtils.copySign(infinityF, value) : // overflow
  1098                                         Math.copySign(infinityF, value) : // overflow
  1099                                         // calculate right answer
  1099                                         // calculate right answer
  1100                                         twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) );
  1100                                         twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) );
  1101                 scale*=2.0f;
  1101                 scale*=2.0f;
  1102             }
  1102             }
  1103         }
  1103         }
  1266                 int scaleFactor = manyScalingFactors[j];
  1266                 int scaleFactor = manyScalingFactors[j];
  1267                 if (Math.abs(scaleFactor) >= MAX_SCALE) {
  1267                 if (Math.abs(scaleFactor) >= MAX_SCALE) {
  1268                     double value = someTestCases[i];
  1268                     double value = someTestCases[i];
  1269                     failures+=testScalbCase(value,
  1269                     failures+=testScalbCase(value,
  1270                                             scaleFactor,
  1270                                             scaleFactor,
  1271                                             FpUtils.copySign( (scaleFactor>0?infinityD:0.0), value) );
  1271                                             Math.copySign( (scaleFactor>0?infinityD:0.0), value) );
  1272                 }
  1272                 }
  1273             }
  1273             }
  1274         }
  1274         }
  1275 
  1275 
  1276         // Test cases that could be done with one floating-point
  1276         // Test cases that could be done with one floating-point
  1300                 int scaleFactor = j;
  1300                 int scaleFactor = j;
  1301 
  1301 
  1302                 failures+=testScalbCase(value,
  1302                 failures+=testScalbCase(value,
  1303                                         scaleFactor,
  1303                                         scaleFactor,
  1304                                         (FpUtils.ilogb(value) +j > DoubleConsts.MAX_EXPONENT ) ?
  1304                                         (FpUtils.ilogb(value) +j > DoubleConsts.MAX_EXPONENT ) ?
  1305                                         FpUtils.copySign(infinityD, value) : // overflow
  1305                                         Math.copySign(infinityD, value) : // overflow
  1306                                         // calculate right answer
  1306                                         // calculate right answer
  1307                                         twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) );
  1307                                         twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) );
  1308                 scale*=2.0;
  1308                 scale*=2.0;
  1309             }
  1309             }
  1310         }
  1310         }
  1421         for(int i = FloatConsts.MIN_EXPONENT; i <= FloatConsts.MAX_EXPONENT; i++) {
  1421         for(int i = FloatConsts.MIN_EXPONENT; i <= FloatConsts.MAX_EXPONENT; i++) {
  1422             float expected;
  1422             float expected;
  1423 
  1423 
  1424             // Create power of two
  1424             // Create power of two
  1425             float po2 = powerOfTwoF(i);
  1425             float po2 = powerOfTwoF(i);
  1426             expected = FpUtils.scalb(1.0f, i - (FloatConsts.SIGNIFICAND_WIDTH-1));
  1426             expected = Math.scalb(1.0f, i - (FloatConsts.SIGNIFICAND_WIDTH-1));
  1427 
  1427 
  1428             failures += testUlpCase(po2, expected);
  1428             failures += testUlpCase(po2, expected);
  1429 
  1429 
  1430             // Generate some random bit patterns for the significand
  1430             // Generate some random bit patterns for the significand
  1431             for(int j = 0; j < 10; j++) {
  1431             for(int j = 0; j < 10; j++) {
  1441 
  1441 
  1442                 failures += testUlpCase(randFloat, expected);
  1442                 failures += testUlpCase(randFloat, expected);
  1443             }
  1443             }
  1444 
  1444 
  1445             if (i > FloatConsts.MIN_EXPONENT) {
  1445             if (i > FloatConsts.MIN_EXPONENT) {
  1446                 float po2minus = FpUtils.nextAfter(po2,
  1446                 float po2minus = Math.nextAfter(po2,
  1447                                                    Float.NEGATIVE_INFINITY);
  1447                                                    Float.NEGATIVE_INFINITY);
  1448                 failures += testUlpCase(po2minus, expected/2.0f);
  1448                 failures += testUlpCase(po2minus, expected/2.0f);
  1449             }
  1449             }
  1450         }
  1450         }
  1451 
  1451 
  1468             failures += testUlpCase(top, Float.MIN_VALUE);
  1468             failures += testUlpCase(top, Float.MIN_VALUE);
  1469 
  1469 
  1470             // Test largest value in next smaller binade
  1470             // Test largest value in next smaller binade
  1471             if (i >= 3) {// (i == 1) would test 0.0;
  1471             if (i >= 3) {// (i == 1) would test 0.0;
  1472                          // (i == 2) would just retest MIN_VALUE
  1472                          // (i == 2) would just retest MIN_VALUE
  1473                 testUlpCase(FpUtils.nextAfter(top, 0.0f),
  1473                 testUlpCase(Math.nextAfter(top, 0.0f),
  1474                             Float.MIN_VALUE);
  1474                             Float.MIN_VALUE);
  1475 
  1475 
  1476                 if( i >= 10) {
  1476                 if( i >= 10) {
  1477                     // create a bit mask with (i-1) 1's in the low order
  1477                     // create a bit mask with (i-1) 1's in the low order
  1478                     // bits
  1478                     // bits
  1526         for(int i = DoubleConsts.MIN_EXPONENT; i <= DoubleConsts.MAX_EXPONENT; i++) {
  1526         for(int i = DoubleConsts.MIN_EXPONENT; i <= DoubleConsts.MAX_EXPONENT; i++) {
  1527             double expected;
  1527             double expected;
  1528 
  1528 
  1529             // Create power of two
  1529             // Create power of two
  1530             double po2 = powerOfTwoD(i);
  1530             double po2 = powerOfTwoD(i);
  1531             expected = FpUtils.scalb(1.0, i - (DoubleConsts.SIGNIFICAND_WIDTH-1));
  1531             expected = Math.scalb(1.0, i - (DoubleConsts.SIGNIFICAND_WIDTH-1));
  1532 
  1532 
  1533             failures += testUlpCase(po2, expected);
  1533             failures += testUlpCase(po2, expected);
  1534 
  1534 
  1535             // Generate some random bit patterns for the significand
  1535             // Generate some random bit patterns for the significand
  1536             for(int j = 0; j < 10; j++) {
  1536             for(int j = 0; j < 10; j++) {
  1546 
  1546 
  1547                 failures += testUlpCase(randDouble, expected);
  1547                 failures += testUlpCase(randDouble, expected);
  1548             }
  1548             }
  1549 
  1549 
  1550             if (i > DoubleConsts.MIN_EXPONENT) {
  1550             if (i > DoubleConsts.MIN_EXPONENT) {
  1551                 double po2minus = FpUtils.nextAfter(po2,
  1551                 double po2minus = Math.nextAfter(po2,
  1552                                                     Double.NEGATIVE_INFINITY);
  1552                                                     Double.NEGATIVE_INFINITY);
  1553                 failures += testUlpCase(po2minus, expected/2.0f);
  1553                 failures += testUlpCase(po2minus, expected/2.0f);
  1554             }
  1554             }
  1555         }
  1555         }
  1556 
  1556 
  1573             failures += testUlpCase(top, Double.MIN_VALUE);
  1573             failures += testUlpCase(top, Double.MIN_VALUE);
  1574 
  1574 
  1575             // Test largest value in next smaller binade
  1575             // Test largest value in next smaller binade
  1576             if (i >= 3) {// (i == 1) would test 0.0;
  1576             if (i >= 3) {// (i == 1) would test 0.0;
  1577                          // (i == 2) would just retest MIN_VALUE
  1577                          // (i == 2) would just retest MIN_VALUE
  1578                 testUlpCase(FpUtils.nextAfter(top, 0.0f),
  1578                 testUlpCase(Math.nextAfter(top, 0.0f),
  1579                             Double.MIN_VALUE);
  1579                             Double.MIN_VALUE);
  1580 
  1580 
  1581                 if( i >= 10) {
  1581                 if( i >= 10) {
  1582                     // create a bit mask with (i-1) 1's in the low order
  1582                     // create a bit mask with (i-1) 1's in the low order
  1583                     // bits
  1583                     // bits