## test/java/lang/Math/CubeRootTests.java

 ``` `````` 78 {-0.0, -0.0}, 79 {+1.0, +1.0}, 80 {-1.0, -1.0}, 81 {+8.0, +2.0}, 82 {-8.0, -2.0} 83 }; 84 85 for(int i = 0; i < testCases.length; i++) { 86 failures += testCubeRootCase(testCases[i][0], 87 testCases[i][1]); 88 } 89 90 // Test integer perfect cubes less than 2^53. 91 for(int i = 0; i <= 208063; i++) { 92 double d = i; 93 failures += testCubeRootCase(d*d*d, (double)i); 94 } 95 96 // Test cbrt(2^(3n)) = 2^n. 97 for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) { 98 failures += testCubeRootCase(FpUtils.scalb(1.0, 3*i), 99 FpUtils.scalb(1.0, i) ); 100 } 101 102 // Test cbrt(2^(-3n)) = 2^-n. 103 for(int i = -1; i >= FpUtils.ilogb(Double.MIN_VALUE)/3; i--) { 104 failures += testCubeRootCase(FpUtils.scalb(1.0, 3*i), 105 FpUtils.scalb(1.0, i) ); 106 } 107 108 // Test random perfect cubes. Create double values with 109 // modest exponents but only have at most the 17 most 110 // significant bits in the significand set; 17*3 = 51, which 111 // is less than the number of bits in a double's significand. 112 long exponentBits1 = 113 Double.doubleToLongBits(FpUtils.scalb(1.0, 55)) & 114 DoubleConsts.EXP_BIT_MASK; 115 long exponentBits2= 116 Double.doubleToLongBits(FpUtils.scalb(1.0, -55)) & 117 DoubleConsts.EXP_BIT_MASK; 118 for(int i = 0; i < 100; i++) { 119 // Take 16 bits since the 17th bit is implicit in the 120 // exponent 121 double input1 = 122 Double.longBitsToDouble(exponentBits1 | 123 // Significand bits 124 ((long) (rand.nextInt() & 0xFFFF))<< 125 (DoubleConsts.SIGNIFICAND_WIDTH-1-16)); 126 failures += testCubeRootCase(input1*input1*input1, input1); 127 128 double input2 = 129 Double.longBitsToDouble(exponentBits2 | 130 // Significand bits 131 ((long) (rand.nextInt() & 0xFFFF))<< 132 (DoubleConsts.SIGNIFICAND_WIDTH-1-16)); 133 failures += testCubeRootCase(input2*input2*input2, input2); 134 } 135 136 // Directly test quality of implementation properties of cbrt `````` 160 // y_pp]. Therefore, it would be an error for y_mm to be a 161 // closer approximation to x^(1/3). In this case, it is 162 // permissible, although not ideal, for y_pp^3 to be a closer 163 // approximation to x^(1/3) than y^3. 164 // 165 // We will use pow(y,3) to compute y^3. Although pow is not 166 // correctly rounded, StrictMath.pow should have at most 1 ulp 167 // error. For y > 1, pow(y_mm,3) and pow(y_pp,3) will differ 168 // from pow(y,3) by more than one ulp so the comparision of 169 // errors should still be valid. 170 171 for(int i = 0; i < 1000; i++) { 172 double d = 1.0 + rand.nextDouble(); 173 double err, err_adjacent; 174 175 double y1 = Math.cbrt(d); 176 double y2 = StrictMath.cbrt(d); 177 178 err = d - StrictMath.pow(y1, 3); 179 if (err != 0.0) { 180 if(FpUtils.isNaN(err)) { 181 failures++; 182 System.err.println("Encountered unexpected NaN value: d = " + d + 183 "\tcbrt(d) = " + y1); 184 } else { 185 if (err < 0.0) { 186 err_adjacent = StrictMath.pow(FpUtils.nextUp(y1), 3) - d; 187 } 188 else { // (err > 0.0) 189 err_adjacent = StrictMath.pow(FpUtils.nextAfter(y1,0.0), 3) - d; 190 } 191 192 if (Math.abs(err) > Math.abs(err_adjacent)) { 193 failures++; 194 System.err.println("For Math.cbrt(" + d + "), returned result " + 195 y1 + "is not as good as adjacent value."); 196 } 197 } 198 } 199 200 201 err = d - StrictMath.pow(y2, 3); 202 if (err != 0.0) { 203 if(FpUtils.isNaN(err)) { 204 failures++; 205 System.err.println("Encountered unexpected NaN value: d = " + d + 206 "\tcbrt(d) = " + y2); 207 } else { 208 if (err < 0.0) { 209 err_adjacent = StrictMath.pow(FpUtils.nextUp(y2), 3) - d; 210 } 211 else { // (err > 0.0) 212 err_adjacent = StrictMath.pow(FpUtils.nextAfter(y2,0.0), 3) - d; 213 } 214 215 if (Math.abs(err) > Math.abs(err_adjacent)) { 216 failures++; 217 System.err.println("For StrictMath.cbrt(" + d + "), returned result " + 218 y2 + "is not as good as adjacent value."); 219 } 220 } 221 } 222 223 224 } 225 226 // Test monotonicity properites near perfect cubes; test two 227 // numbers before and two numbers after; i.e. for 228 // 229 // pcNeighbors[] = 230 // {nextDown(nextDown(pc)), 231 // nextDown(pc), 232 // pc, 233 // nextUp(pc), 234 // nextUp(nextUp(pc))} 235 // 236 // test that cbrt(pcNeighbors[i]) <= cbrt(pcNeighbors[i+1]) 237 { 238 239 double pcNeighbors[] = new double[5]; 240 double pcNeighborsCbrt[] = new double[5]; 241 double pcNeighborsStrictCbrt[] = new double[5]; 242 243 // Test near cbrt(2^(3n)) = 2^n. 244 for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) { 245 double pc = FpUtils.scalb(1.0, 3*i); 246 247 pcNeighbors[2] = pc; 248 pcNeighbors[1] = FpUtils.nextDown(pc); 249 pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]); 250 pcNeighbors[3] = FpUtils.nextUp(pc); 251 pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]); 252 253 for(int j = 0; j < pcNeighbors.length; j++) { 254 pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]); 255 pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]); 256 } 257 258 for(int j = 0; j < pcNeighborsCbrt.length-1; j++) { 259 if(pcNeighborsCbrt[j] > pcNeighborsCbrt[j+1] ) { 260 failures++; 261 System.err.println("Monotonicity failure for Math.cbrt on " + 262 pcNeighbors[j] + " and " + 263 pcNeighbors[j+1] + "\n\treturned " + 264 pcNeighborsCbrt[j] + " and " + 265 pcNeighborsCbrt[j+1] ); 266 } 267 268 if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) { 269 failures++; 270 System.err.println("Monotonicity failure for StrictMath.cbrt on " + 271 pcNeighbors[j] + " and " + 272 pcNeighbors[j+1] + "\n\treturned " + 273 pcNeighborsStrictCbrt[j] + " and " + 274 pcNeighborsStrictCbrt[j+1] ); 275 } 276 277 278 } 279 280 } 281 282 // Test near cbrt(2^(-3n)) = 2^-n. 283 for(int i = -1; i >= FpUtils.ilogb(Double.MIN_VALUE)/3; i--) { 284 double pc = FpUtils.scalb(1.0, 3*i); 285 286 pcNeighbors[2] = pc; 287 pcNeighbors[1] = FpUtils.nextDown(pc); 288 pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]); 289 pcNeighbors[3] = FpUtils.nextUp(pc); 290 pcNeighbors[4] = FpUtils.nextUp(pcNeighbors[3]); 291 292 for(int j = 0; j < pcNeighbors.length; j++) { 293 pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]); 294 pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]); 295 } 296 297 for(int j = 0; j < pcNeighborsCbrt.length-1; j++) { 298 if(pcNeighborsCbrt[j] > pcNeighborsCbrt[j+1] ) { 299 failures++; 300 System.err.println("Monotonicity failure for Math.cbrt on " + 301 pcNeighbors[j] + " and " + 302 pcNeighbors[j+1] + "\n\treturned " + 303 pcNeighborsCbrt[j] + " and " + 304 pcNeighborsCbrt[j+1] ); 305 } 306 307 if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) { 308 failures++; 309 System.err.println("Monotonicity failure for StrictMath.cbrt on " + 310 pcNeighbors[j] + " and " + ``` ``` `````` 78 {-0.0, -0.0}, 79 {+1.0, +1.0}, 80 {-1.0, -1.0}, 81 {+8.0, +2.0}, 82 {-8.0, -2.0} 83 }; 84 85 for(int i = 0; i < testCases.length; i++) { 86 failures += testCubeRootCase(testCases[i][0], 87 testCases[i][1]); 88 } 89 90 // Test integer perfect cubes less than 2^53. 91 for(int i = 0; i <= 208063; i++) { 92 double d = i; 93 failures += testCubeRootCase(d*d*d, (double)i); 94 } 95 96 // Test cbrt(2^(3n)) = 2^n. 97 for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) { 98 failures += testCubeRootCase(Math.scalb(1.0, 3*i), 99 Math.scalb(1.0, i) ); 100 } 101 102 // Test cbrt(2^(-3n)) = 2^-n. 103 for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) { 104 failures += testCubeRootCase(Math.scalb(1.0, 3*i), 105 Math.scalb(1.0, i) ); 106 } 107 108 // Test random perfect cubes. Create double values with 109 // modest exponents but only have at most the 17 most 110 // significant bits in the significand set; 17*3 = 51, which 111 // is less than the number of bits in a double's significand. 112 long exponentBits1 = 113 Double.doubleToLongBits(Math.scalb(1.0, 55)) & 114 DoubleConsts.EXP_BIT_MASK; 115 long exponentBits2= 116 Double.doubleToLongBits(Math.scalb(1.0, -55)) & 117 DoubleConsts.EXP_BIT_MASK; 118 for(int i = 0; i < 100; i++) { 119 // Take 16 bits since the 17th bit is implicit in the 120 // exponent 121 double input1 = 122 Double.longBitsToDouble(exponentBits1 | 123 // Significand bits 124 ((long) (rand.nextInt() & 0xFFFF))<< 125 (DoubleConsts.SIGNIFICAND_WIDTH-1-16)); 126 failures += testCubeRootCase(input1*input1*input1, input1); 127 128 double input2 = 129 Double.longBitsToDouble(exponentBits2 | 130 // Significand bits 131 ((long) (rand.nextInt() & 0xFFFF))<< 132 (DoubleConsts.SIGNIFICAND_WIDTH-1-16)); 133 failures += testCubeRootCase(input2*input2*input2, input2); 134 } 135 136 // Directly test quality of implementation properties of cbrt `````` 160 // y_pp]. Therefore, it would be an error for y_mm to be a 161 // closer approximation to x^(1/3). In this case, it is 162 // permissible, although not ideal, for y_pp^3 to be a closer 163 // approximation to x^(1/3) than y^3. 164 // 165 // We will use pow(y,3) to compute y^3. Although pow is not 166 // correctly rounded, StrictMath.pow should have at most 1 ulp 167 // error. For y > 1, pow(y_mm,3) and pow(y_pp,3) will differ 168 // from pow(y,3) by more than one ulp so the comparision of 169 // errors should still be valid. 170 171 for(int i = 0; i < 1000; i++) { 172 double d = 1.0 + rand.nextDouble(); 173 double err, err_adjacent; 174 175 double y1 = Math.cbrt(d); 176 double y2 = StrictMath.cbrt(d); 177 178 err = d - StrictMath.pow(y1, 3); 179 if (err != 0.0) { 180 if(Double.isNaN(err)) { 181 failures++; 182 System.err.println("Encountered unexpected NaN value: d = " + d + 183 "\tcbrt(d) = " + y1); 184 } else { 185 if (err < 0.0) { 186 err_adjacent = StrictMath.pow(Math.nextUp(y1), 3) - d; 187 } 188 else { // (err > 0.0) 189 err_adjacent = StrictMath.pow(Math.nextAfter(y1,0.0), 3) - d; 190 } 191 192 if (Math.abs(err) > Math.abs(err_adjacent)) { 193 failures++; 194 System.err.println("For Math.cbrt(" + d + "), returned result " + 195 y1 + "is not as good as adjacent value."); 196 } 197 } 198 } 199 200 201 err = d - StrictMath.pow(y2, 3); 202 if (err != 0.0) { 203 if(Double.isNaN(err)) { 204 failures++; 205 System.err.println("Encountered unexpected NaN value: d = " + d + 206 "\tcbrt(d) = " + y2); 207 } else { 208 if (err < 0.0) { 209 err_adjacent = StrictMath.pow(Math.nextUp(y2), 3) - d; 210 } 211 else { // (err > 0.0) 212 err_adjacent = StrictMath.pow(Math.nextAfter(y2,0.0), 3) - d; 213 } 214 215 if (Math.abs(err) > Math.abs(err_adjacent)) { 216 failures++; 217 System.err.println("For StrictMath.cbrt(" + d + "), returned result " + 218 y2 + "is not as good as adjacent value."); 219 } 220 } 221 } 222 223 224 } 225 226 // Test monotonicity properites near perfect cubes; test two 227 // numbers before and two numbers after; i.e. for 228 // 229 // pcNeighbors[] = 230 // {nextDown(nextDown(pc)), 231 // nextDown(pc), 232 // pc, 233 // nextUp(pc), 234 // nextUp(nextUp(pc))} 235 // 236 // test that cbrt(pcNeighbors[i]) <= cbrt(pcNeighbors[i+1]) 237 { 238 239 double pcNeighbors[] = new double[5]; 240 double pcNeighborsCbrt[] = new double[5]; 241 double pcNeighborsStrictCbrt[] = new double[5]; 242 243 // Test near cbrt(2^(3n)) = 2^n. 244 for(int i = 18; i <= DoubleConsts.MAX_EXPONENT/3; i++) { 245 double pc = Math.scalb(1.0, 3*i); 246 247 pcNeighbors[2] = pc; 248 pcNeighbors[1] = FpUtils.nextDown(pc); 249 pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]); 250 pcNeighbors[3] = Math.nextUp(pc); 251 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]); 252 253 for(int j = 0; j < pcNeighbors.length; j++) { 254 pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]); 255 pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]); 256 } 257 258 for(int j = 0; j < pcNeighborsCbrt.length-1; j++) { 259 if(pcNeighborsCbrt[j] > pcNeighborsCbrt[j+1] ) { 260 failures++; 261 System.err.println("Monotonicity failure for Math.cbrt on " + 262 pcNeighbors[j] + " and " + 263 pcNeighbors[j+1] + "\n\treturned " + 264 pcNeighborsCbrt[j] + " and " + 265 pcNeighborsCbrt[j+1] ); 266 } 267 268 if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) { 269 failures++; 270 System.err.println("Monotonicity failure for StrictMath.cbrt on " + 271 pcNeighbors[j] + " and " + 272 pcNeighbors[j+1] + "\n\treturned " + 273 pcNeighborsStrictCbrt[j] + " and " + 274 pcNeighborsStrictCbrt[j+1] ); 275 } 276 277 278 } 279 280 } 281 282 // Test near cbrt(2^(-3n)) = 2^-n. 283 for(int i = -1; i >= DoubleConsts.MIN_SUB_EXPONENT/3; i--) { 284 double pc = Math.scalb(1.0, 3*i); 285 286 pcNeighbors[2] = pc; 287 pcNeighbors[1] = FpUtils.nextDown(pc); 288 pcNeighbors[0] = FpUtils.nextDown(pcNeighbors[1]); 289 pcNeighbors[3] = Math.nextUp(pc); 290 pcNeighbors[4] = Math.nextUp(pcNeighbors[3]); 291 292 for(int j = 0; j < pcNeighbors.length; j++) { 293 pcNeighborsCbrt[j] = Math.cbrt(pcNeighbors[j]); 294 pcNeighborsStrictCbrt[j] = StrictMath.cbrt(pcNeighbors[j]); 295 } 296 297 for(int j = 0; j < pcNeighborsCbrt.length-1; j++) { 298 if(pcNeighborsCbrt[j] > pcNeighborsCbrt[j+1] ) { 299 failures++; 300 System.err.println("Monotonicity failure for Math.cbrt on " + 301 pcNeighbors[j] + " and " + 302 pcNeighbors[j+1] + "\n\treturned " + 303 pcNeighborsCbrt[j] + " and " + 304 pcNeighborsCbrt[j+1] ); 305 } 306 307 if(pcNeighborsStrictCbrt[j] > pcNeighborsStrictCbrt[j+1] ) { 308 failures++; 309 System.err.println("Monotonicity failure for StrictMath.cbrt on " + 310 pcNeighbors[j] + " and " + ```