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 */ 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); 52 failures+=Tests.test("StrictMath.cbrt(double)", input, 53 StrictMath.cbrt(input), expected); 54 failures+=Tests.test("StrictMath.cbrt(double)", minus_input, 55 StrictMath.cbrt(minus_input), minus_expected); 56 57 return failures; 58 } 59 60 static int testCubeRoot() { 61 int failures = 0; 62 double [][] testCases = { 63 {NaNd, NaNd}, 64 {Double.longBitsToDouble(0x7FF0000000000001L), NaNd}, 65 {Double.longBitsToDouble(0xFFF0000000000001L), NaNd}, 66 {Double.longBitsToDouble(0x7FF8555555555555L), NaNd}, 67 {Double.longBitsToDouble(0xFFF8555555555555L), NaNd}, 68 {Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd}, 69 {Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd}, 70 {Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd}, 71 {Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd}, 72 {Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd}, 73 {Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd}, 74 {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}, 75 {Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY}, 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 152 // 153 // The returned value y should be one of the floating-point 154 // values braketing the true result. However, given y, a 155 // priori we don't know if the true result falls in [y_mm, y] 156 // or [y, y_pp]. The above test looks at the error in x-y^3 157 // to determine which region the true result is in; e.g. if 158 // y^3 is smaller than x, the true result should be in [y, 159 // y_pp]. Therefore, it would be an error for y_mm to be a 160 // closer approximation to x^(1/3). In this case, it is 161 // permissible, although not ideal, for y_pp^3 to be a closer 162 // approximation to x^(1/3) than y^3. 163 // 164 // We will use pow(y,3) to compute y^3. Although pow is not 165 // correctly rounded, StrictMath.pow should have at most 1 ulp 166 // error. For y > 1, pow(y_mm,3) and pow(y_pp,3) will differ 167 // from pow(y,3) by more than one ulp so the comparision of 168 // errors should still be valid. 169 170 for(int i = 0; i < 1000; i++) { 171 double d = 1.0 + rand.nextDouble(); 172 double err, err_adjacent; 173 174 double y1 = Math.cbrt(d); 175 double y2 = StrictMath.cbrt(d); 176 177 err = d - StrictMath.pow(y1, 3); 178 if (err != 0.0) { 179 if(Double.isNaN(err)) { 180 failures++; 181 System.err.println("Encountered unexpected NaN value: d = " + d + 182 "\tcbrt(d) = " + y1); 183 } else { 184 if (err < 0.0) { 185 err_adjacent = StrictMath.pow(Math.nextUp(y1), 3) - d; 186 } 187 else { // (err > 0.0) 188 err_adjacent = StrictMath.pow(Math.nextAfter(y1,0.0), 3) - d; 189 } 190 191 if (Math.abs(err) > Math.abs(err_adjacent)) { 192 failures++; 193 System.err.println("For Math.cbrt(" + d + "), returned result " + 194 y1 + "is not as good as adjacent value."); 195 } 196 } 197 } 198 199 200 err = d - StrictMath.pow(y2, 3); 201 if (err != 0.0) { 202 if(Double.isNaN(err)) { 203 failures++; 204 System.err.println("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 } 210 else { // (err > 0.0) 211 err_adjacent = StrictMath.pow(Math.nextAfter(y2,0.0), 3) - d; 212 } 213 214 if (Math.abs(err) > Math.abs(err_adjacent)) { 215 failures++; 216 System.err.println("For StrictMath.cbrt(" + d + "), returned result " + 217 y2 + "is not as good as adjacent value."); 218 } 219 } 220 } 221 222 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 " + 303 pcNeighborsCbrt[j+1] ); 304 } 305 306 if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) { 307 failures++; 308 System.err.println("Monotonicity failure for StrictMath.cbrt on " + 309 pcNeighbors[j] + " and " + 310 pcNeighbors[j+1] + "\n\treturned " + 311 pcNeighborsStrictCbrt[j] + " and " + 312 pcNeighborsStrictCbrt[j+1] ); 313 } 314 315 316 } 317 } 318 } 319 320 return failures; 321 } 322 323 public static void main(String argv[]) { 324 int failures = 0; 325 326 failures += testCubeRoot(); 327 328 if (failures > 0) { 329 System.err.println("Testing cbrt incurred " 330 + failures + " failures."); 331 throw new RuntimeException(); 332 } 333 } 334 335 }