11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4860891 4826732 4780454 4939441 4826652 27 * @summary Tests for IEEE 754[R] recommended functions and similar methods 28 * @author Joseph D. Darcy 29 */ 30 31 import sun.misc.DoubleConsts; 32 import sun.misc.FloatConsts; 33 34 public class IeeeRecommendedTests { 35 private IeeeRecommendedTests(){} 36 37 static final float NaNf = Float.NaN; 38 static final double NaNd = Double.NaN; 39 static final float infinityF = Float.POSITIVE_INFINITY; 40 static final double infinityD = Double.POSITIVE_INFINITY; 41 42 static final float Float_MAX_VALUEmm = 0x1.fffffcP+127f; 43 static final float Float_MAX_SUBNORMAL = 0x0.fffffeP-126f; 44 static final float Float_MAX_SUBNORMALmm = 0x0.fffffcP-126f; 45 46 static final double Double_MAX_VALUEmm = 0x1.ffffffffffffeP+1023; 47 static final double Double_MAX_SUBNORMAL = 0x0.fffffffffffffP-1022; 48 static final double Double_MAX_SUBNORMALmm = 0x0.ffffffffffffeP-1022; 49 50 // Initialize shared random number generator 51 static java.util.Random rand = new java.util.Random(); 52 53 /** 54 * Returns a floating-point power of two in the normal range. 55 */ 56 static double powerOfTwoD(int n) { 57 return Double.longBitsToDouble((((long)n + (long)DoubleConsts.MAX_EXPONENT) << 58 (DoubleConsts.SIGNIFICAND_WIDTH-1)) 59 & DoubleConsts.EXP_BIT_MASK); 60 } 61 62 /** 63 * Returns a floating-point power of two in the normal range. 64 */ 65 static float powerOfTwoF(int n) { 66 return Float.intBitsToFloat(((n + FloatConsts.MAX_EXPONENT) << 67 (FloatConsts.SIGNIFICAND_WIDTH-1)) 68 & FloatConsts.EXP_BIT_MASK); 69 } 70 71 /* ******************** getExponent tests ****************************** */ 72 73 /* 74 * The tests for getExponent should test the special values (NaN, +/- 75 * infinity, etc.), test the endpoints of each binade (set of 76 * floating-point values with the same exponent), and for good 77 * measure, test some random values within each binade. Testing 78 * the endpoints of each binade includes testing both positive and 79 * negative numbers. Subnormal values with different normalized 80 * exponents should be tested too. Both Math and StrictMath 81 * methods should return the same results. 82 */ 83 84 /* 85 * Test Math.getExponent and StrictMath.getExponent with +d and -d. 86 */ 112 failures+=Tests.test("Math.getExponent(double)", minus_d, 113 Math.getExponent(minus_d), expected); 114 115 failures+=Tests.test("StrictMath.getExponent(double)", d, 116 StrictMath.getExponent(d), expected); 117 failures+=Tests.test("StrictMath.getExponent(double)", minus_d, 118 StrictMath.getExponent(minus_d), expected); 119 return failures; 120 } 121 122 public static int testFloatGetExponent() { 123 int failures = 0; 124 float [] specialValues = {NaNf, 125 Float.POSITIVE_INFINITY, 126 +0.0f, 127 +1.0f, 128 +2.0f, 129 +16.0f, 130 +Float.MIN_VALUE, 131 +Float_MAX_SUBNORMAL, 132 +FloatConsts.MIN_NORMAL, 133 +Float.MAX_VALUE 134 }; 135 136 int [] specialResults = {Float.MAX_EXPONENT + 1, // NaN results 137 Float.MAX_EXPONENT + 1, // Infinite results 138 Float.MIN_EXPONENT - 1, // Zero results 139 0, 140 1, 141 4, 142 FloatConsts.MIN_EXPONENT - 1, 143 -FloatConsts.MAX_EXPONENT, 144 FloatConsts.MIN_EXPONENT, 145 FloatConsts.MAX_EXPONENT 146 }; 147 148 // Special value tests 149 for(int i = 0; i < specialValues.length; i++) { 150 failures += testGetExponentCase(specialValues[i], specialResults[i]); 151 } 152 153 154 // Normal exponent tests 155 for(int i = FloatConsts.MIN_EXPONENT; i <= FloatConsts.MAX_EXPONENT; i++) { 156 int result; 157 158 // Create power of two 159 float po2 = powerOfTwoF(i); 160 161 failures += testGetExponentCase(po2, i); 162 163 // Generate some random bit patterns for the significand 164 for(int j = 0; j < 10; j++) { 165 int randSignif = rand.nextInt(); 166 float randFloat; 167 168 randFloat = Float.intBitsToFloat( // Exponent 169 (Float.floatToIntBits(po2)& 170 (~FloatConsts.SIGNIF_BIT_MASK)) | 171 // Significand 172 (randSignif & 173 FloatConsts.SIGNIF_BIT_MASK) ); 174 175 failures += testGetExponentCase(randFloat, i); 176 } 177 178 if (i > FloatConsts.MIN_EXPONENT) { 179 float po2minus = Math.nextAfter(po2, 180 Float.NEGATIVE_INFINITY); 181 failures += testGetExponentCase(po2minus, i-1); 182 } 183 } 184 185 // Subnormal exponent tests 186 187 /* 188 * Start with MIN_VALUE, left shift, test high value, low 189 * values, and random in between. 190 * 191 * Use nextAfter to calculate, high value of previous binade, 192 * loop count i will indicate how many random bits, if any are 193 * needed. 194 */ 195 196 float top=Float.MIN_VALUE; 197 for( int i = 1; 198 i < FloatConsts.SIGNIFICAND_WIDTH; 199 i++, top *= 2.0f) { 200 201 failures += testGetExponentCase(top, 202 FloatConsts.MIN_EXPONENT - 1); 203 204 // Test largest value in next smaller binade 205 if (i >= 3) {// (i == 1) would test 0.0; 206 // (i == 2) would just retest MIN_VALUE 207 testGetExponentCase(Math.nextAfter(top, 0.0f), 208 FloatConsts.MIN_EXPONENT - 1); 209 210 if( i >= 10) { 211 // create a bit mask with (i-1) 1's in the low order 212 // bits 213 int mask = ~((~0)<<(i-1)); 214 float randFloat = Float.intBitsToFloat( // Exponent 215 Float.floatToIntBits(top) | 216 // Significand 217 (rand.nextInt() & mask ) ) ; 218 219 failures += testGetExponentCase(randFloat, 220 FloatConsts.MIN_EXPONENT - 1); 221 } 222 } 223 } 224 225 return failures; 226 } 227 228 229 public static int testDoubleGetExponent() { 230 int failures = 0; 231 double [] specialValues = {NaNd, 232 infinityD, 233 +0.0, 234 +1.0, 235 +2.0, 236 +16.0, 237 +Double.MIN_VALUE, 238 +Double_MAX_SUBNORMAL, 239 +DoubleConsts.MIN_NORMAL, 240 +Double.MAX_VALUE 241 }; 242 243 int [] specialResults = {Double.MAX_EXPONENT + 1, // NaN results 244 Double.MAX_EXPONENT + 1, // Infinite results 245 Double.MIN_EXPONENT - 1, // Zero results 246 0, 247 1, 248 4, 249 DoubleConsts.MIN_EXPONENT - 1, 250 -DoubleConsts.MAX_EXPONENT, 251 DoubleConsts.MIN_EXPONENT, 252 DoubleConsts.MAX_EXPONENT 253 }; 254 255 // Special value tests 256 for(int i = 0; i < specialValues.length; i++) { 257 failures += testGetExponentCase(specialValues[i], specialResults[i]); 258 } 259 260 261 // Normal exponent tests 262 for(int i = DoubleConsts.MIN_EXPONENT; i <= DoubleConsts.MAX_EXPONENT; i++) { 263 int result; 264 265 // Create power of two 266 double po2 = powerOfTwoD(i); 267 268 failures += testGetExponentCase(po2, i); 269 270 // Generate some random bit patterns for the significand 271 for(int j = 0; j < 10; j++) { 272 long randSignif = rand.nextLong(); 273 double randFloat; 274 275 randFloat = Double.longBitsToDouble( // Exponent 276 (Double.doubleToLongBits(po2)& 277 (~DoubleConsts.SIGNIF_BIT_MASK)) | 278 // Significand 279 (randSignif & 280 DoubleConsts.SIGNIF_BIT_MASK) ); 281 282 failures += testGetExponentCase(randFloat, i); 283 } 284 285 if (i > DoubleConsts.MIN_EXPONENT) { 286 double po2minus = Math.nextAfter(po2, 287 Double.NEGATIVE_INFINITY); 288 failures += testGetExponentCase(po2minus, i-1); 289 } 290 } 291 292 // Subnormal exponent tests 293 294 /* 295 * Start with MIN_VALUE, left shift, test high value, low 296 * values, and random in between. 297 * 298 * Use nextAfter to calculate, high value of previous binade; 299 * loop count i will indicate how many random bits, if any are 300 * needed. 301 */ 302 303 double top=Double.MIN_VALUE; 304 for( int i = 1; 305 i < DoubleConsts.SIGNIFICAND_WIDTH; 306 i++, top *= 2.0f) { 307 308 failures += testGetExponentCase(top, 309 DoubleConsts.MIN_EXPONENT - 1); 310 311 // Test largest value in next smaller binade 312 if (i >= 3) {// (i == 1) would test 0.0; 313 // (i == 2) would just retest MIN_VALUE 314 testGetExponentCase(Math.nextAfter(top, 0.0), 315 DoubleConsts.MIN_EXPONENT - 1); 316 317 if( i >= 10) { 318 // create a bit mask with (i-1) 1's in the low order 319 // bits 320 long mask = ~((~0L)<<(i-1)); 321 double randFloat = Double.longBitsToDouble( // Exponent 322 Double.doubleToLongBits(top) | 323 // Significand 324 (rand.nextLong() & mask ) ) ; 325 326 failures += testGetExponentCase(randFloat, 327 DoubleConsts.MIN_EXPONENT - 1); 328 } 329 } 330 } 331 332 return failures; 333 } 334 335 336 /* ******************** nextAfter tests ****************************** */ 337 338 static int testNextAfterCase(float start, double direction, float expected) { 339 int failures=0; 340 float minus_start = -start; 341 double minus_direction = -direction; 342 float minus_expected = -expected; 343 344 failures+=Tests.test("Math.nextAfter(float,double)", start, direction, 345 Math.nextAfter(start, direction), expected); 346 failures+=Tests.test("Math.nextAfter(float,double)", minus_start, minus_direction, 347 Math.nextAfter(minus_start, minus_direction), minus_expected); 383 float [][] testCases = { 384 {NaNf, NaNf, NaNf}, 385 {NaNf, 0.0f, NaNf}, 386 {0.0f, NaNf, NaNf}, 387 {NaNf, infinityF, NaNf}, 388 {infinityF, NaNf, NaNf}, 389 390 {infinityF, infinityF, infinityF}, 391 {infinityF, -infinityF, Float.MAX_VALUE}, 392 {infinityF, 0.0f, Float.MAX_VALUE}, 393 394 {Float.MAX_VALUE, infinityF, infinityF}, 395 {Float.MAX_VALUE, -infinityF, Float_MAX_VALUEmm}, 396 {Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE}, 397 {Float.MAX_VALUE, 0.0f, Float_MAX_VALUEmm}, 398 399 {Float_MAX_VALUEmm, Float.MAX_VALUE, Float.MAX_VALUE}, 400 {Float_MAX_VALUEmm, infinityF, Float.MAX_VALUE}, 401 {Float_MAX_VALUEmm, Float_MAX_VALUEmm, Float_MAX_VALUEmm}, 402 403 {FloatConsts.MIN_NORMAL, infinityF, FloatConsts.MIN_NORMAL+ 404 Float.MIN_VALUE}, 405 {FloatConsts.MIN_NORMAL, -infinityF, Float_MAX_SUBNORMAL}, 406 {FloatConsts.MIN_NORMAL, 1.0f, FloatConsts.MIN_NORMAL+ 407 Float.MIN_VALUE}, 408 {FloatConsts.MIN_NORMAL, -1.0f, Float_MAX_SUBNORMAL}, 409 {FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL}, 410 411 {Float_MAX_SUBNORMAL, FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL}, 412 {Float_MAX_SUBNORMAL, Float_MAX_SUBNORMAL, Float_MAX_SUBNORMAL}, 413 {Float_MAX_SUBNORMAL, 0.0f, Float_MAX_SUBNORMALmm}, 414 415 {Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMAL, Float_MAX_SUBNORMAL}, 416 {Float_MAX_SUBNORMALmm, 0.0f, Float_MAX_SUBNORMALmm-Float.MIN_VALUE}, 417 {Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMALmm}, 418 419 {Float.MIN_VALUE, 0.0f, 0.0f}, 420 {-Float.MIN_VALUE, 0.0f, -0.0f}, 421 {Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE}, 422 {Float.MIN_VALUE, 1.0f, 2*Float.MIN_VALUE}, 423 424 // Make sure zero behavior is tested 425 {0.0f, 0.0f, 0.0f}, 426 {0.0f, -0.0f, -0.0f}, 427 {-0.0f, 0.0f, 0.0f}, 428 {-0.0f, -0.0f, -0.0f}, 429 {0.0f, infinityF, Float.MIN_VALUE}, 430 {0.0f, -infinityF, -Float.MIN_VALUE}, 431 {-0.0f, infinityF, Float.MIN_VALUE}, 455 double [][] testCases = { 456 {NaNd, NaNd, NaNd}, 457 {NaNd, 0.0d, NaNd}, 458 {0.0d, NaNd, NaNd}, 459 {NaNd, infinityD, NaNd}, 460 {infinityD, NaNd, NaNd}, 461 462 {infinityD, infinityD, infinityD}, 463 {infinityD, -infinityD, Double.MAX_VALUE}, 464 {infinityD, 0.0d, Double.MAX_VALUE}, 465 466 {Double.MAX_VALUE, infinityD, infinityD}, 467 {Double.MAX_VALUE, -infinityD, Double_MAX_VALUEmm}, 468 {Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE}, 469 {Double.MAX_VALUE, 0.0d, Double_MAX_VALUEmm}, 470 471 {Double_MAX_VALUEmm, Double.MAX_VALUE, Double.MAX_VALUE}, 472 {Double_MAX_VALUEmm, infinityD, Double.MAX_VALUE}, 473 {Double_MAX_VALUEmm, Double_MAX_VALUEmm, Double_MAX_VALUEmm}, 474 475 {DoubleConsts.MIN_NORMAL, infinityD, DoubleConsts.MIN_NORMAL+ 476 Double.MIN_VALUE}, 477 {DoubleConsts.MIN_NORMAL, -infinityD, Double_MAX_SUBNORMAL}, 478 {DoubleConsts.MIN_NORMAL, 1.0f, DoubleConsts.MIN_NORMAL+ 479 Double.MIN_VALUE}, 480 {DoubleConsts.MIN_NORMAL, -1.0f, Double_MAX_SUBNORMAL}, 481 {DoubleConsts.MIN_NORMAL, DoubleConsts.MIN_NORMAL,DoubleConsts.MIN_NORMAL}, 482 483 {Double_MAX_SUBNORMAL, DoubleConsts.MIN_NORMAL,DoubleConsts.MIN_NORMAL}, 484 {Double_MAX_SUBNORMAL, Double_MAX_SUBNORMAL, Double_MAX_SUBNORMAL}, 485 {Double_MAX_SUBNORMAL, 0.0d, Double_MAX_SUBNORMALmm}, 486 487 {Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMAL, Double_MAX_SUBNORMAL}, 488 {Double_MAX_SUBNORMALmm, 0.0d, Double_MAX_SUBNORMALmm-Double.MIN_VALUE}, 489 {Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMALmm}, 490 491 {Double.MIN_VALUE, 0.0d, 0.0d}, 492 {-Double.MIN_VALUE, 0.0d, -0.0d}, 493 {Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE}, 494 {Double.MIN_VALUE, 1.0f, 2*Double.MIN_VALUE}, 495 496 // Make sure zero behavior is tested 497 {0.0d, 0.0d, 0.0d}, 498 {0.0d, -0.0d, -0.0d}, 499 {-0.0d, 0.0d, 0.0d}, 500 {-0.0d, -0.0d, -0.0d}, 501 {0.0d, infinityD, Double.MIN_VALUE}, 502 {0.0d, -infinityD, -Double.MIN_VALUE}, 503 {-0.0d, infinityD, Double.MIN_VALUE}, 512 failures += testNextAfterCase(testCases[i][0], testCases[i][1], 513 testCases[i][2]); 514 } 515 return failures; 516 } 517 518 /* ******************** nextUp tests ********************************* */ 519 520 public static int testFloatNextUp() { 521 int failures=0; 522 523 /* 524 * Each row of testCases represents one test case for nextUp; 525 * the first column is the input and the second column is the 526 * expected result. 527 */ 528 float testCases [][] = { 529 {NaNf, NaNf}, 530 {-infinityF, -Float.MAX_VALUE}, 531 {-Float.MAX_VALUE, -Float_MAX_VALUEmm}, 532 {-FloatConsts.MIN_NORMAL, -Float_MAX_SUBNORMAL}, 533 {-Float_MAX_SUBNORMAL, -Float_MAX_SUBNORMALmm}, 534 {-Float.MIN_VALUE, -0.0f}, 535 {-0.0f, Float.MIN_VALUE}, 536 {+0.0f, Float.MIN_VALUE}, 537 {Float.MIN_VALUE, Float.MIN_VALUE*2}, 538 {Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMAL}, 539 {Float_MAX_SUBNORMAL, FloatConsts.MIN_NORMAL}, 540 {FloatConsts.MIN_NORMAL, FloatConsts.MIN_NORMAL+Float.MIN_VALUE}, 541 {Float_MAX_VALUEmm, Float.MAX_VALUE}, 542 {Float.MAX_VALUE, infinityF}, 543 {infinityF, infinityF} 544 }; 545 546 for(int i = 0; i < testCases.length; i++) { 547 failures+=Tests.test("Math.nextUp(float)", 548 testCases[i][0], Math.nextUp(testCases[i][0]), testCases[i][1]); 549 550 failures+=Tests.test("StrictMath.nextUp(float)", 551 testCases[i][0], StrictMath.nextUp(testCases[i][0]), testCases[i][1]); 552 } 553 554 return failures; 555 } 556 557 558 public static int testDoubleNextUp() { 559 int failures=0; 560 561 /* 562 * Each row of testCases represents one test case for nextUp; 563 * the first column is the input and the second column is the 564 * expected result. 565 */ 566 double testCases [][] = { 567 {NaNd, NaNd}, 568 {-infinityD, -Double.MAX_VALUE}, 569 {-Double.MAX_VALUE, -Double_MAX_VALUEmm}, 570 {-DoubleConsts.MIN_NORMAL, -Double_MAX_SUBNORMAL}, 571 {-Double_MAX_SUBNORMAL, -Double_MAX_SUBNORMALmm}, 572 {-Double.MIN_VALUE, -0.0d}, 573 {-0.0d, Double.MIN_VALUE}, 574 {+0.0d, Double.MIN_VALUE}, 575 {Double.MIN_VALUE, Double.MIN_VALUE*2}, 576 {Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMAL}, 577 {Double_MAX_SUBNORMAL, DoubleConsts.MIN_NORMAL}, 578 {DoubleConsts.MIN_NORMAL, DoubleConsts.MIN_NORMAL+Double.MIN_VALUE}, 579 {Double_MAX_VALUEmm, Double.MAX_VALUE}, 580 {Double.MAX_VALUE, infinityD}, 581 {infinityD, infinityD} 582 }; 583 584 for(int i = 0; i < testCases.length; i++) { 585 failures+=Tests.test("Math.nextUp(double)", 586 testCases[i][0], Math.nextUp(testCases[i][0]), testCases[i][1]); 587 588 failures+=Tests.test("StrictMath.nextUp(double)", 589 testCases[i][0], StrictMath.nextUp(testCases[i][0]), testCases[i][1]); 590 } 591 592 return failures; 593 } 594 595 /* ******************** nextDown tests ********************************* */ 596 597 public static int testFloatNextDown() { 598 int failures=0; 599 600 /* 601 * Each row of testCases represents one test case for nextDown; 602 * the first column is the input and the second column is the 603 * expected result. 604 */ 605 float testCases [][] = { 606 {NaNf, NaNf}, 607 {-infinityF, -infinityF}, 608 {-Float.MAX_VALUE, -infinityF}, 609 {-Float_MAX_VALUEmm, -Float.MAX_VALUE}, 610 {-Float_MAX_SUBNORMAL, -FloatConsts.MIN_NORMAL}, 611 {-Float_MAX_SUBNORMALmm, -Float_MAX_SUBNORMAL}, 612 {-0.0f, -Float.MIN_VALUE}, 613 {+0.0f, -Float.MIN_VALUE}, 614 {Float.MIN_VALUE, 0.0f}, 615 {Float.MIN_VALUE*2, Float.MIN_VALUE}, 616 {Float_MAX_SUBNORMAL, Float_MAX_SUBNORMALmm}, 617 {FloatConsts.MIN_NORMAL, Float_MAX_SUBNORMAL}, 618 {FloatConsts.MIN_NORMAL+ 619 Float.MIN_VALUE, FloatConsts.MIN_NORMAL}, 620 {Float.MAX_VALUE, Float_MAX_VALUEmm}, 621 {infinityF, Float.MAX_VALUE}, 622 }; 623 624 for(int i = 0; i < testCases.length; i++) { 625 failures+=Tests.test("Math.nextDown(float)", 626 testCases[i][0], Math.nextDown(testCases[i][0]), testCases[i][1]); 627 628 failures+=Tests.test("StrictMath.nextDown(float)", 629 testCases[i][0], StrictMath.nextDown(testCases[i][0]), testCases[i][1]); 630 } 631 632 return failures; 633 } 634 635 636 public static int testDoubleNextDown() { 637 int failures=0; 638 639 /* 640 * Each row of testCases represents one test case for nextDown; 641 * the first column is the input and the second column is the 642 * expected result. 643 */ 644 double testCases [][] = { 645 {NaNd, NaNd}, 646 {-infinityD, -infinityD}, 647 {-Double.MAX_VALUE, -infinityD}, 648 {-Double_MAX_VALUEmm, -Double.MAX_VALUE}, 649 {-Double_MAX_SUBNORMAL, -DoubleConsts.MIN_NORMAL}, 650 {-Double_MAX_SUBNORMALmm, -Double_MAX_SUBNORMAL}, 651 {-0.0d, -Double.MIN_VALUE}, 652 {+0.0d, -Double.MIN_VALUE}, 653 {Double.MIN_VALUE, 0.0d}, 654 {Double.MIN_VALUE*2, Double.MIN_VALUE}, 655 {Double_MAX_SUBNORMAL, Double_MAX_SUBNORMALmm}, 656 {DoubleConsts.MIN_NORMAL, Double_MAX_SUBNORMAL}, 657 {DoubleConsts.MIN_NORMAL+ 658 Double.MIN_VALUE, DoubleConsts.MIN_NORMAL}, 659 {Double.MAX_VALUE, Double_MAX_VALUEmm}, 660 {infinityD, Double.MAX_VALUE}, 661 }; 662 663 for(int i = 0; i < testCases.length; i++) { 664 failures+=Tests.test("Math.nextDown(double)", 665 testCases[i][0], Math.nextDown(testCases[i][0]), testCases[i][1]); 666 667 failures+=Tests.test("StrictMath.nextDown(double)", 668 testCases[i][0], StrictMath.nextDown(testCases[i][0]), testCases[i][1]); 669 } 670 671 return failures; 672 } 673 674 675 /* ********************** boolean tests ****************************** */ 676 677 /* 678 * Combined tests for boolean functions, isFinite, isInfinite, 679 * isNaN, isUnordered. 680 */ 681 682 public static int testFloatBooleanMethods() { 683 int failures = 0; 684 685 float testCases [] = { 686 NaNf, 687 -infinityF, 688 infinityF, 689 -Float.MAX_VALUE, 690 -3.0f, 691 -1.0f, 692 -FloatConsts.MIN_NORMAL, 693 -Float_MAX_SUBNORMALmm, 694 -Float_MAX_SUBNORMAL, 695 -Float.MIN_VALUE, 696 -0.0f, 697 +0.0f, 698 Float.MIN_VALUE, 699 Float_MAX_SUBNORMALmm, 700 Float_MAX_SUBNORMAL, 701 FloatConsts.MIN_NORMAL, 702 1.0f, 703 3.0f, 704 Float_MAX_VALUEmm, 705 Float.MAX_VALUE 706 }; 707 708 for(int i = 0; i < testCases.length; i++) { 709 // isNaN 710 failures+=Tests.test("Float.isNaN(float)", testCases[i], 711 Float.isNaN(testCases[i]), (i ==0)); 712 713 // isFinite 714 failures+=Tests.test("Float.isFinite(float)", testCases[i], 715 Float.isFinite(testCases[i]), (i >= 3)); 716 717 // isInfinite 718 failures+=Tests.test("Float.isInfinite(float)", testCases[i], 719 Float.isInfinite(testCases[i]), (i==1 || i==2)); 720 721 // isUnorderd 722 for(int j = 0; j < testCases.length; j++) { 723 failures+=Tests.test("Tests.isUnordered(float, float)", testCases[i],testCases[j], 724 Tests.isUnordered(testCases[i],testCases[j]), (i==0 || j==0)); 725 } 726 } 727 728 return failures; 729 } 730 731 public static int testDoubleBooleanMethods() { 732 int failures = 0; 733 boolean result = false; 734 735 double testCases [] = { 736 NaNd, 737 -infinityD, 738 infinityD, 739 -Double.MAX_VALUE, 740 -3.0d, 741 -1.0d, 742 -DoubleConsts.MIN_NORMAL, 743 -Double_MAX_SUBNORMALmm, 744 -Double_MAX_SUBNORMAL, 745 -Double.MIN_VALUE, 746 -0.0d, 747 +0.0d, 748 Double.MIN_VALUE, 749 Double_MAX_SUBNORMALmm, 750 Double_MAX_SUBNORMAL, 751 DoubleConsts.MIN_NORMAL, 752 1.0d, 753 3.0d, 754 Double_MAX_VALUEmm, 755 Double.MAX_VALUE 756 }; 757 758 for(int i = 0; i < testCases.length; i++) { 759 // isNaN 760 failures+=Tests.test("Double.isNaN(double)", testCases[i], 761 Double.isNaN(testCases[i]), (i ==0)); 762 763 // isFinite 764 failures+=Tests.test("Double.isFinite(double)", testCases[i], 765 Double.isFinite(testCases[i]), (i >= 3)); 766 767 // isInfinite 768 failures+=Tests.test("Double.isInfinite(double)", testCases[i], 769 Double.isInfinite(testCases[i]), (i==1 || i==2)); 770 771 // isUnorderd 773 failures+=Tests.test("Tests.isUnordered(double, double)", testCases[i],testCases[j], 774 Tests.isUnordered(testCases[i],testCases[j]), (i==0 || j==0)); 775 } 776 } 777 778 return failures; 779 } 780 781 /* ******************** copySign tests******************************** */ 782 783 public static int testFloatCopySign() { 784 int failures = 0; 785 786 // testCases[0] are logically positive numbers; 787 // testCases[1] are negative numbers. 788 float testCases [][] = { 789 {+0.0f, 790 Float.MIN_VALUE, 791 Float_MAX_SUBNORMALmm, 792 Float_MAX_SUBNORMAL, 793 FloatConsts.MIN_NORMAL, 794 1.0f, 795 3.0f, 796 Float_MAX_VALUEmm, 797 Float.MAX_VALUE, 798 infinityF, 799 }, 800 {-infinityF, 801 -Float.MAX_VALUE, 802 -3.0f, 803 -1.0f, 804 -FloatConsts.MIN_NORMAL, 805 -Float_MAX_SUBNORMALmm, 806 -Float_MAX_SUBNORMAL, 807 -Float.MIN_VALUE, 808 -0.0f} 809 }; 810 811 float NaNs[] = {Float.intBitsToFloat(0x7fc00000), // "positive" NaN 812 Float.intBitsToFloat(0xFfc00000)}; // "negative" NaN 813 814 // Tests shared between raw and non-raw versions 815 for(int i = 0; i < 2; i++) { 816 for(int j = 0; j < 2; j++) { 817 for(int m = 0; m < testCases[i].length; m++) { 818 for(int n = 0; n < testCases[j].length; n++) { 819 // copySign(magnitude, sign) 820 failures+=Tests.test("Math.copySign(float,float)", 821 testCases[i][m],testCases[j][n], 822 Math.copySign(testCases[i][m], testCases[j][n]), 823 (j==0?1.0f:-1.0f)*Math.abs(testCases[i][m]) ); 824 847 testCases[i][m], NaNs[j], 848 StrictMath.copySign(testCases[i][m], NaNs[j]), 849 Math.abs(testCases[i][m]) ); 850 } 851 } 852 } 853 854 return failures; 855 } 856 857 public static int testDoubleCopySign() { 858 int failures = 0; 859 860 // testCases[0] are logically positive numbers; 861 // testCases[1] are negative numbers. 862 double testCases [][] = { 863 {+0.0d, 864 Double.MIN_VALUE, 865 Double_MAX_SUBNORMALmm, 866 Double_MAX_SUBNORMAL, 867 DoubleConsts.MIN_NORMAL, 868 1.0d, 869 3.0d, 870 Double_MAX_VALUEmm, 871 Double.MAX_VALUE, 872 infinityD, 873 }, 874 {-infinityD, 875 -Double.MAX_VALUE, 876 -3.0d, 877 -1.0d, 878 -DoubleConsts.MIN_NORMAL, 879 -Double_MAX_SUBNORMALmm, 880 -Double_MAX_SUBNORMAL, 881 -Double.MIN_VALUE, 882 -0.0d} 883 }; 884 885 double NaNs[] = {Double.longBitsToDouble(0x7ff8000000000000L), // "positive" NaN 886 Double.longBitsToDouble(0xfff8000000000000L), // "negative" NaN 887 Double.longBitsToDouble(0x7FF0000000000001L), 888 Double.longBitsToDouble(0xFFF0000000000001L), 889 Double.longBitsToDouble(0x7FF8555555555555L), 890 Double.longBitsToDouble(0xFFF8555555555555L), 891 Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), 892 Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), 893 Double.longBitsToDouble(0x7FFDeadBeef00000L), 894 Double.longBitsToDouble(0xFFFDeadBeef00000L), 895 Double.longBitsToDouble(0x7FFCafeBabe00000L), 896 Double.longBitsToDouble(0xFFFCafeBabe00000L)}; 897 898 // Tests shared between Math and StrictMath versions 947 failures+=Tests.test("Math.scalb(float,int)", 948 value, scale_factor, 949 Math.scalb(value, scale_factor), expected); 950 951 failures+=Tests.test("Math.scalb(float,int)", 952 -value, scale_factor, 953 Math.scalb(-value, scale_factor), -expected); 954 955 failures+=Tests.test("StrictMath.scalb(float,int)", 956 value, scale_factor, 957 StrictMath.scalb(value, scale_factor), expected); 958 959 failures+=Tests.test("StrictMath.scalb(float,int)", 960 -value, scale_factor, 961 StrictMath.scalb(-value, scale_factor), -expected); 962 return failures; 963 } 964 965 public static int testFloatScalb() { 966 int failures=0; 967 int MAX_SCALE = FloatConsts.MAX_EXPONENT + -FloatConsts.MIN_EXPONENT + 968 FloatConsts.SIGNIFICAND_WIDTH + 1; 969 970 971 // Arguments x, where scalb(x,n) is x for any n. 972 float [] identityTestCases = {NaNf, 973 -0.0f, 974 +0.0f, 975 infinityF, 976 -infinityF 977 }; 978 979 float [] subnormalTestCases = { 980 Float.MIN_VALUE, 981 3.0f*Float.MIN_VALUE, 982 Float_MAX_SUBNORMALmm, 983 Float_MAX_SUBNORMAL 984 }; 985 986 float [] someTestCases = { 987 Float.MIN_VALUE, 988 3.0f*Float.MIN_VALUE, 989 Float_MAX_SUBNORMALmm, 990 Float_MAX_SUBNORMAL, 991 FloatConsts.MIN_NORMAL, 992 1.0f, 993 2.0f, 994 3.0f, 995 (float)Math.PI, 996 Float_MAX_VALUEmm, 997 Float.MAX_VALUE 998 }; 999 1000 int [] oneMultiplyScalingFactors = { 1001 FloatConsts.MIN_EXPONENT, 1002 FloatConsts.MIN_EXPONENT+1, 1003 -3, 1004 -2, 1005 -1, 1006 0, 1007 1, 1008 2, 1009 3, 1010 FloatConsts.MAX_EXPONENT-1, 1011 FloatConsts.MAX_EXPONENT 1012 }; 1013 1014 int [] manyScalingFactors = { 1015 Integer.MIN_VALUE, 1016 Integer.MIN_VALUE+1, 1017 -MAX_SCALE -1, 1018 -MAX_SCALE, 1019 -MAX_SCALE+1, 1020 1021 2*FloatConsts.MIN_EXPONENT-1, // -253 1022 2*FloatConsts.MIN_EXPONENT, // -252 1023 2*FloatConsts.MIN_EXPONENT+1, // -251 1024 1025 FloatConsts.MIN_EXPONENT - FloatConsts.SIGNIFICAND_WIDTH, 1026 FloatConsts.MIN_SUB_EXPONENT, 1027 -FloatConsts.MAX_EXPONENT, // -127 1028 FloatConsts.MIN_EXPONENT, // -126 1029 1030 -2, 1031 -1, 1032 0, 1033 1, 1034 2, 1035 1036 FloatConsts.MAX_EXPONENT-1, // 126 1037 FloatConsts.MAX_EXPONENT, // 127 1038 FloatConsts.MAX_EXPONENT+1, // 128 1039 1040 2*FloatConsts.MAX_EXPONENT-1, // 253 1041 2*FloatConsts.MAX_EXPONENT, // 254 1042 2*FloatConsts.MAX_EXPONENT+1, // 255 1043 1044 MAX_SCALE-1, 1045 MAX_SCALE, 1046 MAX_SCALE+1, 1047 Integer.MAX_VALUE-1, 1048 Integer.MAX_VALUE 1049 }; 1050 1051 // Test cases where scaling is always a no-op 1052 for(int i=0; i < identityTestCases.length; i++) { 1053 for(int j=0; j < manyScalingFactors.length; j++) { 1054 failures += testScalbCase(identityTestCases[i], 1055 manyScalingFactors[j], 1056 identityTestCases[i]); 1057 } 1058 } 1059 1060 // Test cases where result is 0.0 or infinity due to magnitude 1061 // of the scaling factor 1062 for(int i=0; i < someTestCases.length; i++) { 1069 Math.copySign( (scaleFactor>0?infinityF:0.0f), value) ); 1070 } 1071 } 1072 } 1073 1074 // Test cases that could be done with one floating-point 1075 // multiply. 1076 for(int i=0; i < someTestCases.length; i++) { 1077 for(int j=0; j < oneMultiplyScalingFactors.length; j++) { 1078 int scaleFactor = oneMultiplyScalingFactors[j]; 1079 float value = someTestCases[i]; 1080 1081 failures+=testScalbCase(value, 1082 scaleFactor, 1083 value*powerOfTwoF(scaleFactor)); 1084 } 1085 } 1086 1087 // Create 2^MAX_EXPONENT 1088 float twoToTheMaxExp = 1.0f; // 2^0 1089 for(int i = 0; i < FloatConsts.MAX_EXPONENT; i++) 1090 twoToTheMaxExp *=2.0f; 1091 1092 // Scale-up subnormal values until they all overflow 1093 for(int i=0; i < subnormalTestCases.length; i++) { 1094 float scale = 1.0f; // 2^j 1095 float value = subnormalTestCases[i]; 1096 1097 for(int j=FloatConsts.MAX_EXPONENT*2; j < MAX_SCALE; j++) { // MAX_SCALE -1 should cause overflow 1098 int scaleFactor = j; 1099 1100 failures+=testScalbCase(value, 1101 scaleFactor, 1102 (Tests.ilogb(value) +j > FloatConsts.MAX_EXPONENT ) ? 1103 Math.copySign(infinityF, value) : // overflow 1104 // calculate right answer 1105 twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) ); 1106 scale*=2.0f; 1107 } 1108 } 1109 1110 // Scale down a large number until it underflows. By scaling 1111 // down MAX_NORMALmm, the first subnormal result will be exact 1112 // but the next one will round -- all those results can be 1113 // checked by halving a separate value in the loop. Actually, 1114 // we can keep halving and checking until the product is zero 1115 // since: 1116 // 1117 // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact 1118 // it will round *up* 1119 // 1120 // 2. When rounding first occurs in the expected product, it 1121 // too rounds up, to 2^-MAX_EXPONENT. 1122 // 1155 value, scale_factor, 1156 Math.scalb(value, scale_factor), expected); 1157 1158 failures+=Tests.test("Math.scalb(double,int)", 1159 -value, scale_factor, 1160 Math.scalb(-value, scale_factor), -expected); 1161 1162 failures+=Tests.test("StrictMath.scalb(double,int)", 1163 value, scale_factor, 1164 StrictMath.scalb(value, scale_factor), expected); 1165 1166 failures+=Tests.test("StrictMath.scalb(double,int)", 1167 -value, scale_factor, 1168 StrictMath.scalb(-value, scale_factor), -expected); 1169 1170 return failures; 1171 } 1172 1173 public static int testDoubleScalb() { 1174 int failures=0; 1175 int MAX_SCALE = DoubleConsts.MAX_EXPONENT + -DoubleConsts.MIN_EXPONENT + 1176 DoubleConsts.SIGNIFICAND_WIDTH + 1; 1177 1178 1179 // Arguments x, where scalb(x,n) is x for any n. 1180 double [] identityTestCases = {NaNd, 1181 -0.0, 1182 +0.0, 1183 infinityD, 1184 }; 1185 1186 double [] subnormalTestCases = { 1187 Double.MIN_VALUE, 1188 3.0d*Double.MIN_VALUE, 1189 Double_MAX_SUBNORMALmm, 1190 Double_MAX_SUBNORMAL 1191 }; 1192 1193 double [] someTestCases = { 1194 Double.MIN_VALUE, 1195 3.0d*Double.MIN_VALUE, 1196 Double_MAX_SUBNORMALmm, 1197 Double_MAX_SUBNORMAL, 1198 DoubleConsts.MIN_NORMAL, 1199 1.0d, 1200 2.0d, 1201 3.0d, 1202 Math.PI, 1203 Double_MAX_VALUEmm, 1204 Double.MAX_VALUE 1205 }; 1206 1207 int [] oneMultiplyScalingFactors = { 1208 DoubleConsts.MIN_EXPONENT, 1209 DoubleConsts.MIN_EXPONENT+1, 1210 -3, 1211 -2, 1212 -1, 1213 0, 1214 1, 1215 2, 1216 3, 1217 DoubleConsts.MAX_EXPONENT-1, 1218 DoubleConsts.MAX_EXPONENT 1219 }; 1220 1221 int [] manyScalingFactors = { 1222 Integer.MIN_VALUE, 1223 Integer.MIN_VALUE+1, 1224 -MAX_SCALE -1, 1225 -MAX_SCALE, 1226 -MAX_SCALE+1, 1227 1228 2*DoubleConsts.MIN_EXPONENT-1, // -2045 1229 2*DoubleConsts.MIN_EXPONENT, // -2044 1230 2*DoubleConsts.MIN_EXPONENT+1, // -2043 1231 1232 DoubleConsts.MIN_EXPONENT, // -1022 1233 DoubleConsts.MIN_EXPONENT - DoubleConsts.SIGNIFICAND_WIDTH, 1234 DoubleConsts.MIN_SUB_EXPONENT, 1235 -DoubleConsts.MAX_EXPONENT, // -1023 1236 DoubleConsts.MIN_EXPONENT, // -1022 1237 1238 -2, 1239 -1, 1240 0, 1241 1, 1242 2, 1243 1244 DoubleConsts.MAX_EXPONENT-1, // 1022 1245 DoubleConsts.MAX_EXPONENT, // 1023 1246 DoubleConsts.MAX_EXPONENT+1, // 1024 1247 1248 2*DoubleConsts.MAX_EXPONENT-1, // 2045 1249 2*DoubleConsts.MAX_EXPONENT, // 2046 1250 2*DoubleConsts.MAX_EXPONENT+1, // 2047 1251 1252 MAX_SCALE-1, 1253 MAX_SCALE, 1254 MAX_SCALE+1, 1255 Integer.MAX_VALUE-1, 1256 Integer.MAX_VALUE 1257 }; 1258 1259 // Test cases where scaling is always a no-op 1260 for(int i=0; i < identityTestCases.length; i++) { 1261 for(int j=0; j < manyScalingFactors.length; j++) { 1262 failures += testScalbCase(identityTestCases[i], 1263 manyScalingFactors[j], 1264 identityTestCases[i]); 1265 } 1266 } 1267 1268 // Test cases where result is 0.0 or infinity due to magnitude 1269 // of the scaling factor 1270 for(int i=0; i < someTestCases.length; i++) { 1277 Math.copySign( (scaleFactor>0?infinityD:0.0), value) ); 1278 } 1279 } 1280 } 1281 1282 // Test cases that could be done with one floating-point 1283 // multiply. 1284 for(int i=0; i < someTestCases.length; i++) { 1285 for(int j=0; j < oneMultiplyScalingFactors.length; j++) { 1286 int scaleFactor = oneMultiplyScalingFactors[j]; 1287 double value = someTestCases[i]; 1288 1289 failures+=testScalbCase(value, 1290 scaleFactor, 1291 value*powerOfTwoD(scaleFactor)); 1292 } 1293 } 1294 1295 // Create 2^MAX_EXPONENT 1296 double twoToTheMaxExp = 1.0; // 2^0 1297 for(int i = 0; i < DoubleConsts.MAX_EXPONENT; i++) 1298 twoToTheMaxExp *=2.0; 1299 1300 // Scale-up subnormal values until they all overflow 1301 for(int i=0; i < subnormalTestCases.length; i++) { 1302 double scale = 1.0; // 2^j 1303 double value = subnormalTestCases[i]; 1304 1305 for(int j=DoubleConsts.MAX_EXPONENT*2; j < MAX_SCALE; j++) { // MAX_SCALE -1 should cause overflow 1306 int scaleFactor = j; 1307 1308 failures+=testScalbCase(value, 1309 scaleFactor, 1310 (Tests.ilogb(value) +j > DoubleConsts.MAX_EXPONENT ) ? 1311 Math.copySign(infinityD, value) : // overflow 1312 // calculate right answer 1313 twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) ); 1314 scale*=2.0; 1315 } 1316 } 1317 1318 // Scale down a large number until it underflows. By scaling 1319 // down MAX_NORMALmm, the first subnormal result will be exact 1320 // but the next one will round -- all those results can be 1321 // checked by halving a separate value in the loop. Actually, 1322 // we can keep halving and checking until the product is zero 1323 // since: 1324 // 1325 // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact 1326 // it will round *up* 1327 // 1328 // 2. When rounding first occurs in the expected product, it 1329 // too rounds up, to 2^-MAX_EXPONENT. 1330 // 1331 // Halving expected after rounding happends to give the same 1332 // result as the scalb operation. 1333 double expected = Double_MAX_VALUEmm *0.5f; 1334 for(int i = -1; i > -MAX_SCALE; i--) { 1335 failures+=testScalbCase(Double_MAX_VALUEmm, i, expected); 1336 1337 expected *= 0.5; 1338 } 1339 1340 // Tricky rounding tests: 1341 // Scale down a large number into subnormal range such that if 1342 // scalb is being implemented with multiple floating-point 1343 // multiplies, the value would round twice if the multiplies 1344 // were done in the wrong order. 1345 1346 double value = 0x1.000000000000bP-1; 1347 expected = 0x0.2000000000001P-1022; 1348 for(int i = 0; i < DoubleConsts.MAX_EXPONENT+2; i++) { 1349 failures+=testScalbCase(value, 1350 -1024-i, 1351 expected); 1352 value *=2.0; 1353 } 1354 1355 return failures; 1356 } 1357 1358 /* ************************* ulp tests ******************************* */ 1359 1360 1361 /* 1362 * Test Math.ulp and StrictMath.ulp with +d and -d. 1363 */ 1364 static int testUlpCase(float f, float expected) { 1365 float minus_f = -f; 1366 int failures=0; 1367 1368 failures+=Tests.test("Math.ulp(float)", f, 1384 Math.ulp(d), expected); 1385 failures+=Tests.test("Math.ulp(double)", minus_d, 1386 Math.ulp(minus_d), expected); 1387 failures+=Tests.test("StrictMath.ulp(double)", d, 1388 StrictMath.ulp(d), expected); 1389 failures+=Tests.test("StrictMath.ulp(double)", minus_d, 1390 StrictMath.ulp(minus_d), expected); 1391 return failures; 1392 } 1393 1394 public static int testFloatUlp() { 1395 int failures = 0; 1396 float [] specialValues = {NaNf, 1397 Float.POSITIVE_INFINITY, 1398 +0.0f, 1399 +1.0f, 1400 +2.0f, 1401 +16.0f, 1402 +Float.MIN_VALUE, 1403 +Float_MAX_SUBNORMAL, 1404 +FloatConsts.MIN_NORMAL, 1405 +Float.MAX_VALUE 1406 }; 1407 1408 float [] specialResults = {NaNf, 1409 Float.POSITIVE_INFINITY, 1410 Float.MIN_VALUE, 1411 powerOfTwoF(-23), 1412 powerOfTwoF(-22), 1413 powerOfTwoF(-19), 1414 Float.MIN_VALUE, 1415 Float.MIN_VALUE, 1416 Float.MIN_VALUE, 1417 powerOfTwoF(104) 1418 }; 1419 1420 // Special value tests 1421 for(int i = 0; i < specialValues.length; i++) { 1422 failures += testUlpCase(specialValues[i], specialResults[i]); 1423 } 1424 1425 1426 // Normal exponent tests 1427 for(int i = FloatConsts.MIN_EXPONENT; i <= FloatConsts.MAX_EXPONENT; i++) { 1428 float expected; 1429 1430 // Create power of two 1431 float po2 = powerOfTwoF(i); 1432 expected = Math.scalb(1.0f, i - (FloatConsts.SIGNIFICAND_WIDTH-1)); 1433 1434 failures += testUlpCase(po2, expected); 1435 1436 // Generate some random bit patterns for the significand 1437 for(int j = 0; j < 10; j++) { 1438 int randSignif = rand.nextInt(); 1439 float randFloat; 1440 1441 randFloat = Float.intBitsToFloat( // Exponent 1442 (Float.floatToIntBits(po2)& 1443 (~FloatConsts.SIGNIF_BIT_MASK)) | 1444 // Significand 1445 (randSignif & 1446 FloatConsts.SIGNIF_BIT_MASK) ); 1447 1448 failures += testUlpCase(randFloat, expected); 1449 } 1450 1451 if (i > FloatConsts.MIN_EXPONENT) { 1452 float po2minus = Math.nextAfter(po2, 1453 Float.NEGATIVE_INFINITY); 1454 failures += testUlpCase(po2minus, expected/2.0f); 1455 } 1456 } 1457 1458 // Subnormal tests 1459 1460 /* 1461 * Start with MIN_VALUE, left shift, test high value, low 1462 * values, and random in between. 1463 * 1464 * Use nextAfter to calculate, high value of previous binade, 1465 * loop count i will indicate how many random bits, if any are 1466 * needed. 1467 */ 1468 1469 float top=Float.MIN_VALUE; 1470 for( int i = 1; 1471 i < FloatConsts.SIGNIFICAND_WIDTH; 1489 (rand.nextInt() & mask ) ) ; 1490 1491 failures += testUlpCase(randFloat, Float.MIN_VALUE); 1492 } 1493 } 1494 } 1495 1496 return failures; 1497 } 1498 1499 public static int testDoubleUlp() { 1500 int failures = 0; 1501 double [] specialValues = {NaNd, 1502 Double.POSITIVE_INFINITY, 1503 +0.0d, 1504 +1.0d, 1505 +2.0d, 1506 +16.0d, 1507 +Double.MIN_VALUE, 1508 +Double_MAX_SUBNORMAL, 1509 +DoubleConsts.MIN_NORMAL, 1510 +Double.MAX_VALUE 1511 }; 1512 1513 double [] specialResults = {NaNf, 1514 Double.POSITIVE_INFINITY, 1515 Double.MIN_VALUE, 1516 powerOfTwoD(-52), 1517 powerOfTwoD(-51), 1518 powerOfTwoD(-48), 1519 Double.MIN_VALUE, 1520 Double.MIN_VALUE, 1521 Double.MIN_VALUE, 1522 powerOfTwoD(971) 1523 }; 1524 1525 // Special value tests 1526 for(int i = 0; i < specialValues.length; i++) { 1527 failures += testUlpCase(specialValues[i], specialResults[i]); 1528 } 1529 1530 1531 // Normal exponent tests 1532 for(int i = DoubleConsts.MIN_EXPONENT; i <= DoubleConsts.MAX_EXPONENT; i++) { 1533 double expected; 1534 1535 // Create power of two 1536 double po2 = powerOfTwoD(i); 1537 expected = Math.scalb(1.0, i - (DoubleConsts.SIGNIFICAND_WIDTH-1)); 1538 1539 failures += testUlpCase(po2, expected); 1540 1541 // Generate some random bit patterns for the significand 1542 for(int j = 0; j < 10; j++) { 1543 long randSignif = rand.nextLong(); 1544 double randDouble; 1545 1546 randDouble = Double.longBitsToDouble( // Exponent 1547 (Double.doubleToLongBits(po2)& 1548 (~DoubleConsts.SIGNIF_BIT_MASK)) | 1549 // Significand 1550 (randSignif & 1551 DoubleConsts.SIGNIF_BIT_MASK) ); 1552 1553 failures += testUlpCase(randDouble, expected); 1554 } 1555 1556 if (i > DoubleConsts.MIN_EXPONENT) { 1557 double po2minus = Math.nextAfter(po2, 1558 Double.NEGATIVE_INFINITY); 1559 failures += testUlpCase(po2minus, expected/2.0f); 1560 } 1561 } 1562 1563 // Subnormal tests 1564 1565 /* 1566 * Start with MIN_VALUE, left shift, test high value, low 1567 * values, and random in between. 1568 * 1569 * Use nextAfter to calculate, high value of previous binade, 1570 * loop count i will indicate how many random bits, if any are 1571 * needed. 1572 */ 1573 1574 double top=Double.MIN_VALUE; 1575 for( int i = 1; 1576 i < DoubleConsts.SIGNIFICAND_WIDTH; 1590 int mask = ~((~0)<<(i-1)); 1591 double randDouble = Double.longBitsToDouble( // Exponent 1592 Double.doubleToLongBits(top) | 1593 // Significand 1594 (rand.nextLong() & mask ) ) ; 1595 1596 failures += testUlpCase(randDouble, Double.MIN_VALUE); 1597 } 1598 } 1599 } 1600 1601 return failures; 1602 } 1603 1604 public static int testFloatSignum() { 1605 int failures = 0; 1606 float testCases [][] = { 1607 {NaNf, NaNf}, 1608 {-infinityF, -1.0f}, 1609 {-Float.MAX_VALUE, -1.0f}, 1610 {-FloatConsts.MIN_NORMAL, -1.0f}, 1611 {-1.0f, -1.0f}, 1612 {-2.0f, -1.0f}, 1613 {-Float_MAX_SUBNORMAL, -1.0f}, 1614 {-Float.MIN_VALUE, -1.0f}, 1615 {-0.0f, -0.0f}, 1616 {+0.0f, +0.0f}, 1617 {Float.MIN_VALUE, 1.0f}, 1618 {Float_MAX_SUBNORMALmm, 1.0f}, 1619 {Float_MAX_SUBNORMAL, 1.0f}, 1620 {FloatConsts.MIN_NORMAL, 1.0f}, 1621 {1.0f, 1.0f}, 1622 {2.0f, 1.0f}, 1623 {Float_MAX_VALUEmm, 1.0f}, 1624 {Float.MAX_VALUE, 1.0f}, 1625 {infinityF, 1.0f} 1626 }; 1627 1628 for(int i = 0; i < testCases.length; i++) { 1629 failures+=Tests.test("Math.signum(float)", 1630 testCases[i][0], Math.signum(testCases[i][0]), testCases[i][1]); 1631 failures+=Tests.test("StrictMath.signum(float)", 1632 testCases[i][0], StrictMath.signum(testCases[i][0]), testCases[i][1]); 1633 } 1634 1635 return failures; 1636 } 1637 1638 public static int testDoubleSignum() { 1639 int failures = 0; 1640 double testCases [][] = { 1641 {NaNd, NaNd}, 1642 {-infinityD, -1.0}, 1643 {-Double.MAX_VALUE, -1.0}, 1644 {-DoubleConsts.MIN_NORMAL, -1.0}, 1645 {-1.0, -1.0}, 1646 {-2.0, -1.0}, 1647 {-Double_MAX_SUBNORMAL, -1.0}, 1648 {-Double.MIN_VALUE, -1.0d}, 1649 {-0.0d, -0.0d}, 1650 {+0.0d, +0.0d}, 1651 {Double.MIN_VALUE, 1.0}, 1652 {Double_MAX_SUBNORMALmm, 1.0}, 1653 {Double_MAX_SUBNORMAL, 1.0}, 1654 {DoubleConsts.MIN_NORMAL, 1.0}, 1655 {1.0, 1.0}, 1656 {2.0, 1.0}, 1657 {Double_MAX_VALUEmm, 1.0}, 1658 {Double.MAX_VALUE, 1.0}, 1659 {infinityD, 1.0} 1660 }; 1661 1662 for(int i = 0; i < testCases.length; i++) { 1663 failures+=Tests.test("Math.signum(double)", 1664 testCases[i][0], Math.signum(testCases[i][0]), testCases[i][1]); 1665 failures+=Tests.test("StrictMath.signum(double)", 1666 testCases[i][0], StrictMath.signum(testCases[i][0]), testCases[i][1]); 1667 } 1668 1669 return failures; 1670 } 1671 1672 1673 public static void main(String argv[]) { 1674 int failures = 0; | 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4860891 4826732 4780454 4939441 4826652 27 * @summary Tests for IEEE 754[R] recommended functions and similar methods 28 * @author Joseph D. Darcy 29 */ 30 31 public class IeeeRecommendedTests { 32 private IeeeRecommendedTests(){} 33 34 static final float NaNf = Float.NaN; 35 static final double NaNd = Double.NaN; 36 static final float infinityF = Float.POSITIVE_INFINITY; 37 static final double infinityD = Double.POSITIVE_INFINITY; 38 39 static final float Float_MAX_VALUEmm = 0x1.fffffcP+127f; 40 static final float Float_MAX_SUBNORMAL = 0x0.fffffeP-126f; 41 static final float Float_MAX_SUBNORMALmm = 0x0.fffffcP-126f; 42 43 static final double Double_MAX_VALUEmm = 0x1.ffffffffffffeP+1023; 44 static final double Double_MAX_SUBNORMAL = 0x0.fffffffffffffP-1022; 45 static final double Double_MAX_SUBNORMALmm = 0x0.ffffffffffffeP-1022; 46 47 // Initialize shared random number generator 48 static java.util.Random rand = new java.util.Random(); 49 50 /** 51 * Returns a floating-point power of two in the normal range. 52 */ 53 static double powerOfTwoD(int n) { 54 return Double.longBitsToDouble((((long)n + (long)Double.MAX_EXPONENT) << 55 (DoubleConsts.SIGNIFICAND_WIDTH-1)) 56 & DoubleConsts.EXP_BIT_MASK); 57 } 58 59 /** 60 * Returns a floating-point power of two in the normal range. 61 */ 62 static float powerOfTwoF(int n) { 63 return Float.intBitsToFloat(((n + Float.MAX_EXPONENT) << 64 (FloatConsts.SIGNIFICAND_WIDTH-1)) 65 & FloatConsts.EXP_BIT_MASK); 66 } 67 68 /* ******************** getExponent tests ****************************** */ 69 70 /* 71 * The tests for getExponent should test the special values (NaN, +/- 72 * infinity, etc.), test the endpoints of each binade (set of 73 * floating-point values with the same exponent), and for good 74 * measure, test some random values within each binade. Testing 75 * the endpoints of each binade includes testing both positive and 76 * negative numbers. Subnormal values with different normalized 77 * exponents should be tested too. Both Math and StrictMath 78 * methods should return the same results. 79 */ 80 81 /* 82 * Test Math.getExponent and StrictMath.getExponent with +d and -d. 83 */ 109 failures+=Tests.test("Math.getExponent(double)", minus_d, 110 Math.getExponent(minus_d), expected); 111 112 failures+=Tests.test("StrictMath.getExponent(double)", d, 113 StrictMath.getExponent(d), expected); 114 failures+=Tests.test("StrictMath.getExponent(double)", minus_d, 115 StrictMath.getExponent(minus_d), expected); 116 return failures; 117 } 118 119 public static int testFloatGetExponent() { 120 int failures = 0; 121 float [] specialValues = {NaNf, 122 Float.POSITIVE_INFINITY, 123 +0.0f, 124 +1.0f, 125 +2.0f, 126 +16.0f, 127 +Float.MIN_VALUE, 128 +Float_MAX_SUBNORMAL, 129 +Float.MIN_NORMAL, 130 +Float.MAX_VALUE 131 }; 132 133 int [] specialResults = {Float.MAX_EXPONENT + 1, // NaN results 134 Float.MAX_EXPONENT + 1, // Infinite results 135 Float.MIN_EXPONENT - 1, // Zero results 136 0, 137 1, 138 4, 139 Float.MIN_EXPONENT - 1, 140 -Float.MAX_EXPONENT, 141 Float.MIN_EXPONENT, 142 Float.MAX_EXPONENT 143 }; 144 145 // Special value tests 146 for(int i = 0; i < specialValues.length; i++) { 147 failures += testGetExponentCase(specialValues[i], specialResults[i]); 148 } 149 150 151 // Normal exponent tests 152 for(int i = Float.MIN_EXPONENT; i <= Float.MAX_EXPONENT; i++) { 153 int result; 154 155 // Create power of two 156 float po2 = powerOfTwoF(i); 157 158 failures += testGetExponentCase(po2, i); 159 160 // Generate some random bit patterns for the significand 161 for(int j = 0; j < 10; j++) { 162 int randSignif = rand.nextInt(); 163 float randFloat; 164 165 randFloat = Float.intBitsToFloat( // Exponent 166 (Float.floatToIntBits(po2)& 167 (~FloatConsts.SIGNIF_BIT_MASK)) | 168 // Significand 169 (randSignif & 170 FloatConsts.SIGNIF_BIT_MASK) ); 171 172 failures += testGetExponentCase(randFloat, i); 173 } 174 175 if (i > Float.MIN_EXPONENT) { 176 float po2minus = Math.nextAfter(po2, 177 Float.NEGATIVE_INFINITY); 178 failures += testGetExponentCase(po2minus, i-1); 179 } 180 } 181 182 // Subnormal exponent tests 183 184 /* 185 * Start with MIN_VALUE, left shift, test high value, low 186 * values, and random in between. 187 * 188 * Use nextAfter to calculate, high value of previous binade, 189 * loop count i will indicate how many random bits, if any are 190 * needed. 191 */ 192 193 float top=Float.MIN_VALUE; 194 for( int i = 1; 195 i < FloatConsts.SIGNIFICAND_WIDTH; 196 i++, top *= 2.0f) { 197 198 failures += testGetExponentCase(top, 199 Float.MIN_EXPONENT - 1); 200 201 // Test largest value in next smaller binade 202 if (i >= 3) {// (i == 1) would test 0.0; 203 // (i == 2) would just retest MIN_VALUE 204 testGetExponentCase(Math.nextAfter(top, 0.0f), 205 Float.MIN_EXPONENT - 1); 206 207 if( i >= 10) { 208 // create a bit mask with (i-1) 1's in the low order 209 // bits 210 int mask = ~((~0)<<(i-1)); 211 float randFloat = Float.intBitsToFloat( // Exponent 212 Float.floatToIntBits(top) | 213 // Significand 214 (rand.nextInt() & mask ) ) ; 215 216 failures += testGetExponentCase(randFloat, 217 Float.MIN_EXPONENT - 1); 218 } 219 } 220 } 221 222 return failures; 223 } 224 225 226 public static int testDoubleGetExponent() { 227 int failures = 0; 228 double [] specialValues = {NaNd, 229 infinityD, 230 +0.0, 231 +1.0, 232 +2.0, 233 +16.0, 234 +Double.MIN_VALUE, 235 +Double_MAX_SUBNORMAL, 236 +Double.MIN_NORMAL, 237 +Double.MAX_VALUE 238 }; 239 240 int [] specialResults = {Double.MAX_EXPONENT + 1, // NaN results 241 Double.MAX_EXPONENT + 1, // Infinite results 242 Double.MIN_EXPONENT - 1, // Zero results 243 0, 244 1, 245 4, 246 Double.MIN_EXPONENT - 1, 247 -Double.MAX_EXPONENT, 248 Double.MIN_EXPONENT, 249 Double.MAX_EXPONENT 250 }; 251 252 // Special value tests 253 for(int i = 0; i < specialValues.length; i++) { 254 failures += testGetExponentCase(specialValues[i], specialResults[i]); 255 } 256 257 258 // Normal exponent tests 259 for(int i = Double.MIN_EXPONENT; i <= Double.MAX_EXPONENT; i++) { 260 int result; 261 262 // Create power of two 263 double po2 = powerOfTwoD(i); 264 265 failures += testGetExponentCase(po2, i); 266 267 // Generate some random bit patterns for the significand 268 for(int j = 0; j < 10; j++) { 269 long randSignif = rand.nextLong(); 270 double randFloat; 271 272 randFloat = Double.longBitsToDouble( // Exponent 273 (Double.doubleToLongBits(po2)& 274 (~DoubleConsts.SIGNIF_BIT_MASK)) | 275 // Significand 276 (randSignif & 277 DoubleConsts.SIGNIF_BIT_MASK) ); 278 279 failures += testGetExponentCase(randFloat, i); 280 } 281 282 if (i > Double.MIN_EXPONENT) { 283 double po2minus = Math.nextAfter(po2, 284 Double.NEGATIVE_INFINITY); 285 failures += testGetExponentCase(po2minus, i-1); 286 } 287 } 288 289 // Subnormal exponent tests 290 291 /* 292 * Start with MIN_VALUE, left shift, test high value, low 293 * values, and random in between. 294 * 295 * Use nextAfter to calculate, high value of previous binade; 296 * loop count i will indicate how many random bits, if any are 297 * needed. 298 */ 299 300 double top=Double.MIN_VALUE; 301 for( int i = 1; 302 i < DoubleConsts.SIGNIFICAND_WIDTH; 303 i++, top *= 2.0f) { 304 305 failures += testGetExponentCase(top, 306 Double.MIN_EXPONENT - 1); 307 308 // Test largest value in next smaller binade 309 if (i >= 3) {// (i == 1) would test 0.0; 310 // (i == 2) would just retest MIN_VALUE 311 testGetExponentCase(Math.nextAfter(top, 0.0), 312 Double.MIN_EXPONENT - 1); 313 314 if( i >= 10) { 315 // create a bit mask with (i-1) 1's in the low order 316 // bits 317 long mask = ~((~0L)<<(i-1)); 318 double randFloat = Double.longBitsToDouble( // Exponent 319 Double.doubleToLongBits(top) | 320 // Significand 321 (rand.nextLong() & mask ) ) ; 322 323 failures += testGetExponentCase(randFloat, 324 Double.MIN_EXPONENT - 1); 325 } 326 } 327 } 328 329 return failures; 330 } 331 332 333 /* ******************** nextAfter tests ****************************** */ 334 335 static int testNextAfterCase(float start, double direction, float expected) { 336 int failures=0; 337 float minus_start = -start; 338 double minus_direction = -direction; 339 float minus_expected = -expected; 340 341 failures+=Tests.test("Math.nextAfter(float,double)", start, direction, 342 Math.nextAfter(start, direction), expected); 343 failures+=Tests.test("Math.nextAfter(float,double)", minus_start, minus_direction, 344 Math.nextAfter(minus_start, minus_direction), minus_expected); 380 float [][] testCases = { 381 {NaNf, NaNf, NaNf}, 382 {NaNf, 0.0f, NaNf}, 383 {0.0f, NaNf, NaNf}, 384 {NaNf, infinityF, NaNf}, 385 {infinityF, NaNf, NaNf}, 386 387 {infinityF, infinityF, infinityF}, 388 {infinityF, -infinityF, Float.MAX_VALUE}, 389 {infinityF, 0.0f, Float.MAX_VALUE}, 390 391 {Float.MAX_VALUE, infinityF, infinityF}, 392 {Float.MAX_VALUE, -infinityF, Float_MAX_VALUEmm}, 393 {Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE}, 394 {Float.MAX_VALUE, 0.0f, Float_MAX_VALUEmm}, 395 396 {Float_MAX_VALUEmm, Float.MAX_VALUE, Float.MAX_VALUE}, 397 {Float_MAX_VALUEmm, infinityF, Float.MAX_VALUE}, 398 {Float_MAX_VALUEmm, Float_MAX_VALUEmm, Float_MAX_VALUEmm}, 399 400 {Float.MIN_NORMAL, infinityF, Float.MIN_NORMAL+ 401 Float.MIN_VALUE}, 402 {Float.MIN_NORMAL, -infinityF, Float_MAX_SUBNORMAL}, 403 {Float.MIN_NORMAL, 1.0f, Float.MIN_NORMAL+ 404 Float.MIN_VALUE}, 405 {Float.MIN_NORMAL, -1.0f, Float_MAX_SUBNORMAL}, 406 {Float.MIN_NORMAL, Float.MIN_NORMAL, Float.MIN_NORMAL}, 407 408 {Float_MAX_SUBNORMAL, Float.MIN_NORMAL, Float.MIN_NORMAL}, 409 {Float_MAX_SUBNORMAL, Float_MAX_SUBNORMAL, Float_MAX_SUBNORMAL}, 410 {Float_MAX_SUBNORMAL, 0.0f, Float_MAX_SUBNORMALmm}, 411 412 {Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMAL, Float_MAX_SUBNORMAL}, 413 {Float_MAX_SUBNORMALmm, 0.0f, Float_MAX_SUBNORMALmm-Float.MIN_VALUE}, 414 {Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMALmm}, 415 416 {Float.MIN_VALUE, 0.0f, 0.0f}, 417 {-Float.MIN_VALUE, 0.0f, -0.0f}, 418 {Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE}, 419 {Float.MIN_VALUE, 1.0f, 2*Float.MIN_VALUE}, 420 421 // Make sure zero behavior is tested 422 {0.0f, 0.0f, 0.0f}, 423 {0.0f, -0.0f, -0.0f}, 424 {-0.0f, 0.0f, 0.0f}, 425 {-0.0f, -0.0f, -0.0f}, 426 {0.0f, infinityF, Float.MIN_VALUE}, 427 {0.0f, -infinityF, -Float.MIN_VALUE}, 428 {-0.0f, infinityF, Float.MIN_VALUE}, 452 double [][] testCases = { 453 {NaNd, NaNd, NaNd}, 454 {NaNd, 0.0d, NaNd}, 455 {0.0d, NaNd, NaNd}, 456 {NaNd, infinityD, NaNd}, 457 {infinityD, NaNd, NaNd}, 458 459 {infinityD, infinityD, infinityD}, 460 {infinityD, -infinityD, Double.MAX_VALUE}, 461 {infinityD, 0.0d, Double.MAX_VALUE}, 462 463 {Double.MAX_VALUE, infinityD, infinityD}, 464 {Double.MAX_VALUE, -infinityD, Double_MAX_VALUEmm}, 465 {Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE}, 466 {Double.MAX_VALUE, 0.0d, Double_MAX_VALUEmm}, 467 468 {Double_MAX_VALUEmm, Double.MAX_VALUE, Double.MAX_VALUE}, 469 {Double_MAX_VALUEmm, infinityD, Double.MAX_VALUE}, 470 {Double_MAX_VALUEmm, Double_MAX_VALUEmm, Double_MAX_VALUEmm}, 471 472 {Double.MIN_NORMAL, infinityD, Double.MIN_NORMAL+ 473 Double.MIN_VALUE}, 474 {Double.MIN_NORMAL, -infinityD, Double_MAX_SUBNORMAL}, 475 {Double.MIN_NORMAL, 1.0f, Double.MIN_NORMAL+ 476 Double.MIN_VALUE}, 477 {Double.MIN_NORMAL, -1.0f, Double_MAX_SUBNORMAL}, 478 {Double.MIN_NORMAL, Double.MIN_NORMAL, Double.MIN_NORMAL}, 479 480 {Double_MAX_SUBNORMAL, Double.MIN_NORMAL, Double.MIN_NORMAL}, 481 {Double_MAX_SUBNORMAL, Double_MAX_SUBNORMAL, Double_MAX_SUBNORMAL}, 482 {Double_MAX_SUBNORMAL, 0.0d, Double_MAX_SUBNORMALmm}, 483 484 {Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMAL, Double_MAX_SUBNORMAL}, 485 {Double_MAX_SUBNORMALmm, 0.0d, Double_MAX_SUBNORMALmm-Double.MIN_VALUE}, 486 {Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMALmm}, 487 488 {Double.MIN_VALUE, 0.0d, 0.0d}, 489 {-Double.MIN_VALUE, 0.0d, -0.0d}, 490 {Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE}, 491 {Double.MIN_VALUE, 1.0f, 2*Double.MIN_VALUE}, 492 493 // Make sure zero behavior is tested 494 {0.0d, 0.0d, 0.0d}, 495 {0.0d, -0.0d, -0.0d}, 496 {-0.0d, 0.0d, 0.0d}, 497 {-0.0d, -0.0d, -0.0d}, 498 {0.0d, infinityD, Double.MIN_VALUE}, 499 {0.0d, -infinityD, -Double.MIN_VALUE}, 500 {-0.0d, infinityD, Double.MIN_VALUE}, 509 failures += testNextAfterCase(testCases[i][0], testCases[i][1], 510 testCases[i][2]); 511 } 512 return failures; 513 } 514 515 /* ******************** nextUp tests ********************************* */ 516 517 public static int testFloatNextUp() { 518 int failures=0; 519 520 /* 521 * Each row of testCases represents one test case for nextUp; 522 * the first column is the input and the second column is the 523 * expected result. 524 */ 525 float testCases [][] = { 526 {NaNf, NaNf}, 527 {-infinityF, -Float.MAX_VALUE}, 528 {-Float.MAX_VALUE, -Float_MAX_VALUEmm}, 529 {-Float.MIN_NORMAL, -Float_MAX_SUBNORMAL}, 530 {-Float_MAX_SUBNORMAL, -Float_MAX_SUBNORMALmm}, 531 {-Float.MIN_VALUE, -0.0f}, 532 {-0.0f, Float.MIN_VALUE}, 533 {+0.0f, Float.MIN_VALUE}, 534 {Float.MIN_VALUE, Float.MIN_VALUE*2}, 535 {Float_MAX_SUBNORMALmm, Float_MAX_SUBNORMAL}, 536 {Float_MAX_SUBNORMAL, Float.MIN_NORMAL}, 537 {Float.MIN_NORMAL, Float.MIN_NORMAL+Float.MIN_VALUE}, 538 {Float_MAX_VALUEmm, Float.MAX_VALUE}, 539 {Float.MAX_VALUE, infinityF}, 540 {infinityF, infinityF} 541 }; 542 543 for(int i = 0; i < testCases.length; i++) { 544 failures+=Tests.test("Math.nextUp(float)", 545 testCases[i][0], Math.nextUp(testCases[i][0]), testCases[i][1]); 546 547 failures+=Tests.test("StrictMath.nextUp(float)", 548 testCases[i][0], StrictMath.nextUp(testCases[i][0]), testCases[i][1]); 549 } 550 551 return failures; 552 } 553 554 555 public static int testDoubleNextUp() { 556 int failures=0; 557 558 /* 559 * Each row of testCases represents one test case for nextUp; 560 * the first column is the input and the second column is the 561 * expected result. 562 */ 563 double testCases [][] = { 564 {NaNd, NaNd}, 565 {-infinityD, -Double.MAX_VALUE}, 566 {-Double.MAX_VALUE, -Double_MAX_VALUEmm}, 567 {-Double.MIN_NORMAL, -Double_MAX_SUBNORMAL}, 568 {-Double_MAX_SUBNORMAL, -Double_MAX_SUBNORMALmm}, 569 {-Double.MIN_VALUE, -0.0d}, 570 {-0.0d, Double.MIN_VALUE}, 571 {+0.0d, Double.MIN_VALUE}, 572 {Double.MIN_VALUE, Double.MIN_VALUE*2}, 573 {Double_MAX_SUBNORMALmm, Double_MAX_SUBNORMAL}, 574 {Double_MAX_SUBNORMAL, Double.MIN_NORMAL}, 575 {Double.MIN_NORMAL, Double.MIN_NORMAL+Double.MIN_VALUE}, 576 {Double_MAX_VALUEmm, Double.MAX_VALUE}, 577 {Double.MAX_VALUE, infinityD}, 578 {infinityD, infinityD} 579 }; 580 581 for(int i = 0; i < testCases.length; i++) { 582 failures+=Tests.test("Math.nextUp(double)", 583 testCases[i][0], Math.nextUp(testCases[i][0]), testCases[i][1]); 584 585 failures+=Tests.test("StrictMath.nextUp(double)", 586 testCases[i][0], StrictMath.nextUp(testCases[i][0]), testCases[i][1]); 587 } 588 589 return failures; 590 } 591 592 /* ******************** nextDown tests ********************************* */ 593 594 public static int testFloatNextDown() { 595 int failures=0; 596 597 /* 598 * Each row of testCases represents one test case for nextDown; 599 * the first column is the input and the second column is the 600 * expected result. 601 */ 602 float testCases [][] = { 603 {NaNf, NaNf}, 604 {-infinityF, -infinityF}, 605 {-Float.MAX_VALUE, -infinityF}, 606 {-Float_MAX_VALUEmm, -Float.MAX_VALUE}, 607 {-Float_MAX_SUBNORMAL, -Float.MIN_NORMAL}, 608 {-Float_MAX_SUBNORMALmm, -Float_MAX_SUBNORMAL}, 609 {-0.0f, -Float.MIN_VALUE}, 610 {+0.0f, -Float.MIN_VALUE}, 611 {Float.MIN_VALUE, 0.0f}, 612 {Float.MIN_VALUE*2, Float.MIN_VALUE}, 613 {Float_MAX_SUBNORMAL, Float_MAX_SUBNORMALmm}, 614 {Float.MIN_NORMAL, Float_MAX_SUBNORMAL}, 615 {Float.MIN_NORMAL+ 616 Float.MIN_VALUE, Float.MIN_NORMAL}, 617 {Float.MAX_VALUE, Float_MAX_VALUEmm}, 618 {infinityF, Float.MAX_VALUE}, 619 }; 620 621 for(int i = 0; i < testCases.length; i++) { 622 failures+=Tests.test("Math.nextDown(float)", 623 testCases[i][0], Math.nextDown(testCases[i][0]), testCases[i][1]); 624 625 failures+=Tests.test("StrictMath.nextDown(float)", 626 testCases[i][0], StrictMath.nextDown(testCases[i][0]), testCases[i][1]); 627 } 628 629 return failures; 630 } 631 632 633 public static int testDoubleNextDown() { 634 int failures=0; 635 636 /* 637 * Each row of testCases represents one test case for nextDown; 638 * the first column is the input and the second column is the 639 * expected result. 640 */ 641 double testCases [][] = { 642 {NaNd, NaNd}, 643 {-infinityD, -infinityD}, 644 {-Double.MAX_VALUE, -infinityD}, 645 {-Double_MAX_VALUEmm, -Double.MAX_VALUE}, 646 {-Double_MAX_SUBNORMAL, -Double.MIN_NORMAL}, 647 {-Double_MAX_SUBNORMALmm, -Double_MAX_SUBNORMAL}, 648 {-0.0d, -Double.MIN_VALUE}, 649 {+0.0d, -Double.MIN_VALUE}, 650 {Double.MIN_VALUE, 0.0d}, 651 {Double.MIN_VALUE*2, Double.MIN_VALUE}, 652 {Double_MAX_SUBNORMAL, Double_MAX_SUBNORMALmm}, 653 {Double.MIN_NORMAL, Double_MAX_SUBNORMAL}, 654 {Double.MIN_NORMAL+ 655 Double.MIN_VALUE, Double.MIN_NORMAL}, 656 {Double.MAX_VALUE, Double_MAX_VALUEmm}, 657 {infinityD, Double.MAX_VALUE}, 658 }; 659 660 for(int i = 0; i < testCases.length; i++) { 661 failures+=Tests.test("Math.nextDown(double)", 662 testCases[i][0], Math.nextDown(testCases[i][0]), testCases[i][1]); 663 664 failures+=Tests.test("StrictMath.nextDown(double)", 665 testCases[i][0], StrictMath.nextDown(testCases[i][0]), testCases[i][1]); 666 } 667 668 return failures; 669 } 670 671 672 /* ********************** boolean tests ****************************** */ 673 674 /* 675 * Combined tests for boolean functions, isFinite, isInfinite, 676 * isNaN, isUnordered. 677 */ 678 679 public static int testFloatBooleanMethods() { 680 int failures = 0; 681 682 float testCases [] = { 683 NaNf, 684 -infinityF, 685 infinityF, 686 -Float.MAX_VALUE, 687 -3.0f, 688 -1.0f, 689 -Float.MIN_NORMAL, 690 -Float_MAX_SUBNORMALmm, 691 -Float_MAX_SUBNORMAL, 692 -Float.MIN_VALUE, 693 -0.0f, 694 +0.0f, 695 Float.MIN_VALUE, 696 Float_MAX_SUBNORMALmm, 697 Float_MAX_SUBNORMAL, 698 Float.MIN_NORMAL, 699 1.0f, 700 3.0f, 701 Float_MAX_VALUEmm, 702 Float.MAX_VALUE 703 }; 704 705 for(int i = 0; i < testCases.length; i++) { 706 // isNaN 707 failures+=Tests.test("Float.isNaN(float)", testCases[i], 708 Float.isNaN(testCases[i]), (i ==0)); 709 710 // isFinite 711 failures+=Tests.test("Float.isFinite(float)", testCases[i], 712 Float.isFinite(testCases[i]), (i >= 3)); 713 714 // isInfinite 715 failures+=Tests.test("Float.isInfinite(float)", testCases[i], 716 Float.isInfinite(testCases[i]), (i==1 || i==2)); 717 718 // isUnorderd 719 for(int j = 0; j < testCases.length; j++) { 720 failures+=Tests.test("Tests.isUnordered(float, float)", testCases[i],testCases[j], 721 Tests.isUnordered(testCases[i],testCases[j]), (i==0 || j==0)); 722 } 723 } 724 725 return failures; 726 } 727 728 public static int testDoubleBooleanMethods() { 729 int failures = 0; 730 boolean result = false; 731 732 double testCases [] = { 733 NaNd, 734 -infinityD, 735 infinityD, 736 -Double.MAX_VALUE, 737 -3.0d, 738 -1.0d, 739 -Double.MIN_NORMAL, 740 -Double_MAX_SUBNORMALmm, 741 -Double_MAX_SUBNORMAL, 742 -Double.MIN_VALUE, 743 -0.0d, 744 +0.0d, 745 Double.MIN_VALUE, 746 Double_MAX_SUBNORMALmm, 747 Double_MAX_SUBNORMAL, 748 Double.MIN_NORMAL, 749 1.0d, 750 3.0d, 751 Double_MAX_VALUEmm, 752 Double.MAX_VALUE 753 }; 754 755 for(int i = 0; i < testCases.length; i++) { 756 // isNaN 757 failures+=Tests.test("Double.isNaN(double)", testCases[i], 758 Double.isNaN(testCases[i]), (i ==0)); 759 760 // isFinite 761 failures+=Tests.test("Double.isFinite(double)", testCases[i], 762 Double.isFinite(testCases[i]), (i >= 3)); 763 764 // isInfinite 765 failures+=Tests.test("Double.isInfinite(double)", testCases[i], 766 Double.isInfinite(testCases[i]), (i==1 || i==2)); 767 768 // isUnorderd 770 failures+=Tests.test("Tests.isUnordered(double, double)", testCases[i],testCases[j], 771 Tests.isUnordered(testCases[i],testCases[j]), (i==0 || j==0)); 772 } 773 } 774 775 return failures; 776 } 777 778 /* ******************** copySign tests******************************** */ 779 780 public static int testFloatCopySign() { 781 int failures = 0; 782 783 // testCases[0] are logically positive numbers; 784 // testCases[1] are negative numbers. 785 float testCases [][] = { 786 {+0.0f, 787 Float.MIN_VALUE, 788 Float_MAX_SUBNORMALmm, 789 Float_MAX_SUBNORMAL, 790 Float.MIN_NORMAL, 791 1.0f, 792 3.0f, 793 Float_MAX_VALUEmm, 794 Float.MAX_VALUE, 795 infinityF, 796 }, 797 {-infinityF, 798 -Float.MAX_VALUE, 799 -3.0f, 800 -1.0f, 801 -Float.MIN_NORMAL, 802 -Float_MAX_SUBNORMALmm, 803 -Float_MAX_SUBNORMAL, 804 -Float.MIN_VALUE, 805 -0.0f} 806 }; 807 808 float NaNs[] = {Float.intBitsToFloat(0x7fc00000), // "positive" NaN 809 Float.intBitsToFloat(0xFfc00000)}; // "negative" NaN 810 811 // Tests shared between raw and non-raw versions 812 for(int i = 0; i < 2; i++) { 813 for(int j = 0; j < 2; j++) { 814 for(int m = 0; m < testCases[i].length; m++) { 815 for(int n = 0; n < testCases[j].length; n++) { 816 // copySign(magnitude, sign) 817 failures+=Tests.test("Math.copySign(float,float)", 818 testCases[i][m],testCases[j][n], 819 Math.copySign(testCases[i][m], testCases[j][n]), 820 (j==0?1.0f:-1.0f)*Math.abs(testCases[i][m]) ); 821 844 testCases[i][m], NaNs[j], 845 StrictMath.copySign(testCases[i][m], NaNs[j]), 846 Math.abs(testCases[i][m]) ); 847 } 848 } 849 } 850 851 return failures; 852 } 853 854 public static int testDoubleCopySign() { 855 int failures = 0; 856 857 // testCases[0] are logically positive numbers; 858 // testCases[1] are negative numbers. 859 double testCases [][] = { 860 {+0.0d, 861 Double.MIN_VALUE, 862 Double_MAX_SUBNORMALmm, 863 Double_MAX_SUBNORMAL, 864 Double.MIN_NORMAL, 865 1.0d, 866 3.0d, 867 Double_MAX_VALUEmm, 868 Double.MAX_VALUE, 869 infinityD, 870 }, 871 {-infinityD, 872 -Double.MAX_VALUE, 873 -3.0d, 874 -1.0d, 875 -Double.MIN_NORMAL, 876 -Double_MAX_SUBNORMALmm, 877 -Double_MAX_SUBNORMAL, 878 -Double.MIN_VALUE, 879 -0.0d} 880 }; 881 882 double NaNs[] = {Double.longBitsToDouble(0x7ff8000000000000L), // "positive" NaN 883 Double.longBitsToDouble(0xfff8000000000000L), // "negative" NaN 884 Double.longBitsToDouble(0x7FF0000000000001L), 885 Double.longBitsToDouble(0xFFF0000000000001L), 886 Double.longBitsToDouble(0x7FF8555555555555L), 887 Double.longBitsToDouble(0xFFF8555555555555L), 888 Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), 889 Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), 890 Double.longBitsToDouble(0x7FFDeadBeef00000L), 891 Double.longBitsToDouble(0xFFFDeadBeef00000L), 892 Double.longBitsToDouble(0x7FFCafeBabe00000L), 893 Double.longBitsToDouble(0xFFFCafeBabe00000L)}; 894 895 // Tests shared between Math and StrictMath versions 944 failures+=Tests.test("Math.scalb(float,int)", 945 value, scale_factor, 946 Math.scalb(value, scale_factor), expected); 947 948 failures+=Tests.test("Math.scalb(float,int)", 949 -value, scale_factor, 950 Math.scalb(-value, scale_factor), -expected); 951 952 failures+=Tests.test("StrictMath.scalb(float,int)", 953 value, scale_factor, 954 StrictMath.scalb(value, scale_factor), expected); 955 956 failures+=Tests.test("StrictMath.scalb(float,int)", 957 -value, scale_factor, 958 StrictMath.scalb(-value, scale_factor), -expected); 959 return failures; 960 } 961 962 public static int testFloatScalb() { 963 int failures=0; 964 int MAX_SCALE = Float.MAX_EXPONENT + -Float.MIN_EXPONENT + 965 FloatConsts.SIGNIFICAND_WIDTH + 1; 966 967 968 // Arguments x, where scalb(x,n) is x for any n. 969 float [] identityTestCases = {NaNf, 970 -0.0f, 971 +0.0f, 972 infinityF, 973 -infinityF 974 }; 975 976 float [] subnormalTestCases = { 977 Float.MIN_VALUE, 978 3.0f*Float.MIN_VALUE, 979 Float_MAX_SUBNORMALmm, 980 Float_MAX_SUBNORMAL 981 }; 982 983 float [] someTestCases = { 984 Float.MIN_VALUE, 985 3.0f*Float.MIN_VALUE, 986 Float_MAX_SUBNORMALmm, 987 Float_MAX_SUBNORMAL, 988 Float.MIN_NORMAL, 989 1.0f, 990 2.0f, 991 3.0f, 992 (float)Math.PI, 993 Float_MAX_VALUEmm, 994 Float.MAX_VALUE 995 }; 996 997 int [] oneMultiplyScalingFactors = { 998 Float.MIN_EXPONENT, 999 Float.MIN_EXPONENT+1, 1000 -3, 1001 -2, 1002 -1, 1003 0, 1004 1, 1005 2, 1006 3, 1007 Float.MAX_EXPONENT-1, 1008 Float.MAX_EXPONENT 1009 }; 1010 1011 int [] manyScalingFactors = { 1012 Integer.MIN_VALUE, 1013 Integer.MIN_VALUE+1, 1014 -MAX_SCALE -1, 1015 -MAX_SCALE, 1016 -MAX_SCALE+1, 1017 1018 2*Float.MIN_EXPONENT-1, // -253 1019 2*Float.MIN_EXPONENT, // -252 1020 2*Float.MIN_EXPONENT+1, // -251 1021 1022 Float.MIN_EXPONENT - FloatConsts.SIGNIFICAND_WIDTH, 1023 FloatConsts.MIN_SUB_EXPONENT, 1024 -Float.MAX_EXPONENT, // -127 1025 Float.MIN_EXPONENT, // -126 1026 1027 -2, 1028 -1, 1029 0, 1030 1, 1031 2, 1032 1033 Float.MAX_EXPONENT-1, // 126 1034 Float.MAX_EXPONENT, // 127 1035 Float.MAX_EXPONENT+1, // 128 1036 1037 2*Float.MAX_EXPONENT-1, // 253 1038 2*Float.MAX_EXPONENT, // 254 1039 2*Float.MAX_EXPONENT+1, // 255 1040 1041 MAX_SCALE-1, 1042 MAX_SCALE, 1043 MAX_SCALE+1, 1044 Integer.MAX_VALUE-1, 1045 Integer.MAX_VALUE 1046 }; 1047 1048 // Test cases where scaling is always a no-op 1049 for(int i=0; i < identityTestCases.length; i++) { 1050 for(int j=0; j < manyScalingFactors.length; j++) { 1051 failures += testScalbCase(identityTestCases[i], 1052 manyScalingFactors[j], 1053 identityTestCases[i]); 1054 } 1055 } 1056 1057 // Test cases where result is 0.0 or infinity due to magnitude 1058 // of the scaling factor 1059 for(int i=0; i < someTestCases.length; i++) { 1066 Math.copySign( (scaleFactor>0?infinityF:0.0f), value) ); 1067 } 1068 } 1069 } 1070 1071 // Test cases that could be done with one floating-point 1072 // multiply. 1073 for(int i=0; i < someTestCases.length; i++) { 1074 for(int j=0; j < oneMultiplyScalingFactors.length; j++) { 1075 int scaleFactor = oneMultiplyScalingFactors[j]; 1076 float value = someTestCases[i]; 1077 1078 failures+=testScalbCase(value, 1079 scaleFactor, 1080 value*powerOfTwoF(scaleFactor)); 1081 } 1082 } 1083 1084 // Create 2^MAX_EXPONENT 1085 float twoToTheMaxExp = 1.0f; // 2^0 1086 for(int i = 0; i < Float.MAX_EXPONENT; i++) 1087 twoToTheMaxExp *=2.0f; 1088 1089 // Scale-up subnormal values until they all overflow 1090 for(int i=0; i < subnormalTestCases.length; i++) { 1091 float scale = 1.0f; // 2^j 1092 float value = subnormalTestCases[i]; 1093 1094 for(int j=Float.MAX_EXPONENT*2; j < MAX_SCALE; j++) { // MAX_SCALE -1 should cause overflow 1095 int scaleFactor = j; 1096 1097 failures+=testScalbCase(value, 1098 scaleFactor, 1099 (Tests.ilogb(value) +j > Float.MAX_EXPONENT ) ? 1100 Math.copySign(infinityF, value) : // overflow 1101 // calculate right answer 1102 twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) ); 1103 scale*=2.0f; 1104 } 1105 } 1106 1107 // Scale down a large number until it underflows. By scaling 1108 // down MAX_NORMALmm, the first subnormal result will be exact 1109 // but the next one will round -- all those results can be 1110 // checked by halving a separate value in the loop. Actually, 1111 // we can keep halving and checking until the product is zero 1112 // since: 1113 // 1114 // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact 1115 // it will round *up* 1116 // 1117 // 2. When rounding first occurs in the expected product, it 1118 // too rounds up, to 2^-MAX_EXPONENT. 1119 // 1152 value, scale_factor, 1153 Math.scalb(value, scale_factor), expected); 1154 1155 failures+=Tests.test("Math.scalb(double,int)", 1156 -value, scale_factor, 1157 Math.scalb(-value, scale_factor), -expected); 1158 1159 failures+=Tests.test("StrictMath.scalb(double,int)", 1160 value, scale_factor, 1161 StrictMath.scalb(value, scale_factor), expected); 1162 1163 failures+=Tests.test("StrictMath.scalb(double,int)", 1164 -value, scale_factor, 1165 StrictMath.scalb(-value, scale_factor), -expected); 1166 1167 return failures; 1168 } 1169 1170 public static int testDoubleScalb() { 1171 int failures=0; 1172 int MAX_SCALE = Double.MAX_EXPONENT + -Double.MIN_EXPONENT + 1173 DoubleConsts.SIGNIFICAND_WIDTH + 1; 1174 1175 1176 // Arguments x, where scalb(x,n) is x for any n. 1177 double [] identityTestCases = {NaNd, 1178 -0.0, 1179 +0.0, 1180 infinityD, 1181 }; 1182 1183 double [] subnormalTestCases = { 1184 Double.MIN_VALUE, 1185 3.0d*Double.MIN_VALUE, 1186 Double_MAX_SUBNORMALmm, 1187 Double_MAX_SUBNORMAL 1188 }; 1189 1190 double [] someTestCases = { 1191 Double.MIN_VALUE, 1192 3.0d*Double.MIN_VALUE, 1193 Double_MAX_SUBNORMALmm, 1194 Double_MAX_SUBNORMAL, 1195 Double.MIN_NORMAL, 1196 1.0d, 1197 2.0d, 1198 3.0d, 1199 Math.PI, 1200 Double_MAX_VALUEmm, 1201 Double.MAX_VALUE 1202 }; 1203 1204 int [] oneMultiplyScalingFactors = { 1205 Double.MIN_EXPONENT, 1206 Double.MIN_EXPONENT+1, 1207 -3, 1208 -2, 1209 -1, 1210 0, 1211 1, 1212 2, 1213 3, 1214 Double.MAX_EXPONENT-1, 1215 Double.MAX_EXPONENT 1216 }; 1217 1218 int [] manyScalingFactors = { 1219 Integer.MIN_VALUE, 1220 Integer.MIN_VALUE+1, 1221 -MAX_SCALE -1, 1222 -MAX_SCALE, 1223 -MAX_SCALE+1, 1224 1225 2*Double.MIN_EXPONENT-1, // -2045 1226 2*Double.MIN_EXPONENT, // -2044 1227 2*Double.MIN_EXPONENT+1, // -2043 1228 1229 Double.MIN_EXPONENT, // -1022 1230 Double.MIN_EXPONENT - DoubleConsts.SIGNIFICAND_WIDTH, 1231 DoubleConsts.MIN_SUB_EXPONENT, 1232 -Double.MAX_EXPONENT, // -1023 1233 Double.MIN_EXPONENT, // -1022 1234 1235 -2, 1236 -1, 1237 0, 1238 1, 1239 2, 1240 1241 Double.MAX_EXPONENT-1, // 1022 1242 Double.MAX_EXPONENT, // 1023 1243 Double.MAX_EXPONENT+1, // 1024 1244 1245 2*Double.MAX_EXPONENT-1, // 2045 1246 2*Double.MAX_EXPONENT, // 2046 1247 2*Double.MAX_EXPONENT+1, // 2047 1248 1249 MAX_SCALE-1, 1250 MAX_SCALE, 1251 MAX_SCALE+1, 1252 Integer.MAX_VALUE-1, 1253 Integer.MAX_VALUE 1254 }; 1255 1256 // Test cases where scaling is always a no-op 1257 for(int i=0; i < identityTestCases.length; i++) { 1258 for(int j=0; j < manyScalingFactors.length; j++) { 1259 failures += testScalbCase(identityTestCases[i], 1260 manyScalingFactors[j], 1261 identityTestCases[i]); 1262 } 1263 } 1264 1265 // Test cases where result is 0.0 or infinity due to magnitude 1266 // of the scaling factor 1267 for(int i=0; i < someTestCases.length; i++) { 1274 Math.copySign( (scaleFactor>0?infinityD:0.0), value) ); 1275 } 1276 } 1277 } 1278 1279 // Test cases that could be done with one floating-point 1280 // multiply. 1281 for(int i=0; i < someTestCases.length; i++) { 1282 for(int j=0; j < oneMultiplyScalingFactors.length; j++) { 1283 int scaleFactor = oneMultiplyScalingFactors[j]; 1284 double value = someTestCases[i]; 1285 1286 failures+=testScalbCase(value, 1287 scaleFactor, 1288 value*powerOfTwoD(scaleFactor)); 1289 } 1290 } 1291 1292 // Create 2^MAX_EXPONENT 1293 double twoToTheMaxExp = 1.0; // 2^0 1294 for(int i = 0; i < Double.MAX_EXPONENT; i++) 1295 twoToTheMaxExp *=2.0; 1296 1297 // Scale-up subnormal values until they all overflow 1298 for(int i=0; i < subnormalTestCases.length; i++) { 1299 double scale = 1.0; // 2^j 1300 double value = subnormalTestCases[i]; 1301 1302 for(int j=Double.MAX_EXPONENT*2; j < MAX_SCALE; j++) { // MAX_SCALE -1 should cause overflow 1303 int scaleFactor = j; 1304 1305 failures+=testScalbCase(value, 1306 scaleFactor, 1307 (Tests.ilogb(value) +j > Double.MAX_EXPONENT ) ? 1308 Math.copySign(infinityD, value) : // overflow 1309 // calculate right answer 1310 twoToTheMaxExp*(twoToTheMaxExp*(scale*value)) ); 1311 scale*=2.0; 1312 } 1313 } 1314 1315 // Scale down a large number until it underflows. By scaling 1316 // down MAX_NORMALmm, the first subnormal result will be exact 1317 // but the next one will round -- all those results can be 1318 // checked by halving a separate value in the loop. Actually, 1319 // we can keep halving and checking until the product is zero 1320 // since: 1321 // 1322 // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact 1323 // it will round *up* 1324 // 1325 // 2. When rounding first occurs in the expected product, it 1326 // too rounds up, to 2^-MAX_EXPONENT. 1327 // 1328 // Halving expected after rounding happends to give the same 1329 // result as the scalb operation. 1330 double expected = Double_MAX_VALUEmm *0.5f; 1331 for(int i = -1; i > -MAX_SCALE; i--) { 1332 failures+=testScalbCase(Double_MAX_VALUEmm, i, expected); 1333 1334 expected *= 0.5; 1335 } 1336 1337 // Tricky rounding tests: 1338 // Scale down a large number into subnormal range such that if 1339 // scalb is being implemented with multiple floating-point 1340 // multiplies, the value would round twice if the multiplies 1341 // were done in the wrong order. 1342 1343 double value = 0x1.000000000000bP-1; 1344 expected = 0x0.2000000000001P-1022; 1345 for(int i = 0; i < Double.MAX_EXPONENT+2; i++) { 1346 failures+=testScalbCase(value, 1347 -1024-i, 1348 expected); 1349 value *=2.0; 1350 } 1351 1352 return failures; 1353 } 1354 1355 /* ************************* ulp tests ******************************* */ 1356 1357 1358 /* 1359 * Test Math.ulp and StrictMath.ulp with +d and -d. 1360 */ 1361 static int testUlpCase(float f, float expected) { 1362 float minus_f = -f; 1363 int failures=0; 1364 1365 failures+=Tests.test("Math.ulp(float)", f, 1381 Math.ulp(d), expected); 1382 failures+=Tests.test("Math.ulp(double)", minus_d, 1383 Math.ulp(minus_d), expected); 1384 failures+=Tests.test("StrictMath.ulp(double)", d, 1385 StrictMath.ulp(d), expected); 1386 failures+=Tests.test("StrictMath.ulp(double)", minus_d, 1387 StrictMath.ulp(minus_d), expected); 1388 return failures; 1389 } 1390 1391 public static int testFloatUlp() { 1392 int failures = 0; 1393 float [] specialValues = {NaNf, 1394 Float.POSITIVE_INFINITY, 1395 +0.0f, 1396 +1.0f, 1397 +2.0f, 1398 +16.0f, 1399 +Float.MIN_VALUE, 1400 +Float_MAX_SUBNORMAL, 1401 +Float.MIN_NORMAL, 1402 +Float.MAX_VALUE 1403 }; 1404 1405 float [] specialResults = {NaNf, 1406 Float.POSITIVE_INFINITY, 1407 Float.MIN_VALUE, 1408 powerOfTwoF(-23), 1409 powerOfTwoF(-22), 1410 powerOfTwoF(-19), 1411 Float.MIN_VALUE, 1412 Float.MIN_VALUE, 1413 Float.MIN_VALUE, 1414 powerOfTwoF(104) 1415 }; 1416 1417 // Special value tests 1418 for(int i = 0; i < specialValues.length; i++) { 1419 failures += testUlpCase(specialValues[i], specialResults[i]); 1420 } 1421 1422 1423 // Normal exponent tests 1424 for(int i = Float.MIN_EXPONENT; i <= Float.MAX_EXPONENT; i++) { 1425 float expected; 1426 1427 // Create power of two 1428 float po2 = powerOfTwoF(i); 1429 expected = Math.scalb(1.0f, i - (FloatConsts.SIGNIFICAND_WIDTH-1)); 1430 1431 failures += testUlpCase(po2, expected); 1432 1433 // Generate some random bit patterns for the significand 1434 for(int j = 0; j < 10; j++) { 1435 int randSignif = rand.nextInt(); 1436 float randFloat; 1437 1438 randFloat = Float.intBitsToFloat( // Exponent 1439 (Float.floatToIntBits(po2)& 1440 (~FloatConsts.SIGNIF_BIT_MASK)) | 1441 // Significand 1442 (randSignif & 1443 FloatConsts.SIGNIF_BIT_MASK) ); 1444 1445 failures += testUlpCase(randFloat, expected); 1446 } 1447 1448 if (i > Float.MIN_EXPONENT) { 1449 float po2minus = Math.nextAfter(po2, 1450 Float.NEGATIVE_INFINITY); 1451 failures += testUlpCase(po2minus, expected/2.0f); 1452 } 1453 } 1454 1455 // Subnormal tests 1456 1457 /* 1458 * Start with MIN_VALUE, left shift, test high value, low 1459 * values, and random in between. 1460 * 1461 * Use nextAfter to calculate, high value of previous binade, 1462 * loop count i will indicate how many random bits, if any are 1463 * needed. 1464 */ 1465 1466 float top=Float.MIN_VALUE; 1467 for( int i = 1; 1468 i < FloatConsts.SIGNIFICAND_WIDTH; 1486 (rand.nextInt() & mask ) ) ; 1487 1488 failures += testUlpCase(randFloat, Float.MIN_VALUE); 1489 } 1490 } 1491 } 1492 1493 return failures; 1494 } 1495 1496 public static int testDoubleUlp() { 1497 int failures = 0; 1498 double [] specialValues = {NaNd, 1499 Double.POSITIVE_INFINITY, 1500 +0.0d, 1501 +1.0d, 1502 +2.0d, 1503 +16.0d, 1504 +Double.MIN_VALUE, 1505 +Double_MAX_SUBNORMAL, 1506 +Double.MIN_NORMAL, 1507 +Double.MAX_VALUE 1508 }; 1509 1510 double [] specialResults = {NaNf, 1511 Double.POSITIVE_INFINITY, 1512 Double.MIN_VALUE, 1513 powerOfTwoD(-52), 1514 powerOfTwoD(-51), 1515 powerOfTwoD(-48), 1516 Double.MIN_VALUE, 1517 Double.MIN_VALUE, 1518 Double.MIN_VALUE, 1519 powerOfTwoD(971) 1520 }; 1521 1522 // Special value tests 1523 for(int i = 0; i < specialValues.length; i++) { 1524 failures += testUlpCase(specialValues[i], specialResults[i]); 1525 } 1526 1527 1528 // Normal exponent tests 1529 for(int i = Double.MIN_EXPONENT; i <= Double.MAX_EXPONENT; i++) { 1530 double expected; 1531 1532 // Create power of two 1533 double po2 = powerOfTwoD(i); 1534 expected = Math.scalb(1.0, i - (DoubleConsts.SIGNIFICAND_WIDTH-1)); 1535 1536 failures += testUlpCase(po2, expected); 1537 1538 // Generate some random bit patterns for the significand 1539 for(int j = 0; j < 10; j++) { 1540 long randSignif = rand.nextLong(); 1541 double randDouble; 1542 1543 randDouble = Double.longBitsToDouble( // Exponent 1544 (Double.doubleToLongBits(po2)& 1545 (~DoubleConsts.SIGNIF_BIT_MASK)) | 1546 // Significand 1547 (randSignif & 1548 DoubleConsts.SIGNIF_BIT_MASK) ); 1549 1550 failures += testUlpCase(randDouble, expected); 1551 } 1552 1553 if (i > Double.MIN_EXPONENT) { 1554 double po2minus = Math.nextAfter(po2, 1555 Double.NEGATIVE_INFINITY); 1556 failures += testUlpCase(po2minus, expected/2.0f); 1557 } 1558 } 1559 1560 // Subnormal tests 1561 1562 /* 1563 * Start with MIN_VALUE, left shift, test high value, low 1564 * values, and random in between. 1565 * 1566 * Use nextAfter to calculate, high value of previous binade, 1567 * loop count i will indicate how many random bits, if any are 1568 * needed. 1569 */ 1570 1571 double top=Double.MIN_VALUE; 1572 for( int i = 1; 1573 i < DoubleConsts.SIGNIFICAND_WIDTH; 1587 int mask = ~((~0)<<(i-1)); 1588 double randDouble = Double.longBitsToDouble( // Exponent 1589 Double.doubleToLongBits(top) | 1590 // Significand 1591 (rand.nextLong() & mask ) ) ; 1592 1593 failures += testUlpCase(randDouble, Double.MIN_VALUE); 1594 } 1595 } 1596 } 1597 1598 return failures; 1599 } 1600 1601 public static int testFloatSignum() { 1602 int failures = 0; 1603 float testCases [][] = { 1604 {NaNf, NaNf}, 1605 {-infinityF, -1.0f}, 1606 {-Float.MAX_VALUE, -1.0f}, 1607 {-Float.MIN_NORMAL, -1.0f}, 1608 {-1.0f, -1.0f}, 1609 {-2.0f, -1.0f}, 1610 {-Float_MAX_SUBNORMAL, -1.0f}, 1611 {-Float.MIN_VALUE, -1.0f}, 1612 {-0.0f, -0.0f}, 1613 {+0.0f, +0.0f}, 1614 {Float.MIN_VALUE, 1.0f}, 1615 {Float_MAX_SUBNORMALmm, 1.0f}, 1616 {Float_MAX_SUBNORMAL, 1.0f}, 1617 {Float.MIN_NORMAL, 1.0f}, 1618 {1.0f, 1.0f}, 1619 {2.0f, 1.0f}, 1620 {Float_MAX_VALUEmm, 1.0f}, 1621 {Float.MAX_VALUE, 1.0f}, 1622 {infinityF, 1.0f} 1623 }; 1624 1625 for(int i = 0; i < testCases.length; i++) { 1626 failures+=Tests.test("Math.signum(float)", 1627 testCases[i][0], Math.signum(testCases[i][0]), testCases[i][1]); 1628 failures+=Tests.test("StrictMath.signum(float)", 1629 testCases[i][0], StrictMath.signum(testCases[i][0]), testCases[i][1]); 1630 } 1631 1632 return failures; 1633 } 1634 1635 public static int testDoubleSignum() { 1636 int failures = 0; 1637 double testCases [][] = { 1638 {NaNd, NaNd}, 1639 {-infinityD, -1.0}, 1640 {-Double.MAX_VALUE, -1.0}, 1641 {-Double.MIN_NORMAL, -1.0}, 1642 {-1.0, -1.0}, 1643 {-2.0, -1.0}, 1644 {-Double_MAX_SUBNORMAL, -1.0}, 1645 {-Double.MIN_VALUE, -1.0d}, 1646 {-0.0d, -0.0d}, 1647 {+0.0d, +0.0d}, 1648 {Double.MIN_VALUE, 1.0}, 1649 {Double_MAX_SUBNORMALmm, 1.0}, 1650 {Double_MAX_SUBNORMAL, 1.0}, 1651 {Double.MIN_NORMAL, 1.0}, 1652 {1.0, 1.0}, 1653 {2.0, 1.0}, 1654 {Double_MAX_VALUEmm, 1.0}, 1655 {Double.MAX_VALUE, 1.0}, 1656 {infinityD, 1.0} 1657 }; 1658 1659 for(int i = 0; i < testCases.length; i++) { 1660 failures+=Tests.test("Math.signum(double)", 1661 testCases[i][0], Math.signum(testCases[i][0]), testCases[i][1]); 1662 failures+=Tests.test("StrictMath.signum(double)", 1663 testCases[i][0], StrictMath.signum(testCases[i][0]), testCases[i][1]); 1664 } 1665 1666 return failures; 1667 } 1668 1669 1670 public static void main(String argv[]) { 1671 int failures = 0; |