Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/CubeRootTests.java
38812 views
/*1* Copyright (c) 2003, 2012, 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 4347132 493944126* @summary Tests for {Math, StrictMath}.cbrt27* @author Joseph D. Darcy28* @key randomness29*/3031import sun.misc.DoubleConsts;3233public class CubeRootTests {34private CubeRootTests(){}3536static final double infinityD = Double.POSITIVE_INFINITY;37static final double NaNd = Double.NaN;3839// Initialize shared random number generator40static java.util.Random rand = new java.util.Random();4142static int testCubeRootCase(double input, double expected) {43int failures=0;4445double minus_input = -input;46double minus_expected = -expected;4748failures+=Tests.test("Math.cbrt(double)", input,49Math.cbrt(input), expected);50failures+=Tests.test("Math.cbrt(double)", minus_input,51Math.cbrt(minus_input), minus_expected);52failures+=Tests.test("StrictMath.cbrt(double)", input,53StrictMath.cbrt(input), expected);54failures+=Tests.test("StrictMath.cbrt(double)", minus_input,55StrictMath.cbrt(minus_input), minus_expected);5657return failures;58}5960static int testCubeRoot() {61int failures = 0;62double [][] testCases = {63{NaNd, NaNd},64{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},65{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},66{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},67{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},68{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},69{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},70{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},71{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},72{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},73{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},74{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},75{Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY},76{+0.0, +0.0},77{-0.0, -0.0},78{+1.0, +1.0},79{-1.0, -1.0},80{+8.0, +2.0},81{-8.0, -2.0}82};8384for(int i = 0; i < testCases.length; i++) {85failures += testCubeRootCase(testCases[i][0],86testCases[i][1]);87}8889// Test integer perfect cubes less than 2^53.90for(int i = 0; i <= 208063; i++) {91double d = i;92failures += testCubeRootCase(d*d*d, (double)i);93}9495// Test cbrt(2^(3n)) = 2^n.96for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) {97failures += testCubeRootCase(Math.scalb(1.0, 3*i),98Math.scalb(1.0, i) );99}100101// Test cbrt(2^(-3n)) = 2^-n.102for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) {103failures += testCubeRootCase(Math.scalb(1.0, 3*i),104Math.scalb(1.0, i) );105}106107// Test random perfect cubes. Create double values with108// modest exponents but only have at most the 17 most109// significant bits in the significand set; 17*3 = 51, which110// is less than the number of bits in a double's significand.111long exponentBits1 =112Double.doubleToLongBits(Math.scalb(1.0, 55)) &113DoubleConsts.EXP_BIT_MASK;114long exponentBits2=115Double.doubleToLongBits(Math.scalb(1.0, -55)) &116DoubleConsts.EXP_BIT_MASK;117for(int i = 0; i < 100; i++) {118// Take 16 bits since the 17th bit is implicit in the119// exponent120double input1 =121Double.longBitsToDouble(exponentBits1 |122// Significand bits123((long) (rand.nextInt() & 0xFFFF))<<124(DoubleConsts.SIGNIFICAND_WIDTH-1-16));125failures += testCubeRootCase(input1*input1*input1, input1);126127double input2 =128Double.longBitsToDouble(exponentBits2 |129// Significand bits130((long) (rand.nextInt() & 0xFFFF))<<131(DoubleConsts.SIGNIFICAND_WIDTH-1-16));132failures += testCubeRootCase(input2*input2*input2, input2);133}134135// Directly test quality of implementation properties of cbrt136// for values that aren't perfect cubes. Verify returned137// result meets the 1 ulp test. That is, we want to verify138// that for positive x > 1,139// y = cbrt(x),140//141// if (err1=x - y^3 ) < 0, abs((y_pp^3 -x )) < err1142// if (err1=x - y^3 ) > 0, abs((y_mm^3 -x )) < err1143//144// where y_mm and y_pp are the next smaller and next larger145// floating-point value to y. In other words, if y^3 is too146// big, making y larger does not improve the result; likewise,147// if y^3 is too small, making y smaller does not improve the148// result.149//150// ...-----|--?--|--?--|-----... Where is the true result?151// y_mm y y_pp152//153// The returned value y should be one of the floating-point154// values braketing the true result. However, given y, a155// priori we don't know if the true result falls in [y_mm, y]156// or [y, y_pp]. The above test looks at the error in x-y^3157// to determine which region the true result is in; e.g. if158// y^3 is smaller than x, the true result should be in [y,159// y_pp]. Therefore, it would be an error for y_mm to be a160// closer approximation to x^(1/3). In this case, it is161// permissible, although not ideal, for y_pp^3 to be a closer162// approximation to x^(1/3) than y^3.163//164// We will use pow(y,3) to compute y^3. Although pow is not165// correctly rounded, StrictMath.pow should have at most 1 ulp166// error. For y > 1, pow(y_mm,3) and pow(y_pp,3) will differ167// from pow(y,3) by more than one ulp so the comparision of168// errors should still be valid.169170for(int i = 0; i < 1000; i++) {171double d = 1.0 + rand.nextDouble();172double err, err_adjacent;173174double y1 = Math.cbrt(d);175double y2 = StrictMath.cbrt(d);176177err = d - StrictMath.pow(y1, 3);178if (err != 0.0) {179if(Double.isNaN(err)) {180failures++;181System.err.println("Encountered unexpected NaN value: d = " + d +182"\tcbrt(d) = " + y1);183} else {184if (err < 0.0) {185err_adjacent = StrictMath.pow(Math.nextUp(y1), 3) - d;186}187else { // (err > 0.0)188err_adjacent = StrictMath.pow(Math.nextAfter(y1,0.0), 3) - d;189}190191if (Math.abs(err) > Math.abs(err_adjacent)) {192failures++;193System.err.println("For Math.cbrt(" + d + "), returned result " +194y1 + "is not as good as adjacent value.");195}196}197}198199200err = d - StrictMath.pow(y2, 3);201if (err != 0.0) {202if(Double.isNaN(err)) {203failures++;204System.err.println("Encountered unexpected NaN value: d = " + d +205"\tcbrt(d) = " + y2);206} else {207if (err < 0.0) {208err_adjacent = StrictMath.pow(Math.nextUp(y2), 3) - d;209}210else { // (err > 0.0)211err_adjacent = StrictMath.pow(Math.nextAfter(y2,0.0), 3) - d;212}213214if (Math.abs(err) > Math.abs(err_adjacent)) {215failures++;216System.err.println("For StrictMath.cbrt(" + d + "), returned result " +217y2 + "is not as good as adjacent value.");218}219}220}221222223}224225// Test monotonicity properites near perfect cubes; test two226// numbers before and two numbers after; i.e. for227//228// pcNeighbors[] =229// {nextDown(nextDown(pc)),230// nextDown(pc),231// pc,232// nextUp(pc),233// nextUp(nextUp(pc))}234//235// test that cbrt(pcNeighbors[i]) <= cbrt(pcNeighbors[i+1])236{237238double pcNeighbors[] = new double[5];239double pcNeighborsCbrt[] = new double[5];240double pcNeighborsStrictCbrt[] = new double[5];241242// Test near cbrt(2^(3n)) = 2^n.243for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) {244double pc = Math.scalb(1.0, 3*i);245246pcNeighbors[2] = pc;247pcNeighbors[1] = Math.nextDown(pc);248pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);249pcNeighbors[3] = Math.nextUp(pc);250pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);251252for(int j = 0; j < pcNeighbors.length; j++) {253pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]);254pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);255}256257for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {258if(pcNeighborsCbrt[j] > pcNeighborsCbrt[j+1] ) {259failures++;260System.err.println("Monotonicity failure for Math.cbrt on " +261pcNeighbors[j] + " and " +262pcNeighbors[j+1] + "\n\treturned " +263pcNeighborsCbrt[j] + " and " +264pcNeighborsCbrt[j+1] );265}266267if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) {268failures++;269System.err.println("Monotonicity failure for StrictMath.cbrt on " +270pcNeighbors[j] + " and " +271pcNeighbors[j+1] + "\n\treturned " +272pcNeighborsStrictCbrt[j] + " and " +273pcNeighborsStrictCbrt[j+1] );274}275276277}278279}280281// Test near cbrt(2^(-3n)) = 2^-n.282for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) {283double pc = Math.scalb(1.0, 3*i);284285pcNeighbors[2] = pc;286pcNeighbors[1] = Math.nextDown(pc);287pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);288pcNeighbors[3] = Math.nextUp(pc);289pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);290291for(int j = 0; j < pcNeighbors.length; j++) {292pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]);293pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);294}295296for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {297if(pcNeighborsCbrt[j] > pcNeighborsCbrt[j+1] ) {298failures++;299System.err.println("Monotonicity failure for Math.cbrt on " +300pcNeighbors[j] + " and " +301pcNeighbors[j+1] + "\n\treturned " +302pcNeighborsCbrt[j] + " and " +303pcNeighborsCbrt[j+1] );304}305306if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) {307failures++;308System.err.println("Monotonicity failure for StrictMath.cbrt on " +309pcNeighbors[j] + " and " +310pcNeighbors[j+1] + "\n\treturned " +311pcNeighborsStrictCbrt[j] + " and " +312pcNeighborsStrictCbrt[j+1] );313}314315316}317}318}319320return failures;321}322323public static void main(String argv[]) {324int failures = 0;325326failures += testCubeRoot();327328if (failures > 0) {329System.err.println("Testing cbrt incurred "330+ failures + " failures.");331throw new RuntimeException();332}333}334335}336337338