Path: blob/master/test/jdk/java/lang/Math/CubeRootTests.java
41149 views
/*1* Copyright (c) 2003, 2017, 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* @library /test/lib26* @build jdk.test.lib.RandomFactory27* @run main CubeRootTests28* @bug 4347132 4939441 807867229* @summary Tests for {Math, StrictMath}.cbrt (use -Dseed=X to set PRNG seed)30* @author Joseph D. Darcy31* @key randomness32*/3334import jdk.test.lib.RandomFactory;3536public class CubeRootTests {37private CubeRootTests(){}3839static final double infinityD = Double.POSITIVE_INFINITY;40static final double NaNd = Double.NaN;4142// Initialize shared random number generator43static java.util.Random rand = RandomFactory.getRandom();4445static int testCubeRootCase(double input, double expected) {46int failures=0;4748double minus_input = -input;49double minus_expected = -expected;5051failures+=Tests.test("Math.cbrt(double)", input,52Math.cbrt(input), expected);53failures+=Tests.test("Math.cbrt(double)", minus_input,54Math.cbrt(minus_input), minus_expected);55failures+=Tests.test("StrictMath.cbrt(double)", input,56StrictMath.cbrt(input), expected);57failures+=Tests.test("StrictMath.cbrt(double)", minus_input,58StrictMath.cbrt(minus_input), minus_expected);5960return failures;61}6263static int testCubeRoot() {64int failures = 0;65double [][] testCases = {66{NaNd, NaNd},67{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},68{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},69{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},70{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},71{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},72{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},73{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},74{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},75{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},76{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},77{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},78{Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY},79{+0.0, +0.0},80{-0.0, -0.0},81{+1.0, +1.0},82{-1.0, -1.0},83{+8.0, +2.0},84{-8.0, -2.0}85};8687for(int i = 0; i < testCases.length; i++) {88failures += testCubeRootCase(testCases[i][0],89testCases[i][1]);90}9192// Test integer perfect cubes less than 2^53.93for(int i = 0; i <= 208063; i++) {94double d = i;95failures += testCubeRootCase(d*d*d, (double)i);96}9798// Test cbrt(2^(3n)) = 2^n.99for(int i = 18; i <= Double.MAX_EXPONENT/3; i++) {100failures += testCubeRootCase(Math.scalb(1.0, 3*i),101Math.scalb(1.0, i) );102}103104// Test cbrt(2^(-3n)) = 2^-n.105for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) {106failures += testCubeRootCase(Math.scalb(1.0, 3*i),107Math.scalb(1.0, i) );108}109110// Test random perfect cubes. Create double values with111// modest exponents but only have at most the 17 most112// significant bits in the significand set; 17*3 = 51, which113// is less than the number of bits in a double's significand.114long exponentBits1 =115Double.doubleToLongBits(Math.scalb(1.0, 55)) &116DoubleConsts.EXP_BIT_MASK;117long exponentBits2=118Double.doubleToLongBits(Math.scalb(1.0, -55)) &119DoubleConsts.EXP_BIT_MASK;120for(int i = 0; i < 100; i++) {121// Take 16 bits since the 17th bit is implicit in the122// exponent123double input1 =124Double.longBitsToDouble(exponentBits1 |125// Significand bits126((long) (rand.nextInt() & 0xFFFF))<<127(DoubleConsts.SIGNIFICAND_WIDTH-1-16));128failures += testCubeRootCase(input1*input1*input1, input1);129130double input2 =131Double.longBitsToDouble(exponentBits2 |132// Significand bits133((long) (rand.nextInt() & 0xFFFF))<<134(DoubleConsts.SIGNIFICAND_WIDTH-1-16));135failures += testCubeRootCase(input2*input2*input2, input2);136}137138// Directly test quality of implementation properties of cbrt139// for values that aren't perfect cubes. Verify returned140// result meets the 1 ulp test. That is, we want to verify141// that for positive x > 1,142// y = cbrt(x),143//144// if (err1=x - y^3 ) < 0, abs((y_pp^3 -x )) < err1145// if (err1=x - y^3 ) > 0, abs((y_mm^3 -x )) < err1146//147// where y_mm and y_pp are the next smaller and next larger148// floating-point value to y. In other words, if y^3 is too149// big, making y larger does not improve the result; likewise,150// if y^3 is too small, making y smaller does not improve the151// result.152//153// ...-----|--?--|--?--|-----... Where is the true result?154// y_mm y y_pp155//156// The returned value y should be one of the floating-point157// values braketing the true result. However, given y, a158// priori we don't know if the true result falls in [y_mm, y]159// or [y, y_pp]. The above test looks at the error in x-y^3160// to determine which region the true result is in; e.g. if161// y^3 is smaller than x, the true result should be in [y,162// y_pp]. Therefore, it would be an error for y_mm to be a163// closer approximation to x^(1/3). In this case, it is164// permissible, although not ideal, for y_pp^3 to be a closer165// approximation to x^(1/3) than y^3.166//167// We will use pow(y,3) to compute y^3. Although pow is not168// correctly rounded, StrictMath.pow should have at most 1 ulp169// error. For y > 1, pow(y_mm,3) and pow(y_pp,3) will differ170// from pow(y,3) by more than one ulp so the comparision of171// errors should still be valid.172173for(int i = 0; i < 1000; i++) {174double d = 1.0 + rand.nextDouble();175double err, err_adjacent;176177double y1 = Math.cbrt(d);178double y2 = StrictMath.cbrt(d);179180err = d - StrictMath.pow(y1, 3);181if (err != 0.0) {182if(Double.isNaN(err)) {183failures++;184System.err.println("Encountered unexpected NaN value: d = " + d +185"\tcbrt(d) = " + y1);186} else {187if (err < 0.0) {188err_adjacent = StrictMath.pow(Math.nextUp(y1), 3) - d;189}190else { // (err > 0.0)191err_adjacent = StrictMath.pow(Math.nextAfter(y1,0.0), 3) - d;192}193194if (Math.abs(err) > Math.abs(err_adjacent)) {195failures++;196System.err.println("For Math.cbrt(" + d + "), returned result " +197y1 + "is not as good as adjacent value.");198}199}200}201202203err = d - StrictMath.pow(y2, 3);204if (err != 0.0) {205if(Double.isNaN(err)) {206failures++;207System.err.println("Encountered unexpected NaN value: d = " + d +208"\tcbrt(d) = " + y2);209} else {210if (err < 0.0) {211err_adjacent = StrictMath.pow(Math.nextUp(y2), 3) - d;212}213else { // (err > 0.0)214err_adjacent = StrictMath.pow(Math.nextAfter(y2,0.0), 3) - d;215}216217if (Math.abs(err) > Math.abs(err_adjacent)) {218failures++;219System.err.println("For StrictMath.cbrt(" + d + "), returned result " +220y2 + "is not as good as adjacent value.");221}222}223}224225226}227228// Test monotonicity properites near perfect cubes; test two229// numbers before and two numbers after; i.e. for230//231// pcNeighbors[] =232// {nextDown(nextDown(pc)),233// nextDown(pc),234// pc,235// nextUp(pc),236// nextUp(nextUp(pc))}237//238// test that cbrt(pcNeighbors[i]) <= cbrt(pcNeighbors[i+1])239{240241double pcNeighbors[] = new double[5];242double pcNeighborsCbrt[] = new double[5];243double pcNeighborsStrictCbrt[] = new double[5];244245// Test near cbrt(2^(3n)) = 2^n.246for(int i = 18; i <= Double.MAX_EXPONENT/3; i++) {247double pc = Math.scalb(1.0, 3*i);248249pcNeighbors[2] = pc;250pcNeighbors[1] = Math.nextDown(pc);251pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);252pcNeighbors[3] = Math.nextUp(pc);253pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);254255for(int j = 0; j < pcNeighbors.length; j++) {256pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]);257pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);258}259260for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {261if(pcNeighborsCbrt[j] > pcNeighborsCbrt[j+1] ) {262failures++;263System.err.println("Monotonicity failure for Math.cbrt on " +264pcNeighbors[j] + " and " +265pcNeighbors[j+1] + "\n\treturned " +266pcNeighborsCbrt[j] + " and " +267pcNeighborsCbrt[j+1] );268}269270if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) {271failures++;272System.err.println("Monotonicity failure for StrictMath.cbrt on " +273pcNeighbors[j] + " and " +274pcNeighbors[j+1] + "\n\treturned " +275pcNeighborsStrictCbrt[j] + " and " +276pcNeighborsStrictCbrt[j+1] );277}278279280}281282}283284// Test near cbrt(2^(-3n)) = 2^-n.285for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) {286double pc = Math.scalb(1.0, 3*i);287288pcNeighbors[2] = pc;289pcNeighbors[1] = Math.nextDown(pc);290pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);291pcNeighbors[3] = Math.nextUp(pc);292pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);293294for(int j = 0; j < pcNeighbors.length; j++) {295pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]);296pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);297}298299for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {300if(pcNeighborsCbrt[j] > pcNeighborsCbrt[j+1] ) {301failures++;302System.err.println("Monotonicity failure for Math.cbrt on " +303pcNeighbors[j] + " and " +304pcNeighbors[j+1] + "\n\treturned " +305pcNeighborsCbrt[j] + " and " +306pcNeighborsCbrt[j+1] );307}308309if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) {310failures++;311System.err.println("Monotonicity failure for StrictMath.cbrt on " +312pcNeighbors[j] + " and " +313pcNeighbors[j+1] + "\n\treturned " +314pcNeighborsStrictCbrt[j] + " and " +315pcNeighborsStrictCbrt[j+1] );316}317318319}320}321}322323return failures;324}325326public static void main(String argv[]) {327int failures = 0;328329failures += testCubeRoot();330331if (failures > 0) {332System.err.println("Testing cbrt incurred "333+ failures + " failures.");334throw new RuntimeException();335}336}337338}339340341