< prev index next >

## test/java/lang/Math/PowTests.java

 ``` 1 /* 2 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 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 4984407 5033578 27 * @summary Tests for {Math, StrictMath}.pow 28 * @author Joseph D. Darcy 29 */ 30 31 public class PowTests { 32 private PowTests(){} 33 34 static final double infinityD = Double.POSITIVE_INFINITY; 35 36 static int testPowCase(double input1, double input2, double expected) { 37 int failures = 0; 38 failures += Tests.test("StrictMath.pow(double, double)", input1, input2, 39 StrictMath.pow(input1, input2), expected); 40 failures += Tests.test("Math.pow(double, double)", input1, input2, 41 Math.pow(input1, input2), expected); 42 return failures; 43 } 44 45 46 static int testStrictPowCase(double input1, double input2, double expected) { `````` 71 }; 72 73 for (double[] testCase : testCases) { 74 failures+=testPowCase(testCase[0], testCase[1], testCase[2]); 75 } 76 77 return failures; 78 } 79 80 /* 81 * Test cross-product of different kinds of arguments. 82 */ 83 static int testCrossProduct() { 84 int failures = 0; 85 86 double testData[] = { 87 Double.NEGATIVE_INFINITY, 88 /* > -oo */ -Double.MAX_VALUE, 89 /**/ (double)Long.MIN_VALUE, 90 /**/ (double) -((1L<<53)+2L), 91 /**/ (double) -((1L<<53)), 92 /**/ (double) -((1L<<53)-1L), 93 /**/ -((double)Integer.MAX_VALUE + 4.0), 94 /**/ (double)Integer.MIN_VALUE - 1.0, 95 /**/ (double)Integer.MIN_VALUE, 96 /**/ (double)Integer.MIN_VALUE + 1.0, 97 /**/ -Math.PI, 98 /**/ -3.0, 99 /**/ -Math.E, 100 /**/ -2.0, 101 /**/ -1.0000000000000004, 102 /* < -1.0 */ -1.0000000000000002, // nextAfter(-1.0, -oo) 103 -1.0, 104 /* > -1.0 */ -0.9999999999999999, // nextAfter(-1.0, +oo) 105 /* > -1.0 */ -0.9999999999999998, 106 /**/ -0.5, 107 /**/ -1.0/3.0, 108 /* < 0.0 */ -Double.MIN_VALUE, 109 -0.0, 110 +0.0, 111 /* > 0.0 */ +Double.MIN_VALUE, 112 /**/ +1.0/3.0, 113 /**/ +0.5, 114 /**/ +0.9999999999999998, 115 /* < +1.0 */ +0.9999999999999999, // nextAfter(-1.0, +oo) 116 +1.0, 117 /* > 1.0 */ +1.0000000000000002, // nextAfter(+1.0, +oo) 118 /**/ +1.0000000000000004, 119 /**/ +2.0, 120 /**/ +Math.E, 121 /**/ +3.0, 122 /**/ +Math.PI, 123 /**/ -(double)Integer.MIN_VALUE - 1.0, 124 /**/ -(double)Integer.MIN_VALUE, 125 /**/ -(double)Integer.MIN_VALUE + 1.0, 126 /**/ (double)Integer.MAX_VALUE + 4.0, 127 /**/ (double) ((1L<<53)-1L), 128 /**/ (double) ((1L<<53)), 129 /**/ (double) ((1L<<53)+2L), 130 /**/ -(double)Long.MIN_VALUE, 131 /* < oo */ Double.MAX_VALUE, 132 Double.POSITIVE_INFINITY, 133 Double.NaN 134 }; 135 136 double NaN = Double.NaN; 137 for(double x: testData) { 138 for(double y: testData) { 139 boolean testPass = false; 140 double expected=NaN; 141 double actual; 142 143 // First, switch on y 144 if( Double.isNaN(y)) { 145 expected = NaN; 146 } else if (y == 0.0) { 147 expected = 1.0; 148 } else if (Double.isInfinite(y) ) { 149 if(y > 0) { // x ^ (+oo) `````` 240 switch( intClassify(y) ) { 241 case 0: 242 return Math.pow(Math.abs(x), y); 243 // break; 244 245 case 1: 246 return -Math.pow(Math.abs(x), y); 247 // break; 248 249 case -1: 250 return Double.NaN; 251 // break; 252 253 default: 254 throw new AssertionError("Bad classification."); 255 // break; 256 } 257 } 258 259 static boolean isFinite(double a) { 260 return (0.0*a == 0); 261 } 262 263 /** 264 * Return classification of argument: -1 for non-integers, 0 for 265 * even integers, 1 for odd integers. 266 */ 267 static int intClassify(double a) { 268 if(!isFinite(a) || // NaNs and infinities 269 (a != Math.floor(a) )) { // only integers are fixed-points of floor 270 return -1; 271 } 272 else { 273 // Determine if argument is an odd or even integer. 274 275 a = StrictMath.abs(a); // absolute value doesn't affect odd/even 276 277 if(a+1.0 == a) { // a > maximum odd floating-point integer 278 return 0; // Large integers are all even 279 } 280 else { // Convert double -> long and look at low-order bit ``` ``` 1 /* 2 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 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 4984407 5033578 8134795 27 * @summary Tests for {Math, StrictMath}.pow 28 * @author Joseph D. Darcy 29 */ 30 31 public class PowTests { 32 private PowTests(){} 33 34 static final double infinityD = Double.POSITIVE_INFINITY; 35 36 static int testPowCase(double input1, double input2, double expected) { 37 int failures = 0; 38 failures += Tests.test("StrictMath.pow(double, double)", input1, input2, 39 StrictMath.pow(input1, input2), expected); 40 failures += Tests.test("Math.pow(double, double)", input1, input2, 41 Math.pow(input1, input2), expected); 42 return failures; 43 } 44 45 46 static int testStrictPowCase(double input1, double input2, double expected) { `````` 71 }; 72 73 for (double[] testCase : testCases) { 74 failures+=testPowCase(testCase[0], testCase[1], testCase[2]); 75 } 76 77 return failures; 78 } 79 80 /* 81 * Test cross-product of different kinds of arguments. 82 */ 83 static int testCrossProduct() { 84 int failures = 0; 85 86 double testData[] = { 87 Double.NEGATIVE_INFINITY, 88 /* > -oo */ -Double.MAX_VALUE, 89 /**/ (double)Long.MIN_VALUE, 90 /**/ (double) -((1L<<53)+2L), 91 -0x1.0p65, 92 -0x1.0000000000001p64, 93 -0x1.0p64, 94 /**/ (double) -((1L<<53)), 95 /**/ (double) -((1L<<53)-1L), 96 /**/ -((double)Integer.MAX_VALUE + 4.0), 97 /**/ (double)Integer.MIN_VALUE - 1.0, 98 /**/ (double)Integer.MIN_VALUE, 99 /**/ (double)Integer.MIN_VALUE + 1.0, 100 -0x1.0p31 + 2.0, 101 -0x1.0p31 + 1.0, 102 -0x1.0000000000001p31, 103 -0x1.0p31, 104 /**/ -Math.PI, 105 /**/ -3.0, 106 /**/ -Math.E, 107 /**/ -2.0, 108 /**/ -1.0000000000000004, 109 /* < -1.0 */ -1.0000000000000002, // nextAfter(-1.0, -oo) 110 -1.0, 111 /* > -1.0 */ -0.9999999999999999, // nextAfter(-1.0, +oo) 112 /* > -1.0 */ -0.9999999999999998, 113 -0x1.fffffp-1, 114 -0x1.ffffeffffffffp-1, 115 /**/ -0.5, 116 /**/ -1.0/3.0, 117 /* < 0.0 */ -Double.MIN_VALUE, 118 -0.0, 119 +0.0, 120 /* > 0.0 */ +Double.MIN_VALUE, 121 /**/ +1.0/3.0, 122 /**/ +0.5, 123 +0x1.ffffeffffffffp-1, 124 +0x1.fffffp-1, 125 /**/ +0.9999999999999998, 126 /* < +1.0 */ +0.9999999999999999, // nextAfter(-1.0, +oo) 127 +1.0, 128 /* > 1.0 */ +1.0000000000000002, // nextAfter(+1.0, +oo) 129 /**/ +1.0000000000000004, 130 /**/ +2.0, 131 /**/ +Math.E, 132 /**/ +3.0, 133 /**/ +Math.PI, 134 0x1.0p31, 135 0x1.0000000000001p31, 136 0x1.0p31 + 1.0, 137 0x1.0p31 + 2.0, 138 /**/ -(double)Integer.MIN_VALUE - 1.0, 139 /**/ -(double)Integer.MIN_VALUE, 140 /**/ -(double)Integer.MIN_VALUE + 1.0, 141 /**/ (double)Integer.MAX_VALUE + 4.0, 142 /**/ (double) ((1L<<53)-1L), 143 /**/ (double) ((1L<<53)), 144 /**/ (double) ((1L<<53)+2L), 145 0x1.0p64, 146 0x1.0000000000001p64, 147 0x1.0p65, 148 /**/ -(double)Long.MIN_VALUE, 149 /* < oo */ Double.MAX_VALUE, 150 Double.POSITIVE_INFINITY, 151 Double.NaN 152 }; 153 154 double NaN = Double.NaN; 155 for(double x: testData) { 156 for(double y: testData) { 157 boolean testPass = false; 158 double expected=NaN; 159 double actual; 160 161 // First, switch on y 162 if( Double.isNaN(y)) { 163 expected = NaN; 164 } else if (y == 0.0) { 165 expected = 1.0; 166 } else if (Double.isInfinite(y) ) { 167 if(y > 0) { // x ^ (+oo) `````` 258 switch( intClassify(y) ) { 259 case 0: 260 return Math.pow(Math.abs(x), y); 261 // break; 262 263 case 1: 264 return -Math.pow(Math.abs(x), y); 265 // break; 266 267 case -1: 268 return Double.NaN; 269 // break; 270 271 default: 272 throw new AssertionError("Bad classification."); 273 // break; 274 } 275 } 276 277 static boolean isFinite(double a) { 278 return (0.0 * a == 0); 279 } 280 281 /** 282 * Return classification of argument: -1 for non-integers, 0 for 283 * even integers, 1 for odd integers. 284 */ 285 static int intClassify(double a) { 286 if(!isFinite(a) || // NaNs and infinities 287 (a != Math.floor(a) )) { // only integers are fixed-points of floor 288 return -1; 289 } 290 else { 291 // Determine if argument is an odd or even integer. 292 293 a = StrictMath.abs(a); // absolute value doesn't affect odd/even 294 295 if(a+1.0 == a) { // a > maximum odd floating-point integer 296 return 0; // Large integers are all even 297 } 298 else { // Convert double -> long and look at low-order bit ```
< prev index next >