809N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
809N/A *
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 *
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 *
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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
809N/A */
809N/A
809N/A/*
809N/A * @test
809N/A * @bug 4347132 4939441
809N/A * @summary Tests for {Math, StrictMath}.cbrt
809N/A * @author Joseph D. Darcy
809N/A */
809N/A
809N/Aimport sun.misc.FpUtils;
809N/Aimport sun.misc.DoubleConsts;
809N/A
809N/Apublic class CubeRootTests {
809N/A private CubeRootTests(){}
809N/A
809N/A static final double infinityD = Double.POSITIVE_INFINITY;
809N/A static final double NaNd = Double.NaN;
809N/A
809N/A // Initialize shared random number generator
809N/A static java.util.Random rand = new java.util.Random();
809N/A
809N/A static int testCubeRootCase(double input, double expected) {
809N/A int failures=0;
809N/A
809N/A double minus_input = -input;
809N/A double minus_expected = -expected;
809N/A
809N/A failures+=Tests.test("Math.cbrt(double)", input,
809N/A Math.cbrt(input), expected);
809N/A failures+=Tests.test("Math.cbrt(double)", minus_input,
809N/A Math.cbrt(minus_input), minus_expected);
809N/A failures+=Tests.test("StrictMath.cbrt(double)", input,
809N/A StrictMath.cbrt(input), expected);
809N/A failures+=Tests.test("StrictMath.cbrt(double)", minus_input,
809N/A StrictMath.cbrt(minus_input), minus_expected);
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A static int testCubeRoot() {
809N/A int failures = 0;
809N/A double [][] testCases = {
809N/A {NaNd, NaNd},
809N/A {Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
809N/A {Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
809N/A {Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
809N/A {Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
809N/A {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
809N/A {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
809N/A {Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
809N/A {Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
809N/A {Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
809N/A {Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
809N/A {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
809N/A {Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY},
809N/A {+0.0, +0.0},
809N/A {-0.0, -0.0},
809N/A {+1.0, +1.0},
809N/A {-1.0, -1.0},
809N/A {+8.0, +2.0},
809N/A {-8.0, -2.0}
809N/A };
809N/A
809N/A for(int i = 0; i < testCases.length; i++) {
809N/A failures += testCubeRootCase(testCases[i][0],
809N/A testCases[i][1]);
809N/A }
809N/A
809N/A // Test integer perfect cubes less than 2^53.
809N/A for(int i = 0; i <= 208063; i++) {
809N/A double d = i;
809N/A failures += testCubeRootCase(d*d*d, (double)i);
809N/A }
809N/A
809N/A // Test cbrt(2^(3n)) = 2^n.
809N/A for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) {
809N/A failures += testCubeRootCase(FpUtils.scalb(1.0, 3*i),
809N/A FpUtils.scalb(1.0, i) );
809N/A }
809N/A
809N/A // Test cbrt(2^(-3n)) = 2^-n.
809N/A for(int i = -1; i >= FpUtils.ilogb(Double.MIN_VALUE)/3; i--) {
809N/A failures += testCubeRootCase(FpUtils.scalb(1.0, 3*i),
809N/A FpUtils.scalb(1.0, i) );
809N/A }
809N/A
809N/A // Test random perfect cubes. Create double values with
809N/A // modest exponents but only have at most the 17 most
809N/A // significant bits in the significand set; 17*3 = 51, which
809N/A // is less than the number of bits in a double's significand.
809N/A long exponentBits1 =
809N/A Double.doubleToLongBits(FpUtils.scalb(1.0, 55)) &
809N/A DoubleConsts.EXP_BIT_MASK;
809N/A long exponentBits2=
809N/A Double.doubleToLongBits(FpUtils.scalb(1.0, -55)) &
809N/A DoubleConsts.EXP_BIT_MASK;
809N/A for(int i = 0; i < 100; i++) {
809N/A // Take 16 bits since the 17th bit is implicit in the
809N/A // exponent
809N/A double input1 =
809N/A Double.longBitsToDouble(exponentBits1 |
809N/A // Significand bits
809N/A ((long) (rand.nextInt() & 0xFFFF))<<
809N/A (DoubleConsts.SIGNIFICAND_WIDTH-1-16));
809N/A failures += testCubeRootCase(input1*input1*input1, input1);
809N/A
809N/A double input2 =
809N/A Double.longBitsToDouble(exponentBits2 |
809N/A // Significand bits
809N/A ((long) (rand.nextInt() & 0xFFFF))<<
809N/A (DoubleConsts.SIGNIFICAND_WIDTH-1-16));
809N/A failures += testCubeRootCase(input2*input2*input2, input2);
809N/A }
809N/A
809N/A // Directly test quality of implementation properties of cbrt
809N/A // for values that aren't perfect cubes. Verify returned
809N/A // result meets the 1 ulp test. That is, we want to verify
809N/A // that for positive x > 1,
809N/A // y = cbrt(x),
809N/A //
809N/A // if (err1=x - y^3 ) < 0, abs((y_pp^3 -x )) < err1
809N/A // if (err1=x - y^3 ) > 0, abs((y_mm^3 -x )) < err1
809N/A //
809N/A // where y_mm and y_pp are the next smaller and next larger
809N/A // floating-point value to y. In other words, if y^3 is too
809N/A // big, making y larger does not improve the result; likewise,
809N/A // if y^3 is too small, making y smaller does not improve the
809N/A // result.
809N/A //
809N/A // ...-----|--?--|--?--|-----... Where is the true result?
809N/A // y_mm y y_pp
809N/A //
809N/A // The returned value y should be one of the floating-point
809N/A // values braketing the true result. However, given y, a
809N/A // priori we don't know if the true result falls in [y_mm, y]
809N/A // or [y, y_pp]. The above test looks at the error in x-y^3
809N/A // to determine which region the true result is in; e.g. if
809N/A // y^3 is smaller than x, the true result should be in [y,
809N/A // y_pp]. Therefore, it would be an error for y_mm to be a
809N/A // closer approximation to x^(1/3). In this case, it is
809N/A // permissible, although not ideal, for y_pp^3 to be a closer
809N/A // approximation to x^(1/3) than y^3.
809N/A //
809N/A // We will use pow(y,3) to compute y^3. Although pow is not
809N/A // correctly rounded, StrictMath.pow should have at most 1 ulp
809N/A // error. For y > 1, pow(y_mm,3) and pow(y_pp,3) will differ
809N/A // from pow(y,3) by more than one ulp so the comparision of
809N/A // errors should still be valid.
809N/A
809N/A for(int i = 0; i < 1000; i++) {
809N/A double d = 1.0 + rand.nextDouble();
809N/A double err, err_adjacent;
809N/A
809N/A double y1 = Math.cbrt(d);
809N/A double y2 = StrictMath.cbrt(d);
809N/A
809N/A err = d - StrictMath.pow(y1, 3);
809N/A if (err != 0.0) {
809N/A if(FpUtils.isNaN(err)) {
809N/A failures++;
809N/A System.err.println("Encountered unexpected NaN value: d = " + d +
809N/A "\tcbrt(d) = " + y1);
809N/A } else {
809N/A if (err < 0.0) {
809N/A err_adjacent = StrictMath.pow(FpUtils.nextUp(y1), 3) - d;
809N/A }
809N/A else { // (err > 0.0)
809N/A err_adjacent = StrictMath.pow(FpUtils.nextAfter(y1,0.0), 3) - d;
809N/A }
809N/A
809N/A if (Math.abs(err) > Math.abs(err_adjacent)) {
809N/A failures++;
809N/A System.err.println("For Math.cbrt(" + d + "), returned result " +
809N/A y1 + "is not as good as adjacent value.");
809N/A }
809N/A }
809N/A }
809N/A
809N/A
809N/A err = d - StrictMath.pow(y2, 3);
809N/A if (err != 0.0) {
809N/A if(FpUtils.isNaN(err)) {
809N/A failures++;
809N/A System.err.println("Encountered unexpected NaN value: d = " + d +
809N/A "\tcbrt(d) = " + y2);
809N/A } else {
809N/A if (err < 0.0) {
809N/A err_adjacent = StrictMath.pow(FpUtils.nextUp(y2), 3) - d;
809N/A }
809N/A else { // (err > 0.0)
809N/A err_adjacent = StrictMath.pow(FpUtils.nextAfter(y2,0.0), 3) - d;
809N/A }
809N/A
809N/A if (Math.abs(err) > Math.abs(err_adjacent)) {
809N/A failures++;
809N/A System.err.println("For StrictMath.cbrt(" + d + "), returned result " +
809N/A y2 + "is not as good as adjacent value.");
809N/A }
809N/A }
809N/A }
809N/A
809N/A
809N/A }
809N/A
809N/A // Test monotonicity properites near perfect cubes; test two
809N/A // numbers before and two numbers after; i.e. for
809N/A //
809N/A // pcNeighbors[] =
809N/A // {nextDown(nextDown(pc)),
809N/A // nextDown(pc),
809N/A // pc,
809N/A // nextUp(pc),
809N/A // nextUp(nextUp(pc))}
809N/A //
809N/A // test that cbrt(pcNeighbors[i]) <= cbrt(pcNeighbors[i+1])
809N/A {
809N/A
809N/A double pcNeighbors[] = new double[5];
809N/A double pcNeighborsCbrt[] = new double[5];
809N/A double pcNeighborsStrictCbrt[] = new double[5];
809N/A
809N/A // Test near cbrt(2^(3n)) = 2^n.
809N/A for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) {
809N/A double pc = FpUtils.scalb(1.0, 3*i);
809N/A
809N/A pcNeighbors[2] = pc;
809N/A pcNeighbors[1] = FpUtils.nextDown(pc);
809N/A pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
809N/A pcNeighbors[3] = FpUtils.nextUp(pc);
809N/A pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
809N/A
809N/A for(int j = 0; j < pcNeighbors.length; j++) {
809N/A pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]);
809N/A pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);
809N/A }
809N/A
809N/A for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {
809N/A if(pcNeighborsCbrt[j] > pcNeighborsCbrt[j+1] ) {
809N/A failures++;
809N/A System.err.println("Monotonicity failure for Math.cbrt on " +
809N/A pcNeighbors[j] + " and " +
809N/A pcNeighbors[j+1] + "\n\treturned " +
809N/A pcNeighborsCbrt[j] + " and " +
809N/A pcNeighborsCbrt[j+1] );
809N/A }
809N/A
809N/A if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) {
809N/A failures++;
809N/A System.err.println("Monotonicity failure for StrictMath.cbrt on " +
809N/A pcNeighbors[j] + " and " +
809N/A pcNeighbors[j+1] + "\n\treturned " +
809N/A pcNeighborsStrictCbrt[j] + " and " +
809N/A pcNeighborsStrictCbrt[j+1] );
809N/A }
809N/A
809N/A
809N/A }
809N/A
809N/A }
809N/A
809N/A // Test near cbrt(2^(-3n)) = 2^-n.
809N/A for(int i = -1; i >= FpUtils.ilogb(Double.MIN_VALUE)/3; i--) {
809N/A double pc = FpUtils.scalb(1.0, 3*i);
809N/A
809N/A pcNeighbors[2] = pc;
809N/A pcNeighbors[1] = FpUtils.nextDown(pc);
809N/A pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]);
809N/A pcNeighbors[3] = FpUtils.nextUp(pc);
809N/A pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]);
809N/A
809N/A for(int j = 0; j < pcNeighbors.length; j++) {
809N/A pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]);
809N/A pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);
809N/A }
809N/A
809N/A for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {
809N/A if(pcNeighborsCbrt[j] > pcNeighborsCbrt[j+1] ) {
809N/A failures++;
809N/A System.err.println("Monotonicity failure for Math.cbrt on " +
809N/A pcNeighbors[j] + " and " +
809N/A pcNeighbors[j+1] + "\n\treturned " +
809N/A pcNeighborsCbrt[j] + " and " +
809N/A pcNeighborsCbrt[j+1] );
809N/A }
809N/A
809N/A if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) {
809N/A failures++;
809N/A System.err.println("Monotonicity failure for StrictMath.cbrt on " +
809N/A pcNeighbors[j] + " and " +
809N/A pcNeighbors[j+1] + "\n\treturned " +
809N/A pcNeighborsStrictCbrt[j] + " and " +
809N/A pcNeighborsStrictCbrt[j+1] );
809N/A }
809N/A
809N/A
809N/A }
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A public static void main(String argv[]) {
809N/A int failures = 0;
809N/A
809N/A failures += testCubeRoot();
809N/A
809N/A if (failures > 0) {
809N/A System.err.println("Testing cbrt incurred "
809N/A + failures + " failures.");
809N/A throw new RuntimeException();
809N/A }
809N/A }
809N/A
809N/A}