Path: blob/master/test/jdk/java/lang/Math/PowTests.java
66644 views
/*1* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4984407 5033578 813479526* @summary Tests for {Math, StrictMath}.pow27* @author Joseph D. Darcy28*/2930public class PowTests {31private PowTests(){}3233static final double infinityD = Double.POSITIVE_INFINITY;3435static int testPowCase(double input1, double input2, double expected) {36int failures = 0;37failures += Tests.test("StrictMath.pow(double, double)", input1, input2,38StrictMath.pow(input1, input2), expected);39failures += Tests.test("Math.pow(double, double)", input1, input2,40Math.pow(input1, input2), expected);41return failures;42}434445static int testStrictPowCase(double input1, double input2, double expected) {46int failures = 0;47failures += Tests.test("StrictMath.pow(double, double)", input1, input2,48StrictMath.pow(input1, input2), expected);49return failures;50}5152static int testNonstrictPowCase(double input1, double input2, double expected) {53int failures = 0;54failures += Tests.test("Math.pow(double, double)", input1, input2,55Math.pow(input1, input2), expected);56return failures;57}5859static int testStrictVsNonstrictPowCase(double input1, double input2) {60double smResult = StrictMath.pow(input1, input2);61double mResult = Math.pow(input1, input2);62return Tests.testUlpDiff(63"StrictMath.pow(double, double) vs Math.pow(double, double)",64input1, input2, mResult, smResult, 2.065);66}6768/*69* Test for bad negation implementation.70*/71static int testPow() {72int failures = 0;7374double [][] testCases = {75{-0.0, 3.0, -0.0},76{-0.0, 4.0, 0.0},77{-infinityD, -3.0, -0.0},78{-infinityD, -4.0, 0.0},79};8081for (double[] testCase : testCases) {82failures+=testPowCase(testCase[0], testCase[1], testCase[2]);83}8485return failures;86}8788/*89* Test cross-product of different kinds of arguments.90*/91static int testCrossProduct() {92int failures = 0;9394double testData[] = {95Double.NEGATIVE_INFINITY,96/* > -oo */ -Double.MAX_VALUE,97/**/ (double)Long.MIN_VALUE,98/**/ (double) -((1L<<53)+2L),99-0x1.0p65,100-0x1.0000000000001p64,101-0x1.0p64,102/**/ (double) -((1L<<53)),103/**/ (double) -((1L<<53)-1L),104/**/ -((double)Integer.MAX_VALUE + 4.0),105/**/ (double)Integer.MIN_VALUE - 1.0,106/**/ (double)Integer.MIN_VALUE,107/**/ (double)Integer.MIN_VALUE + 1.0,108-0x1.0p31 + 2.0,109-0x1.0p31 + 1.0,110-0x1.0000000000001p31,111-0x1.0p31,112/**/ -Math.PI,113/**/ -3.0,114/**/ -Math.E,115/**/ -2.0,116/**/ -1.0000000000000004,117/* < -1.0 */ -1.0000000000000002, // nextAfter(-1.0, -oo)118-1.0,119/* > -1.0 */ -0.9999999999999999, // nextAfter(-1.0, +oo)120/* > -1.0 */ -0.9999999999999998,121-0x1.fffffp-1,122-0x1.ffffeffffffffp-1,123/**/ -0.5,124/**/ -1.0/3.0,125/* < 0.0 */ -Double.MIN_VALUE,126-0.0,127+0.0,128/* > 0.0 */ +Double.MIN_VALUE,129/**/ +1.0/3.0,130/**/ +0.5,131+0x1.ffffeffffffffp-1,132+0x1.fffffp-1,133/**/ +0.9999999999999998,134/* < +1.0 */ +0.9999999999999999, // nextAfter(-1.0, +oo)135+1.0,136/* > 1.0 */ +1.0000000000000002, // nextAfter(+1.0, +oo)137/**/ +1.0000000000000004,138/**/ +2.0,139/**/ +Math.E,140/**/ +3.0,141/**/ +Math.PI,1420x1.0p31,1430x1.0000000000001p31,1440x1.0p31 + 1.0,1450x1.0p31 + 2.0,146/**/ -(double)Integer.MIN_VALUE - 1.0,147/**/ -(double)Integer.MIN_VALUE,148/**/ -(double)Integer.MIN_VALUE + 1.0,149/**/ (double)Integer.MAX_VALUE + 4.0,150/**/ (double) ((1L<<53)-1L),151/**/ (double) ((1L<<53)),152/**/ (double) ((1L<<53)+2L),1530x1.0p64,1540x1.0000000000001p64,1550x1.0p65,156/**/ -(double)Long.MIN_VALUE,157/* < oo */ Double.MAX_VALUE,158Double.POSITIVE_INFINITY,159Double.NaN160};161162double NaN = Double.NaN;163for(double x: testData) {164for(double y: testData) {165boolean testPass = false;166double expected=NaN;167double actual;168169// First, switch on y170if( Double.isNaN(y)) {171expected = NaN;172} else if (y == 0.0) {173expected = 1.0;174} else if (Double.isInfinite(y) ) {175if(y > 0) { // x ^ (+oo)176if (Math.abs(x) > 1.0) {177expected = Double.POSITIVE_INFINITY;178} else if (Math.abs(x) == 1.0) {179expected = NaN;180} else if (Math.abs(x) < 1.0) {181expected = +0.0;182} else { // x is NaN183assert Double.isNaN(x);184expected = NaN;185}186} else { // x ^ (-oo)187if (Math.abs(x) > 1.0) {188expected = +0.0;189} else if (Math.abs(x) == 1.0) {190expected = NaN;191} else if (Math.abs(x) < 1.0) {192expected = Double.POSITIVE_INFINITY;193} else { // x is NaN194assert Double.isNaN(x);195expected = NaN;196}197} /* end Double.isInfinite(y) */198} else if (y == 1.0) {199expected = x;200} else if (Double.isNaN(x)) { // Now start switching on x201assert y != 0.0;202expected = NaN;203} else if (x == Double.NEGATIVE_INFINITY) {204expected = (y < 0.0) ? f2(y) :f1(y);205} else if (x == Double.POSITIVE_INFINITY) {206expected = (y < 0.0) ? +0.0 : Double.POSITIVE_INFINITY;207} else if (equivalent(x, +0.0)) {208assert y != 0.0;209expected = (y < 0.0) ? Double.POSITIVE_INFINITY: +0.0;210} else if (equivalent(x, -0.0)) {211assert y != 0.0;212expected = (y < 0.0) ? f1(y): f2(y);213} else if( x < 0.0) {214assert y != 0.0;215failures += testStrictPowCase(x, y, f3(x, y));216failures += testNonstrictPowCase(x, y, f3ns(x, y));217failures += testStrictVsNonstrictPowCase(x, y);218continue;219} else {220failures += testStrictVsNonstrictPowCase(x, y);221// go to next iteration222expected = NaN;223continue;224}225226failures += testPowCase(x, y, expected);227} // y228} // x229return failures;230}231232static boolean equivalent(double a, double b) {233return Double.compare(a, b) == 0;234}235236static double f1(double y) {237return (intClassify(y) == 1)?238Double.NEGATIVE_INFINITY:239Double.POSITIVE_INFINITY;240}241242243static double f2(double y) {244return (intClassify(y) == 1)?-0.0:0.0;245}246247static double f3(double x, double y) {248switch( intClassify(y) ) {249case 0:250return StrictMath.pow(Math.abs(x), y);251// break;252253case 1:254return -StrictMath.pow(Math.abs(x), y);255// break;256257case -1:258return Double.NaN;259// break;260261default:262throw new AssertionError("Bad classification.");263// break;264}265}266267static double f3ns(double x, double y) {268switch( intClassify(y) ) {269case 0:270return Math.pow(Math.abs(x), y);271// break;272273case 1:274return -Math.pow(Math.abs(x), y);275// break;276277case -1:278return Double.NaN;279// break;280281default:282throw new AssertionError("Bad classification.");283// break;284}285}286287static boolean isFinite(double a) {288return (0.0 * a == 0);289}290291/**292* Return classification of argument: -1 for non-integers, 0 for293* even integers, 1 for odd integers.294*/295static int intClassify(double a) {296if(!isFinite(a) || // NaNs and infinities297(a != Math.floor(a) )) { // only integers are fixed-points of floor298return -1;299}300else {301// Determine if argument is an odd or even integer.302303a = StrictMath.abs(a); // absolute value doesn't affect odd/even304305if(a+1.0 == a) { // a > maximum odd floating-point integer306return 0; // Large integers are all even307}308else { // Convert double -> long and look at low-order bit309long ell = (long) a;310return ((ell & 0x1L) == (long)1)?1:0;311}312}313}314315public static void main(String [] argv) {316int failures = 0;317318failures += testPow();319failures += testCrossProduct();320321if (failures > 0) {322System.err.println("Testing pow incurred "323+ failures + " failures.");324throw new RuntimeException();325}326}327}328329330