1 /*
   2  * Copyright (c) 2003, 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  * @library /lib/testlibrary/
  27  * @build jdk.testlibrary.*
  28  * @run main CubeRootTests
  29  * @bug 4347132 4939441 8078672
  30  * @summary Tests for {Math, StrictMath}.cbrt (use -Dseed=X to set PRNG seed)
  31  * @author Joseph D. Darcy
  32  * @key randomness
  33  */
  34 
  35 import jdk.testlibrary.RandomFactory;
  36 
  37 public class CubeRootTests {
  38     private CubeRootTests(){}
  39 
  40     static final double infinityD = Double.POSITIVE_INFINITY;
  41     static final double NaNd = Double.NaN;
  42 
  43     // Initialize shared random number generator
  44     static java.util.Random rand = RandomFactory.getRandom();
  45 
  46     static int testCubeRootCase(double input, double expected) {
  47         int failures=0;
  48 
  49         double minus_input = -input;
  50         double minus_expected = -expected;
  51 
  52         failures+=Tests.test("Math.cbrt(double)", input,
  53                              Math.cbrt(input), expected);
  54         failures+=Tests.test("Math.cbrt(double)", minus_input,
  55                              Math.cbrt(minus_input), minus_expected);
  56         failures+=Tests.test("StrictMath.cbrt(double)", input,
  57                              StrictMath.cbrt(input), expected);
  58         failures+=Tests.test("StrictMath.cbrt(double)", minus_input,
  59                              StrictMath.cbrt(minus_input), minus_expected);
  60 
  61         return failures;
  62     }
  63 
  64     static int testCubeRoot() {
  65         int failures = 0;
  66         double [][] testCases = {
  67             {NaNd,                      NaNd},
  68             {Double.longBitsToDouble(0x7FF0000000000001L),      NaNd},
  69             {Double.longBitsToDouble(0xFFF0000000000001L),      NaNd},
  70             {Double.longBitsToDouble(0x7FF8555555555555L),      NaNd},
  71             {Double.longBitsToDouble(0xFFF8555555555555L),      NaNd},
  72             {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL),      NaNd},
  73             {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL),      NaNd},
  74             {Double.longBitsToDouble(0x7FFDeadBeef00000L),      NaNd},
  75             {Double.longBitsToDouble(0xFFFDeadBeef00000L),      NaNd},
  76             {Double.longBitsToDouble(0x7FFCafeBabe00000L),      NaNd},
  77             {Double.longBitsToDouble(0xFFFCafeBabe00000L),      NaNd},
  78             {Double.POSITIVE_INFINITY,  Double.POSITIVE_INFINITY},
  79             {Double.NEGATIVE_INFINITY,  Double.NEGATIVE_INFINITY},
  80             {+0.0,                      +0.0},
  81             {-0.0,                      -0.0},
  82             {+1.0,                      +1.0},
  83             {-1.0,                      -1.0},
  84             {+8.0,                      +2.0},
  85             {-8.0,                      -2.0}
  86         };
  87 
  88         for(int i = 0; i < testCases.length; i++) {
  89             failures += testCubeRootCase(testCases[i][0],
  90                                          testCases[i][1]);
  91         }
  92 
  93         // Test integer perfect cubes less than 2^53.
  94         for(int i = 0; i <= 208063; i++) {
  95             double d = i;
  96             failures += testCubeRootCase(d*d*d, (double)i);
  97         }
  98 
  99         // Test cbrt(2^(3n)) = 2^n.
 100         for(int i = 18; i <= Double.MAX_EXPONENT/3; i++) {
 101             failures += testCubeRootCase(Math.scalb(1.0, 3*i),
 102                                          Math.scalb(1.0, i) );
 103         }
 104 
 105         // Test cbrt(2^(-3n)) = 2^-n.
 106         for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) {
 107             failures += testCubeRootCase(Math.scalb(1.0, 3*i),
 108                                          Math.scalb(1.0, i) );
 109         }
 110 
 111         // Test random perfect cubes.  Create double values with
 112         // modest exponents but only have at most the 17 most
 113         // significant bits in the significand set; 17*3 = 51, which
 114         // is less than the number of bits in a double's significand.
 115         long exponentBits1 =
 116             Double.doubleToLongBits(Math.scalb(1.0, 55)) &
 117             DoubleConsts.EXP_BIT_MASK;
 118         long exponentBits2=
 119             Double.doubleToLongBits(Math.scalb(1.0, -55)) &
 120             DoubleConsts.EXP_BIT_MASK;
 121         for(int i = 0; i < 100; i++) {
 122             // Take 16 bits since the 17th bit is implicit in the
 123             // exponent
 124            double input1 =
 125                Double.longBitsToDouble(exponentBits1 |
 126                                        // Significand bits
 127                                        ((long) (rand.nextInt() & 0xFFFF))<<
 128                                        (DoubleConsts.SIGNIFICAND_WIDTH-1-16));
 129            failures += testCubeRootCase(input1*input1*input1, input1);
 130 
 131            double input2 =
 132                Double.longBitsToDouble(exponentBits2 |
 133                                        // Significand bits
 134                                        ((long) (rand.nextInt() & 0xFFFF))<<
 135                                        (DoubleConsts.SIGNIFICAND_WIDTH-1-16));
 136            failures += testCubeRootCase(input2*input2*input2, input2);
 137         }
 138 
 139         // Directly test quality of implementation properties of cbrt
 140         // for values that aren't perfect cubes.  Verify returned
 141         // result meets the 1 ulp test.  That is, we want to verify
 142         // that for positive x > 1,
 143         // y = cbrt(x),
 144         //
 145         // if (err1=x - y^3 ) < 0, abs((y_pp^3 -x )) < err1
 146         // if (err1=x - y^3 ) > 0, abs((y_mm^3 -x )) < err1
 147         //
 148         // where y_mm and y_pp are the next smaller and next larger
 149         // floating-point value to y.  In other words, if y^3 is too
 150         // big, making y larger does not improve the result; likewise,
 151         // if y^3 is too small, making y smaller does not improve the
 152         // result.
 153         //
 154         // ...-----|--?--|--?--|-----... Where is the true result?
 155         //         y_mm  y     y_pp
 156         //
 157         // The returned value y should be one of the floating-point
 158         // values braketing the true result.  However, given y, a
 159         // priori we don't know if the true result falls in [y_mm, y]
 160         // or [y, y_pp].  The above test looks at the error in x-y^3
 161         // to determine which region the true result is in; e.g. if
 162         // y^3 is smaller than x, the true result should be in [y,
 163         // y_pp].  Therefore, it would be an error for y_mm to be a
 164         // closer approximation to x^(1/3).  In this case, it is
 165         // permissible, although not ideal, for y_pp^3 to be a closer
 166         // approximation to x^(1/3) than y^3.
 167         //
 168         // We will use pow(y,3) to compute y^3.  Although pow is not
 169         // correctly rounded, StrictMath.pow should have at most 1 ulp
 170         // error.  For y > 1, pow(y_mm,3) and pow(y_pp,3) will differ
 171         // from pow(y,3) by more than one ulp so the comparision of
 172         // errors should still be valid.
 173 
 174         for(int i = 0; i < 1000; i++) {
 175             double d = 1.0 + rand.nextDouble();
 176             double err, err_adjacent;
 177 
 178             double y1 = Math.cbrt(d);
 179             double y2 = StrictMath.cbrt(d);
 180 
 181             err = d - StrictMath.pow(y1, 3);
 182             if (err != 0.0) {
 183                 if(Double.isNaN(err)) {
 184                     failures++;
 185                     System.err.println("Encountered unexpected NaN value: d = " + d +
 186                                        "\tcbrt(d) = " + y1);
 187                 } else {
 188                     if (err < 0.0) {
 189                         err_adjacent = StrictMath.pow(Math.nextUp(y1), 3) - d;
 190                     }
 191                     else  { // (err > 0.0)
 192                         err_adjacent = StrictMath.pow(Math.nextAfter(y1,0.0), 3) - d;
 193                     }
 194 
 195                     if (Math.abs(err) > Math.abs(err_adjacent)) {
 196                         failures++;
 197                         System.err.println("For Math.cbrt(" + d + "), returned result " +
 198                                            y1 + "is not as good as adjacent value.");
 199                     }
 200                 }
 201             }
 202 
 203 
 204             err = d - StrictMath.pow(y2, 3);
 205             if (err != 0.0) {
 206                 if(Double.isNaN(err)) {
 207                     failures++;
 208                     System.err.println("Encountered unexpected NaN value: d = " + d +
 209                                        "\tcbrt(d) = " + y2);
 210                 } else {
 211                     if (err < 0.0) {
 212                         err_adjacent = StrictMath.pow(Math.nextUp(y2), 3) - d;
 213                     }
 214                     else  { // (err > 0.0)
 215                         err_adjacent = StrictMath.pow(Math.nextAfter(y2,0.0), 3) - d;
 216                     }
 217 
 218                     if (Math.abs(err) > Math.abs(err_adjacent)) {
 219                         failures++;
 220                         System.err.println("For StrictMath.cbrt(" + d + "), returned result " +
 221                                            y2 + "is not as good as adjacent value.");
 222                     }
 223                 }
 224             }
 225 
 226 
 227         }
 228 
 229         // Test monotonicity properites near perfect cubes; test two
 230         // numbers before and two numbers after; i.e. for
 231         //
 232         // pcNeighbors[] =
 233         // {nextDown(nextDown(pc)),
 234         // nextDown(pc),
 235         // pc,
 236         // nextUp(pc),
 237         // nextUp(nextUp(pc))}
 238         //
 239         // test that cbrt(pcNeighbors[i]) <= cbrt(pcNeighbors[i+1])
 240         {
 241 
 242             double pcNeighbors[] = new double[5];
 243             double pcNeighborsCbrt[] = new double[5];
 244             double pcNeighborsStrictCbrt[] = new double[5];
 245 
 246             // Test near cbrt(2^(3n)) = 2^n.
 247             for(int i = 18; i <= Double.MAX_EXPONENT/3; i++) {
 248                 double pc = Math.scalb(1.0, 3*i);
 249 
 250                 pcNeighbors[2] = pc;
 251                 pcNeighbors[1] = Math.nextDown(pc);
 252                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
 253                 pcNeighbors[3] = Math.nextUp(pc);
 254                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 255 
 256                 for(int j = 0; j < pcNeighbors.length; j++) {
 257                     pcNeighborsCbrt[j] =           Math.cbrt(pcNeighbors[j]);
 258                     pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);
 259                 }
 260 
 261                 for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {
 262                     if(pcNeighborsCbrt[j] >  pcNeighborsCbrt[j+1] ) {
 263                         failures++;
 264                         System.err.println("Monotonicity failure for Math.cbrt on " +
 265                                           pcNeighbors[j] + " and "  +
 266                                           pcNeighbors[j+1] + "\n\treturned " +
 267                                           pcNeighborsCbrt[j] + " and " +
 268                                           pcNeighborsCbrt[j+1] );
 269                     }
 270 
 271                     if(pcNeighborsStrictCbrt[j] >  pcNeighborsStrictCbrt[j+1] ) {
 272                         failures++;
 273                         System.err.println("Monotonicity failure for StrictMath.cbrt on " +
 274                                           pcNeighbors[j] + " and "  +
 275                                           pcNeighbors[j+1] + "\n\treturned " +
 276                                           pcNeighborsStrictCbrt[j] + " and " +
 277                                           pcNeighborsStrictCbrt[j+1] );
 278                     }
 279 
 280 
 281                 }
 282 
 283             }
 284 
 285             // Test near cbrt(2^(-3n)) = 2^-n.
 286             for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) {
 287                 double pc = Math.scalb(1.0, 3*i);
 288 
 289                 pcNeighbors[2] = pc;
 290                 pcNeighbors[1] = Math.nextDown(pc);
 291                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
 292                 pcNeighbors[3] = Math.nextUp(pc);
 293                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 294 
 295                 for(int j = 0; j < pcNeighbors.length; j++) {
 296                     pcNeighborsCbrt[j] =           Math.cbrt(pcNeighbors[j]);
 297                     pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);
 298                 }
 299 
 300                 for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {
 301                     if(pcNeighborsCbrt[j] >  pcNeighborsCbrt[j+1] ) {
 302                         failures++;
 303                         System.err.println("Monotonicity failure for Math.cbrt on " +
 304                                           pcNeighbors[j] + " and "  +
 305                                           pcNeighbors[j+1] + "\n\treturned " +
 306                                           pcNeighborsCbrt[j] + " and " +
 307                                           pcNeighborsCbrt[j+1] );
 308                     }
 309 
 310                     if(pcNeighborsStrictCbrt[j] >  pcNeighborsStrictCbrt[j+1] ) {
 311                         failures++;
 312                         System.err.println("Monotonicity failure for StrictMath.cbrt on " +
 313                                           pcNeighbors[j] + " and "  +
 314                                           pcNeighbors[j+1] + "\n\treturned " +
 315                                           pcNeighborsStrictCbrt[j] + " and " +
 316                                           pcNeighborsStrictCbrt[j+1] );
 317                     }
 318 
 319 
 320                 }
 321             }
 322         }
 323 
 324         return failures;
 325     }
 326 
 327     public static void main(String argv[]) {
 328         int failures = 0;
 329 
 330         failures += testCubeRoot();
 331 
 332         if (failures > 0) {
 333             System.err.println("Testing cbrt incurred "
 334                                + failures + " failures.");
 335             throw new RuntimeException();
 336         }
 337     }
 338 
 339 }