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