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