IeeeRecommendedTests.java revision 809
809N/A * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved. 809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 809N/A * This code is free software; you can redistribute it and/or modify it 809N/A * under the terms of the GNU General Public License version 2 only, as 809N/A * published by the Free Software Foundation. 809N/A * This code is distributed in the hope that it will be useful, but WITHOUT 809N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 809N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 809N/A * version 2 for more details (a copy is included in the LICENSE file that 809N/A * accompanied this code). 809N/A * You should have received a copy of the GNU General Public License version 809N/A * 2 along with this work; if not, write to the Free Software Foundation, 809N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 809N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 809N/A * CA 95054 USA or visit www.sun.com if you need additional information or 809N/A * @bug 4860891 4826732 4780454 4939441 4826652 809N/A * @summary Tests for IEEE 754[R] recommended functions and similar methods 809N/A * @author Joseph D. Darcy 809N/A * @run main IeeeRecommendedTests 809N/A // Initialize shared random number generator 809N/A * Returns a floating-point power of two in the normal range. 809N/A * Returns a floating-point power of two in the normal range. 809N/A /* ******************** getExponent tests ****************************** */ 809N/A * The tests for getExponent should test the special values (NaN, +/- 809N/A * infinity, etc.), test the endpoints of each binade (set of 809N/A * floating-point values with the same exponent), and for good 809N/A * measure, test some random values within each binade. Testing 809N/A * the endpoints of each binade includes testing both positive and 809N/A * negative numbers. Subnormal values with different normalized 809N/A * exponents should be tested too. Both Math and StrictMath 809N/A * methods should return the same results. 809N/A * Test Math.getExponent and StrictMath.getExponent with +d and -d. 809N/A * Test Math.getExponent and StrictMath.getExponent with +d and -d. 809N/A // Normal exponent tests 809N/A // Generate some random bit patterns for the significand 809N/A for(
int j =
0; j <
10; j++) {
809N/A // Subnormal exponent tests 809N/A * Start with MIN_VALUE, left shift, test high value, low 809N/A * values, and random in between. 809N/A * Use nextAfter to calculate, high value of previous binade, 809N/A * loop count i will indicate how many random bits, if any are 809N/A // Test largest value in next smaller binade 809N/A if (i >=
3) {
// (i == 1) would test 0.0; 809N/A // (i == 2) would just retest MIN_VALUE 809N/A // create a bit mask with (i-1) 1's in the low order 809N/A // Normal exponent tests 809N/A // Generate some random bit patterns for the significand 809N/A for(
int j =
0; j <
10; j++) {
809N/A // Subnormal exponent tests 809N/A * Start with MIN_VALUE, left shift, test high value, low 809N/A * values, and random in between. 809N/A * Use nextAfter to calculate, high value of previous binade; 809N/A * loop count i will indicate how many random bits, if any are 809N/A // Test largest value in next smaller binade 809N/A if (i >=
3) {
// (i == 1) would test 0.0; 809N/A // (i == 2) would just retest MIN_VALUE 809N/A // create a bit mask with (i-1) 1's in the low order 809N/A /* ******************** nextAfter tests ****************************** */ 809N/A * Each row of the testCases matrix represents one test case 809N/A * for nexAfter; given the input of the first two columns, the 809N/A * result in the last column is expected. 809N/A // Make sure zero behavior is tested 809N/A * Each row of the testCases matrix represents one test case 809N/A * for nexAfter; given the input of the first two columns, the 809N/A * result in the last column is expected. 809N/A // Make sure zero behavior is tested 809N/A /* ******************** nextUp tests ********************************* */ 809N/A * Each row of testCases represents one test case for nextUp; 809N/A * the first column is the input and the second column is the 809N/A * Each row of testCases represents one test case for nextUp; 809N/A * the first column is the input and the second column is the 809N/A /* ******************** nextDown tests ********************************* */ 809N/A * Each row of testCases represents one test case for nextDown; 809N/A * the first column is the input and the second column is the 809N/A * Each row of testCases represents one test case for nextDown; 809N/A * the first column is the input and the second column is the 809N/A /* ********************** boolean tests ****************************** */ 809N/A * Combined tests for boolean functions, isFinite, isInfinite, 809N/A /* ******************** copySign tests******************************** */ 809N/A // testCases[0] are logically positive numbers; 809N/A // testCases[1] are negative numbers. 809N/A // Tests shared between raw and non-raw versions 809N/A for(
int i =
0; i <
2; i++) {
809N/A for(
int j =
0; j <
2; j++) {
809N/A // copySign(magnitude, sign) 809N/A // For rawCopySign, NaN may effectively have either sign bit 809N/A // while for copySign NaNs are treated as if they always have 809N/A // a zero sign bit (i.e. as positive numbers) 809N/A for(
int i =
0; i <
2; i++) {
809N/A // copySign(magnitude, sign) 809N/A // testCases[0] are logically positive numbers; 809N/A // testCases[1] are negative numbers. 809N/A // Tests shared between Math and StrictMath versions 809N/A for(
int i =
0; i <
2; i++) {
809N/A for(
int j =
0; j <
2; j++) {
809N/A // copySign(magnitude, sign) 809N/A // For Math.copySign, NaN may effectively have either sign bit 809N/A // while for StrictMath.copySign NaNs are treated as if they 809N/A // always have a zero sign bit (i.e. as positive numbers) 809N/A for(
int i =
0; i <
2; i++) {
809N/A // copySign(magnitude, sign) 809N/A /* ************************ scalb tests ******************************* */ 809N/A // Arguments x, where scalb(x,n) is x for any n. 809N/A // Test cases where scaling is always a no-op 809N/A // Test cases where result is 0.0 or infinity due to magnitude 809N/A // of the scaling factor 809N/A // Test cases that could be done with one floating-point 809N/A // Create 2^MAX_EXPONENT 809N/A // Scale-up subnormal values until they all overflow 809N/A // calculate right answer 809N/A // Scale down a large number until it underflows. By scaling 809N/A // down MAX_NORMALmm, the first subnormal result will be exact 809N/A // but the next one will round -- all those results can be 809N/A // checked by halving a separate value in the loop. Actually, 809N/A // we can keep halving and checking until the product is zero 809N/A // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact 809N/A // 2. When rounding first occurs in the expected product, it 809N/A // too rounds up, to 2^-MAX_EXPONENT. 809N/A // Halving expected after rounding happends to give the same 809N/A // result as the scalb operation. 809N/A // Tricky rounding tests: 809N/A // Scale down a large number into subnormal range such that if 809N/A // scalb is being implemented with multiple floating-point 809N/A // multiplies, the value would round twice if the multiplies 809N/A // were done in the wrong order. 809N/A for(
int i =
0; i <
129; i++) {
809N/A // Arguments x, where scalb(x,n) is x for any n. 809N/A // Test cases where scaling is always a no-op 809N/A // Test cases where result is 0.0 or infinity due to magnitude 809N/A // of the scaling factor 809N/A // Test cases that could be done with one floating-point 809N/A // Create 2^MAX_EXPONENT 809N/A // Scale-up subnormal values until they all overflow 809N/A // calculate right answer 809N/A // Scale down a large number until it underflows. By scaling 809N/A // down MAX_NORMALmm, the first subnormal result will be exact 809N/A // but the next one will round -- all those results can be 809N/A // checked by halving a separate value in the loop. Actually, 809N/A // we can keep halving and checking until the product is zero 809N/A // 1. If the scalb of MAX_VALUEmm is subnormal and *not* exact 809N/A // 2. When rounding first occurs in the expected product, it 809N/A // too rounds up, to 2^-MAX_EXPONENT. 809N/A // Halving expected after rounding happends to give the same 809N/A // result as the scalb operation. 809N/A // Tricky rounding tests: 809N/A // Scale down a large number into subnormal range such that if 809N/A // scalb is being implemented with multiple floating-point 809N/A // multiplies, the value would round twice if the multiplies 809N/A // were done in the wrong order. 809N/A /* ************************* ulp tests ******************************* */ 809N/A * Test Math.ulp and StrictMath.ulp with +d and -d. 809N/A // Normal exponent tests 809N/A // Generate some random bit patterns for the significand 809N/A for(
int j =
0; j <
10; j++) {
809N/A * Start with MIN_VALUE, left shift, test high value, low 809N/A * values, and random in between. 809N/A * Use nextAfter to calculate, high value of previous binade, 809N/A * loop count i will indicate how many random bits, if any are 809N/A // Test largest value in next smaller binade 809N/A if (i >=
3) {
// (i == 1) would test 0.0; 809N/A // (i == 2) would just retest MIN_VALUE 809N/A // create a bit mask with (i-1) 1's in the low order 809N/A // Normal exponent tests 809N/A // Generate some random bit patterns for the significand 809N/A for(
int j =
0; j <
10; j++) {
809N/A * Start with MIN_VALUE, left shift, test high value, low 809N/A * values, and random in between. 809N/A * Use nextAfter to calculate, high value of previous binade, 809N/A * loop count i will indicate how many random bits, if any are 809N/A // Test largest value in next smaller binade 809N/A if (i >=
3) {
// (i == 1) would test 0.0; 809N/A // (i == 2) would just retest MIN_VALUE 809N/A // create a bit mask with (i-1) 1's in the low order