test/java/lang/Math/CubeRootTests.java

Print this page




   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  */
  30 
  31 import sun.misc.DoubleConsts;
  32 
  33 public class CubeRootTests {
  34     private CubeRootTests(){}
  35 
  36     static final double infinityD = Double.POSITIVE_INFINITY;
  37     static final double NaNd = Double.NaN;
  38 
  39     // Initialize shared random number generator
  40     static java.util.Random rand = new java.util.Random();
  41 
  42     static int testCubeRootCase(double input, double expected) {
  43         int failures=0;
  44 
  45         double minus_input = -input;
  46         double minus_expected = -expected;
  47 
  48         failures+=Tests.test("Math.cbrt(double)", input,
  49                              Math.cbrt(input), expected);
  50         failures+=Tests.test("Math.cbrt(double)", minus_input,
  51                              Math.cbrt(minus_input), minus_expected);


  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         };
  83 
  84         for(int i = 0; i < testCases.length; i++) {
  85             failures += testCubeRootCase(testCases[i][0],
  86                                          testCases[i][1]);
  87         }
  88 
  89         // Test integer perfect cubes less than 2^53.
  90         for(int i = 0; i <= 208063; i++) {
  91             double d = i;
  92             failures += testCubeRootCase(d*d*d, (double)i);
  93         }
  94 
  95         // Test cbrt(2^(3n)) = 2^n.
  96         for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) {
  97             failures += testCubeRootCase(Math.scalb(1.0, 3*i),
  98                                          Math.scalb(1.0, i) );
  99         }
 100 
 101         // Test cbrt(2^(-3n)) = 2^-n.
 102         for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) {
 103             failures += testCubeRootCase(Math.scalb(1.0, 3*i),
 104                                          Math.scalb(1.0, i) );
 105         }
 106 
 107         // Test random perfect cubes.  Create double values with
 108         // modest exponents but only have at most the 17 most
 109         // significant bits in the significand set; 17*3 = 51, which
 110         // is less than the number of bits in a double's significand.
 111         long exponentBits1 =
 112             Double.doubleToLongBits(Math.scalb(1.0, 55)) &
 113             DoubleConsts.EXP_BIT_MASK;
 114         long exponentBits2=
 115             Double.doubleToLongBits(Math.scalb(1.0, -55)) &
 116             DoubleConsts.EXP_BIT_MASK;
 117         for(int i = 0; i < 100; i++) {
 118             // Take 16 bits since the 17th bit is implicit in the
 119             // exponent
 120            double input1 =
 121                Double.longBitsToDouble(exponentBits1 |
 122                                        // Significand bits
 123                                        ((long) (rand.nextInt() & 0xFFFF))<<
 124                                        (DoubleConsts.SIGNIFICAND_WIDTH-1-16));
 125            failures += testCubeRootCase(input1*input1*input1, input1);
 126 
 127            double input2 =
 128                Double.longBitsToDouble(exponentBits2 |
 129                                        // Significand bits
 130                                        ((long) (rand.nextInt() & 0xFFFF))<<
 131                                        (DoubleConsts.SIGNIFICAND_WIDTH-1-16));
 132            failures += testCubeRootCase(input2*input2*input2, input2);
 133         }
 134 
 135         // Directly test quality of implementation properties of cbrt
 136         // for values that aren't perfect cubes.  Verify returned
 137         // result meets the 1 ulp test.  That is, we want to verify
 138         // that for positive x > 1,
 139         // y = cbrt(x),
 140         //
 141         // if (err1=x - y^3 ) < 0, abs((y_pp^3 -x )) < err1
 142         // if (err1=x - y^3 ) > 0, abs((y_mm^3 -x )) < err1
 143         //
 144         // where y_mm and y_pp are the next smaller and next larger
 145         // floating-point value to y.  In other words, if y^3 is too
 146         // big, making y larger does not improve the result; likewise,
 147         // if y^3 is too small, making y smaller does not improve the
 148         // result.
 149         //
 150         // ...-----|--?--|--?--|-----... Where is the true result?
 151         //         y_mm  y     y_pp


 223         }
 224 
 225         // Test monotonicity properites near perfect cubes; test two
 226         // numbers before and two numbers after; i.e. for
 227         //
 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         {
 237 
 238             double pcNeighbors[] = new double[5];
 239             double pcNeighborsCbrt[] = new double[5];
 240             double pcNeighborsStrictCbrt[] = new double[5];
 241 
 242             // Test near cbrt(2^(3n)) = 2^n.
 243             for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) {
 244                 double pc = Math.scalb(1.0, 3*i);
 245 
 246                 pcNeighbors[2] = pc;
 247                 pcNeighbors[1] = Math.nextDown(pc);
 248                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
 249                 pcNeighbors[3] = Math.nextUp(pc);
 250                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 251 
 252                 for(int j = 0; j < pcNeighbors.length; j++) {
 253                     pcNeighborsCbrt[j] =           Math.cbrt(pcNeighbors[j]);
 254                     pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);
 255                 }
 256 
 257                 for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {
 258                     if(pcNeighborsCbrt[j] >  pcNeighborsCbrt[j+1] ) {
 259                         failures++;
 260                         System.err.println("Monotonicity failure for Math.cbrt on " +
 261                                           pcNeighbors[j] + " and "  +
 262                                           pcNeighbors[j+1] + "\n\treturned " +
 263                                           pcNeighborsCbrt[j] + " and " +
 264                                           pcNeighborsCbrt[j+1] );
 265                     }
 266 
 267                     if(pcNeighborsStrictCbrt[j] >  pcNeighborsStrictCbrt[j+1] ) {
 268                         failures++;
 269                         System.err.println("Monotonicity failure for StrictMath.cbrt on " +
 270                                           pcNeighbors[j] + " and "  +
 271                                           pcNeighbors[j+1] + "\n\treturned " +
 272                                           pcNeighborsStrictCbrt[j] + " and " +
 273                                           pcNeighborsStrictCbrt[j+1] );
 274                     }
 275 
 276 
 277                 }
 278 
 279             }
 280 
 281             // Test near cbrt(2^(-3n)) = 2^-n.
 282             for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) {
 283                 double pc = Math.scalb(1.0, 3*i);
 284 
 285                 pcNeighbors[2] = pc;
 286                 pcNeighbors[1] = Math.nextDown(pc);
 287                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
 288                 pcNeighbors[3] = Math.nextUp(pc);
 289                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 290 
 291                 for(int j = 0; j < pcNeighbors.length; j++) {
 292                     pcNeighborsCbrt[j] =           Math.cbrt(pcNeighbors[j]);
 293                     pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);
 294                 }
 295 
 296                 for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {
 297                     if(pcNeighborsCbrt[j] >  pcNeighborsCbrt[j+1] ) {
 298                         failures++;
 299                         System.err.println("Monotonicity failure for Math.cbrt on " +
 300                                           pcNeighbors[j] + " and "  +
 301                                           pcNeighbors[j+1] + "\n\treturned " +
 302                                           pcNeighborsCbrt[j] + " and " +




   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  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.DoubleUtils jdk.testlibrary.FloatUtils
  30  * @run main CubeRootTests
  31  * @author Joseph D. Darcy
  32  */
  33 
  34 import static jdk.testlibrary.DoubleUtils.*;
  35 
  36 public class CubeRootTests {
  37     private CubeRootTests(){}
  38 
  39     static final double infinityD = Double.POSITIVE_INFINITY;
  40     static final double NaNd = Double.NaN;
  41 
  42     // Initialize shared random number generator
  43     static java.util.Random rand = new java.util.Random();
  44 
  45     static int testCubeRootCase(double input, double expected) {
  46         int failures=0;
  47 
  48         double minus_input = -input;
  49         double minus_expected = -expected;
  50 
  51         failures+=Tests.test("Math.cbrt(double)", input,
  52                              Math.cbrt(input), expected);
  53         failures+=Tests.test("Math.cbrt(double)", minus_input,
  54                              Math.cbrt(minus_input), minus_expected);


  79             {+0.0,                      +0.0},
  80             {-0.0,                      -0.0},
  81             {+1.0,                      +1.0},
  82             {-1.0,                      -1.0},
  83             {+8.0,                      +2.0},
  84             {-8.0,                      -2.0}
  85         };
  86 
  87         for(int i = 0; i < testCases.length; i++) {
  88             failures += testCubeRootCase(testCases[i][0],
  89                                          testCases[i][1]);
  90         }
  91 
  92         // Test integer perfect cubes less than 2^53.
  93         for(int i = 0; i <= 208063; i++) {
  94             double d = i;
  95             failures += testCubeRootCase(d*d*d, (double)i);
  96         }
  97 
  98         // Test cbrt(2^(3n)) = 2^n.
  99         for(int i = 18; i <= Double.MAX_EXPONENT/3; i++) {
 100             failures += testCubeRootCase(Math.scalb(1.0, 3*i),
 101                                          Math.scalb(1.0, i) );
 102         }
 103 
 104         // Test cbrt(2^(-3n)) = 2^-n.
 105         for(int i = -1; i >= MIN_SUB_EXPONENT/3; i--) {
 106             failures += testCubeRootCase(Math.scalb(1.0, 3*i),
 107                                          Math.scalb(1.0, i) );
 108         }
 109 
 110         // Test random perfect cubes.  Create double values with
 111         // modest exponents but only have at most the 17 most
 112         // significant bits in the significand set; 17*3 = 51, which
 113         // is less than the number of bits in a double's significand.
 114         long exponentBits1 =
 115             Double.doubleToLongBits(Math.scalb(1.0, 55)) &
 116             EXP_BIT_MASK;
 117         long exponentBits2=
 118             Double.doubleToLongBits(Math.scalb(1.0, -55)) &
 119             EXP_BIT_MASK;
 120         for(int i = 0; i < 100; i++) {
 121             // Take 16 bits since the 17th bit is implicit in the
 122             // exponent
 123            double input1 =
 124                Double.longBitsToDouble(exponentBits1 |
 125                                        // Significand bits
 126                                        ((long) (rand.nextInt() & 0xFFFF))<<
 127                                        (SIGNIFICAND_WIDTH-1-16));
 128            failures += testCubeRootCase(input1*input1*input1, input1);
 129 
 130            double input2 =
 131                Double.longBitsToDouble(exponentBits2 |
 132                                        // Significand bits
 133                                        ((long) (rand.nextInt() & 0xFFFF))<<
 134                                        (SIGNIFICAND_WIDTH-1-16));
 135            failures += testCubeRootCase(input2*input2*input2, input2);
 136         }
 137 
 138         // Directly test quality of implementation properties of cbrt
 139         // for values that aren't perfect cubes.  Verify returned
 140         // result meets the 1 ulp test.  That is, we want to verify
 141         // that for positive x > 1,
 142         // y = cbrt(x),
 143         //
 144         // if (err1=x - y^3 ) < 0, abs((y_pp^3 -x )) < err1
 145         // if (err1=x - y^3 ) > 0, abs((y_mm^3 -x )) < err1
 146         //
 147         // where y_mm and y_pp are the next smaller and next larger
 148         // floating-point value to y.  In other words, if y^3 is too
 149         // big, making y larger does not improve the result; likewise,
 150         // if y^3 is too small, making y smaller does not improve the
 151         // result.
 152         //
 153         // ...-----|--?--|--?--|-----... Where is the true result?
 154         //         y_mm  y     y_pp


 226         }
 227 
 228         // Test monotonicity properites near perfect cubes; test two
 229         // numbers before and two numbers after; i.e. for
 230         //
 231         // pcNeighbors[] =
 232         // {nextDown(nextDown(pc)),
 233         // nextDown(pc),
 234         // pc,
 235         // nextUp(pc),
 236         // nextUp(nextUp(pc))}
 237         //
 238         // test that cbrt(pcNeighbors[i]) <= cbrt(pcNeighbors[i+1])
 239         {
 240 
 241             double pcNeighbors[] = new double[5];
 242             double pcNeighborsCbrt[] = new double[5];
 243             double pcNeighborsStrictCbrt[] = new double[5];
 244 
 245             // Test near cbrt(2^(3n)) = 2^n.
 246             for(int i = 18; i <= Double.MAX_EXPONENT/3; i++) {
 247                 double pc = Math.scalb(1.0, 3*i);
 248 
 249                 pcNeighbors[2] = pc;
 250                 pcNeighbors[1] = Math.nextDown(pc);
 251                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
 252                 pcNeighbors[3] = Math.nextUp(pc);
 253                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 254 
 255                 for(int j = 0; j < pcNeighbors.length; j++) {
 256                     pcNeighborsCbrt[j] =           Math.cbrt(pcNeighbors[j]);
 257                     pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);
 258                 }
 259 
 260                 for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {
 261                     if(pcNeighborsCbrt[j] >  pcNeighborsCbrt[j+1] ) {
 262                         failures++;
 263                         System.err.println("Monotonicity failure for Math.cbrt on " +
 264                                           pcNeighbors[j] + " and "  +
 265                                           pcNeighbors[j+1] + "\n\treturned " +
 266                                           pcNeighborsCbrt[j] + " and " +
 267                                           pcNeighborsCbrt[j+1] );
 268                     }
 269 
 270                     if(pcNeighborsStrictCbrt[j] >  pcNeighborsStrictCbrt[j+1] ) {
 271                         failures++;
 272                         System.err.println("Monotonicity failure for StrictMath.cbrt on " +
 273                                           pcNeighbors[j] + " and "  +
 274                                           pcNeighbors[j+1] + "\n\treturned " +
 275                                           pcNeighborsStrictCbrt[j] + " and " +
 276                                           pcNeighborsStrictCbrt[j+1] );
 277                     }
 278 
 279 
 280                 }
 281 
 282             }
 283 
 284             // Test near cbrt(2^(-3n)) = 2^-n.
 285             for(int i = -1; i >= MIN_SUB_EXPONENT/3; i--) {
 286                 double pc = Math.scalb(1.0, 3*i);
 287 
 288                 pcNeighbors[2] = pc;
 289                 pcNeighbors[1] = Math.nextDown(pc);
 290                 pcNeighbors[0] = Math.nextDown(pcNeighbors[1]);
 291                 pcNeighbors[3] = Math.nextUp(pc);
 292                 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]);
 293 
 294                 for(int j = 0; j < pcNeighbors.length; j++) {
 295                     pcNeighborsCbrt[j] =           Math.cbrt(pcNeighbors[j]);
 296                     pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]);
 297                 }
 298 
 299                 for(int j = 0; j < pcNeighborsCbrt.length-1; j++) {
 300                     if(pcNeighborsCbrt[j] >  pcNeighborsCbrt[j+1] ) {
 301                         failures++;
 302                         System.err.println("Monotonicity failure for Math.cbrt on " +
 303                                           pcNeighbors[j] + " and "  +
 304                                           pcNeighbors[j+1] + "\n\treturned " +
 305                                           pcNeighborsCbrt[j] + " and " +