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