1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.tests.java.lang; 19 20 public class MathTest extends junit.framework.TestCase { 21 22 double HYP = Math.sqrt(2.0); 23 24 double OPP = 1.0; 25 26 double ADJ = 1.0; 27 28 /* Required to make previous preprocessor flags work - do not remove */ 29 int unused = 0; 30 31 /** 32 * java.lang.Math#abs(double) 33 */ test_absD()34 public void test_absD() { 35 // Test for method double java.lang.Math.abs(double) 36 37 assertTrue("Incorrect double abs value", 38 (Math.abs(-1908.8976) == 1908.8976)); 39 assertTrue("Incorrect double abs value", 40 (Math.abs(1908.8976) == 1908.8976)); 41 } 42 43 /** 44 * java.lang.Math#abs(float) 45 */ test_absF()46 public void test_absF() { 47 // Test for method float java.lang.Math.abs(float) 48 assertTrue("Incorrect float abs value", 49 (Math.abs(-1908.8976f) == 1908.8976f)); 50 assertTrue("Incorrect float abs value", 51 (Math.abs(1908.8976f) == 1908.8976f)); 52 } 53 54 /** 55 * java.lang.Math#abs(int) 56 */ test_absI()57 public void test_absI() { 58 // Test for method int java.lang.Math.abs(int) 59 assertTrue("Incorrect int abs value", (Math.abs(-1908897) == 1908897)); 60 assertTrue("Incorrect int abs value", (Math.abs(1908897) == 1908897)); 61 } 62 63 /** 64 * java.lang.Math#abs(long) 65 */ test_absJ()66 public void test_absJ() { 67 // Test for method long java.lang.Math.abs(long) 68 assertTrue("Incorrect long abs value", 69 (Math.abs(-19088976000089L) == 19088976000089L)); 70 assertTrue("Incorrect long abs value", 71 (Math.abs(19088976000089L) == 19088976000089L)); 72 } 73 74 /** 75 * java.lang.Math#acos(double) 76 */ test_acosD()77 public void test_acosD() { 78 // Test for method double java.lang.Math.acos(double) 79 double r = Math.cos(Math.acos(ADJ / HYP)); 80 long lr = Double.doubleToLongBits(r); 81 long t = Double.doubleToLongBits(ADJ / HYP); 82 assertTrue("Returned incorrect arc cosine", lr == t || (lr + 1) == t 83 || (lr - 1) == t); 84 } 85 86 /** 87 * java.lang.Math#asin(double) 88 */ test_asinD()89 public void test_asinD() { 90 // Test for method double java.lang.Math.asin(double) 91 double r = Math.sin(Math.asin(OPP / HYP)); 92 long lr = Double.doubleToLongBits(r); 93 long t = Double.doubleToLongBits(OPP / HYP); 94 assertTrue("Returned incorrect arc sine", lr == t || (lr + 1) == t 95 || (lr - 1) == t); 96 } 97 98 /** 99 * java.lang.Math#atan(double) 100 */ test_atanD()101 public void test_atanD() { 102 // Test for method double java.lang.Math.atan(double) 103 double answer = Math.tan(Math.atan(1.0)); 104 assertTrue("Returned incorrect arc tangent: " + answer, answer <= 1.0 105 && answer >= 9.9999999999999983E-1); 106 } 107 108 /** 109 * java.lang.Math#atan2(double, double) 110 */ test_atan2DD()111 public void test_atan2DD() { 112 // Test for method double java.lang.Math.atan2(double, double) 113 double answer = Math.atan(Math.tan(1.0)); 114 assertTrue("Returned incorrect arc tangent: " + answer, answer <= 1.0 115 && answer >= 9.9999999999999983E-1); 116 } 117 118 /** 119 * java.lang.Math#cbrt(double) 120 */ test_cbrt_D()121 public void test_cbrt_D() { 122 //Test for special situations 123 assertTrue(Double.isNaN(Math.cbrt(Double.NaN))); 124 assertEquals(Double.POSITIVE_INFINITY, Math.cbrt(Double.POSITIVE_INFINITY), 0D); 125 assertEquals(Double.NEGATIVE_INFINITY, Math.cbrt(Double.NEGATIVE_INFINITY), 0D); 126 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math.cbrt(0.0))); 127 assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math.cbrt(+0.0))); 128 assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math.cbrt(-0.0))); 129 130 assertEquals(3.0, Math.cbrt(27.0), 0D); 131 assertEquals(23.111993172558684, Math.cbrt(12345.6), Math.ulp(23.111993172558684)); 132 assertEquals(5.643803094122362E102, Math.cbrt(Double.MAX_VALUE), 0D); 133 assertEquals(0.01, Math.cbrt(0.000001), 0D); 134 135 assertEquals(-3.0, Math.cbrt(-27.0), 0D); 136 assertEquals(-23.111993172558684, Math.cbrt(-12345.6), Math.ulp(-23.111993172558684)); 137 assertEquals(1.7031839360032603E-108, Math.cbrt(Double.MIN_VALUE), 0D); 138 assertEquals(-0.01, Math.cbrt(-0.000001), 0D); 139 } 140 141 /** 142 * java.lang.Math#ceil(double) 143 */ test_ceilD()144 public void test_ceilD() { 145 // Test for method double java.lang.Math.ceil(double) 146 assertEquals("Incorrect ceiling for double", 147 79, Math.ceil(78.89), 0); 148 assertEquals("Incorrect ceiling for double", 149 -78, Math.ceil(-78.89), 0); 150 } 151 152 /** 153 * cases for test_copySign_DD in MathTest/StrictMathTest 154 */ 155 static final double[] COPYSIGN_DD_CASES = new double[] { 156 Double.POSITIVE_INFINITY, Double.MAX_VALUE, 3.4E302, 2.3, 157 Double.MIN_NORMAL, Double.MIN_NORMAL / 2, Double.MIN_VALUE, +0.0, 158 0.0, -0.0, -Double.MIN_VALUE, -Double.MIN_NORMAL / 2, 159 -Double.MIN_NORMAL, -4.5, -3.4E102, -Double.MAX_VALUE, 160 Double.NEGATIVE_INFINITY }; 161 162 /** 163 * {@link java.lang.Math#copySign(double, double)} 164 * @since 1.6 165 */ 166 @SuppressWarnings("boxing") test_copySign_DD()167 public void test_copySign_DD() { 168 for (int i = 0; i < COPYSIGN_DD_CASES.length; i++) { 169 final double magnitude = COPYSIGN_DD_CASES[i]; 170 final long absMagnitudeBits = Double.doubleToLongBits(Math 171 .abs(magnitude)); 172 final long negMagnitudeBits = Double.doubleToLongBits(-Math 173 .abs(magnitude)); 174 175 // cases for NaN 176 assertEquals("If the sign is NaN, the result should be positive.", 177 absMagnitudeBits, Double.doubleToLongBits(Math.copySign( 178 magnitude, Double.NaN))); 179 assertTrue("The result should be NaN.", Double.isNaN(Math.copySign( 180 Double.NaN, magnitude))); 181 182 for (int j = 0; j < COPYSIGN_DD_CASES.length; j++) { 183 final double sign = COPYSIGN_DD_CASES[j]; 184 final long resultBits = Double.doubleToLongBits(Math.copySign( 185 magnitude, sign)); 186 187 if (sign > 0 || Double.valueOf(+0.0).equals(sign) 188 || Double.valueOf(0.0).equals(sign)) { 189 assertEquals( 190 "If the sign is positive, the result should be positive.", 191 absMagnitudeBits, resultBits); 192 } 193 if (sign < 0 || Double.valueOf(-0.0).equals(sign)) { 194 assertEquals( 195 "If the sign is negative, the result should be negative.", 196 negMagnitudeBits, resultBits); 197 } 198 } 199 } 200 201 assertTrue("The result should be NaN.", Double.isNaN(Math.copySign( 202 Double.NaN, Double.NaN))); 203 204 try { 205 Math.copySign((Double) null, 2.3); 206 fail("Should throw NullPointerException"); 207 } catch (NullPointerException e) { 208 // Expected 209 } 210 try { 211 Math.copySign(2.3, (Double) null); 212 fail("Should throw NullPointerException"); 213 } catch (NullPointerException e) { 214 // Expected 215 } 216 try { 217 Math.copySign((Double) null, (Double) null); 218 fail("Should throw NullPointerException"); 219 } catch (NullPointerException e) { 220 // Expected 221 } 222 } 223 224 /** 225 * cases for test_copySign_FF in MathTest/StrictMathTest 226 */ 227 static final float[] COPYSIGN_FF_CASES = new float[] { 228 Float.POSITIVE_INFINITY, Float.MAX_VALUE, 3.4E12f, 2.3f, 229 Float.MIN_NORMAL, Float.MIN_NORMAL / 2, Float.MIN_VALUE, +0.0f, 230 0.0f, -0.0f, -Float.MIN_VALUE, -Float.MIN_NORMAL / 2, 231 -Float.MIN_NORMAL, -4.5f, -5.6442E21f, -Float.MAX_VALUE, 232 Float.NEGATIVE_INFINITY }; 233 234 /** 235 * {@link java.lang.Math#copySign(float, float)} 236 * @since 1.6 237 */ 238 @SuppressWarnings("boxing") test_copySign_FF()239 public void test_copySign_FF() { 240 for (int i = 0; i < COPYSIGN_FF_CASES.length; i++) { 241 final float magnitude = COPYSIGN_FF_CASES[i]; 242 final int absMagnitudeBits = Float.floatToIntBits(Math 243 .abs(magnitude)); 244 final int negMagnitudeBits = Float.floatToIntBits(-Math 245 .abs(magnitude)); 246 247 // cases for NaN 248 assertEquals("If the sign is NaN, the result should be positive.", 249 absMagnitudeBits, Float.floatToIntBits(Math.copySign( 250 magnitude, Float.NaN))); 251 assertTrue("The result should be NaN.", Float.isNaN(Math.copySign( 252 Float.NaN, magnitude))); 253 254 for (int j = 0; j < COPYSIGN_FF_CASES.length; j++) { 255 final float sign = COPYSIGN_FF_CASES[j]; 256 final int resultBits = Float.floatToIntBits(Math.copySign( 257 magnitude, sign)); 258 if (sign > 0 || Float.valueOf(+0.0f).equals(sign) 259 || Float.valueOf(0.0f).equals(sign)) { 260 assertEquals( 261 "If the sign is positive, the result should be positive.", 262 absMagnitudeBits, resultBits); 263 } 264 if (sign < 0 || Float.valueOf(-0.0f).equals(sign)) { 265 assertEquals( 266 "If the sign is negative, the result should be negative.", 267 negMagnitudeBits, resultBits); 268 } 269 } 270 } 271 272 assertTrue("The result should be NaN.", Float.isNaN(Math.copySign( 273 Float.NaN, Float.NaN))); 274 275 try { 276 Math.copySign((Float) null, 2.3f); 277 fail("Should throw NullPointerException"); 278 } catch (NullPointerException e) { 279 // Expected 280 } 281 try { 282 Math.copySign(2.3f, (Float) null); 283 fail("Should throw NullPointerException"); 284 } catch (NullPointerException e) { 285 // Expected 286 } 287 try { 288 Math.copySign((Float) null, (Float) null); 289 fail("Should throw NullPointerException"); 290 } catch (NullPointerException e) { 291 // Expected 292 } 293 } 294 295 /** 296 * java.lang.Math#cos(double) 297 */ test_cosD()298 public void test_cosD() { 299 // Test for method double java.lang.Math.cos(double) 300 assertEquals("Incorrect answer", 1.0, Math.cos(0), 0D); 301 assertEquals("Incorrect answer", 0.5403023058681398, Math.cos(1), 0D); 302 } 303 304 /** 305 * java.lang.Math#cosh(double) 306 */ test_cosh_D()307 public void test_cosh_D() { 308 // Test for special situations 309 assertTrue(Double.isNaN(Math.cosh(Double.NaN))); 310 assertEquals("Should return POSITIVE_INFINITY", 311 Double.POSITIVE_INFINITY, Math.cosh(Double.POSITIVE_INFINITY), 0D); 312 assertEquals("Should return POSITIVE_INFINITY", 313 Double.POSITIVE_INFINITY, Math.cosh(Double.NEGATIVE_INFINITY), 0D); 314 assertEquals("Should return 1.0", 1.0, Math.cosh(+0.0), 0D); 315 assertEquals("Should return 1.0", 1.0, Math.cosh(-0.0), 0D); 316 317 assertEquals("Should return POSITIVE_INFINITY", 318 Double.POSITIVE_INFINITY, Math.cosh(1234.56), 0D); 319 assertEquals("Should return POSITIVE_INFINITY", 320 Double.POSITIVE_INFINITY, Math.cosh(-1234.56), 0D); 321 assertEquals("Should return 1.0000000000005", 1.0000000000005, Math 322 .cosh(0.000001), 0D); 323 assertEquals("Should return 1.0000000000005", 1.0000000000005, Math 324 .cosh(-0.000001), 0D); 325 assertEquals("Should return 5.212214351945598", 5.212214351945598, Math 326 .cosh(2.33482), 0D); 327 328 assertEquals("Should return POSITIVE_INFINITY", 329 Double.POSITIVE_INFINITY, Math.cosh(Double.MAX_VALUE), 0D); 330 assertEquals("Should return 1.0", 1.0, Math.cosh(Double.MIN_VALUE), 0D); 331 } 332 333 /** 334 * java.lang.Math#exp(double) 335 */ test_expD()336 public void test_expD() { 337 // Test for method double java.lang.Math.exp(double) 338 assertTrue("Incorrect answer returned for simple power", Math.abs(Math 339 .exp(4D) 340 - Math.E * Math.E * Math.E * Math.E) < 0.1D); 341 assertTrue("Incorrect answer returned for larger power", Math.log(Math 342 .abs(Math.exp(5.5D)) - 5.5D) < 10.0D); 343 } 344 345 /** 346 * java.lang.Math#expm1(double) 347 */ test_expm1_D()348 public void test_expm1_D() { 349 // Test for special cases 350 assertTrue("Should return NaN", Double.isNaN(Math.expm1(Double.NaN))); 351 assertEquals("Should return POSITIVE_INFINITY", 352 Double.POSITIVE_INFINITY, Math.expm1(Double.POSITIVE_INFINITY), 0D); 353 assertEquals("Should return -1.0", -1.0, Math 354 .expm1(Double.NEGATIVE_INFINITY), 0D); 355 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 356 .expm1(0.0))); 357 assertEquals(Double.doubleToLongBits(+0.0), Double 358 .doubleToLongBits(Math.expm1(+0.0))); 359 assertEquals(Double.doubleToLongBits(-0.0), Double 360 .doubleToLongBits(Math.expm1(-0.0))); 361 362 assertEquals("Should return -9.999950000166666E-6", 363 -9.999950000166666E-6, Math.expm1(-0.00001), 0D); 364 assertEquals("Should return 1.0145103074469635E60", 365 1.0145103074469635E60, Math.expm1(138.16951162), 0D); 366 assertEquals("Should return POSITIVE_INFINITY", 367 Double.POSITIVE_INFINITY, Math 368 .expm1(123456789123456789123456789.4521584223), 0D); 369 assertEquals("Should return POSITIVE_INFINITY", 370 Double.POSITIVE_INFINITY, Math.expm1(Double.MAX_VALUE), 0D); 371 assertEquals("Should return MIN_VALUE", Double.MIN_VALUE, Math 372 .expm1(Double.MIN_VALUE), 0D); 373 } 374 375 /** 376 * java.lang.Math#floor(double) 377 */ test_floorD()378 public void test_floorD() { 379 assertEquals("Incorrect floor for int", 42, Math.floor(42), 0); 380 assertEquals("Incorrect floor for -int", -2, Math.floor(-2), 0); 381 assertEquals("Incorrect floor for zero", 0d, Math.floor(0d), 0); 382 383 assertEquals("Incorrect floor for +double", 78, Math.floor(78.89), 0); 384 assertEquals("Incorrect floor for -double", -79, Math.floor(-78.89), 0); 385 assertEquals("floor large +double", 3.7314645675925406E19, Math.floor(3.7314645675925406E19), 0); 386 assertEquals("floor large -double", -8.173521839218E12, Math.floor(-8.173521839218E12), 0); 387 assertEquals("floor small double", 0.0d, Math.floor(1.11895241315E-102), 0); 388 389 // Compare toString representations here since -0.0 = +0.0, and 390 // NaN != NaN and we need to distinguish 391 assertEquals("Floor failed for NaN", 392 Double.toString(Double.NaN), Double.toString(Math.floor(Double.NaN))); 393 assertEquals("Floor failed for +0.0", 394 Double.toString(+0.0d), Double.toString(Math.floor(+0.0d))); 395 assertEquals("Floor failed for -0.0", 396 Double.toString(-0.0d), Double.toString(Math.floor(-0.0d))); 397 assertEquals("Floor failed for +infinity", 398 Double.toString(Double.POSITIVE_INFINITY), Double.toString(Math.floor(Double.POSITIVE_INFINITY))); 399 assertEquals("Floor failed for -infinity", 400 Double.toString(Double.NEGATIVE_INFINITY), Double.toString(Math.floor(Double.NEGATIVE_INFINITY))); 401 } 402 403 /** 404 * cases for test_getExponent_D in MathTest/StrictMathTest 405 */ 406 static final double GETEXPONENT_D_CASES[] = new double[] { 407 Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 408 Double.MAX_VALUE, -Double.MAX_VALUE, 2.342E231, -2.342E231, 2800.0, 409 -2800.0, 5.323, -5.323, 1.323, -1.323, 0.623, -0.623, 0.323, 410 -0.323, Double.MIN_NORMAL * 24, -Double.MIN_NORMAL * 24, 411 Double.MIN_NORMAL, -Double.MIN_NORMAL, Double.MIN_NORMAL / 2, 412 -Double.MIN_NORMAL / 2, Double.MIN_VALUE, -Double.MIN_VALUE, +0.0, 413 0.0, -0.0, Double.NaN }; 414 415 /** 416 * result for test_getExponent_D in MathTest/StrictMathTest 417 */ 418 static final int GETEXPONENT_D_RESULTS[] = new int[] { 419 Double.MAX_EXPONENT + 1, Double.MAX_EXPONENT + 1, 420 Double.MAX_EXPONENT, Double.MAX_EXPONENT, 768, 768, 11, 11, 2, 2, 421 0, 0, -1, -1, -2, -2, -1018, -1018, Double.MIN_EXPONENT, 422 Double.MIN_EXPONENT, Double.MIN_EXPONENT - 1, 423 Double.MIN_EXPONENT - 1, Double.MIN_EXPONENT - 1, 424 Double.MIN_EXPONENT - 1, Double.MIN_EXPONENT - 1, 425 Double.MIN_EXPONENT - 1, Double.MIN_EXPONENT - 1, 426 Double.MAX_EXPONENT + 1 }; 427 428 /** 429 * {@link java.lang.Math#getExponent(double)} 430 * @since 1.6 431 */ 432 @SuppressWarnings("boxing") test_getExponent_D()433 public void test_getExponent_D() { 434 for (int i = 0; i < GETEXPONENT_D_CASES.length; i++) { 435 final double number = GETEXPONENT_D_CASES[i]; 436 final int result = GETEXPONENT_D_RESULTS[i]; 437 assertEquals("Wrong result of getExponent(double).", result, Math 438 .getExponent(number)); 439 } 440 441 try { 442 Math.getExponent((Double) null); 443 fail("Should throw NullPointerException"); 444 } catch (NullPointerException e) { 445 // Expected 446 } 447 } 448 449 /** 450 * cases for test_getExponent_F in MathTest/StrictMathTest 451 */ 452 static final float GETEXPONENT_F_CASES[] = new float[] { 453 Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.MAX_VALUE, 454 -Float.MAX_VALUE, 3.4256E23f, -3.4256E23f, 2800.0f, -2800.0f, 455 5.323f, -5.323f, 1.323f, -1.323f, 0.623f, -0.623f, 0.323f, -0.323f, 456 Float.MIN_NORMAL * 24, -Float.MIN_NORMAL * 24, Float.MIN_NORMAL, 457 -Float.MIN_NORMAL, Float.MIN_NORMAL / 2, -Float.MIN_NORMAL / 2, 458 Float.MIN_VALUE, -Float.MIN_VALUE, +0.0f, 0.0f, -0.0f, Float.NaN, 1, Float.MIN_NORMAL * 1.5f }; 459 460 /** 461 * result for test_getExponent_F in MathTest/StrictMathTest 462 */ 463 static final int GETEXPONENT_F_RESULTS[] = new int[] { 464 Float.MAX_EXPONENT + 1, Float.MAX_EXPONENT + 1, Float.MAX_EXPONENT, 465 Float.MAX_EXPONENT, 78, 78, 11, 11, 2, 2, 0, 0, -1, -1, -2, -2, 466 -122, -122, Float.MIN_EXPONENT, Float.MIN_EXPONENT, 467 Float.MIN_EXPONENT - 1, Float.MIN_EXPONENT - 1, 468 Float.MIN_EXPONENT - 1, Float.MIN_EXPONENT - 1, 469 Float.MIN_EXPONENT - 1, Float.MIN_EXPONENT - 1, 470 Float.MIN_EXPONENT - 1, Float.MAX_EXPONENT + 1, 0, Float.MIN_EXPONENT }; 471 472 /** 473 * {@link java.lang.Math#getExponent(float)} 474 * @since 1.6 475 */ 476 @SuppressWarnings("boxing") test_getExponent_F()477 public void test_getExponent_F() { 478 for (int i = 0; i < GETEXPONENT_F_CASES.length; i++) { 479 final float number = GETEXPONENT_F_CASES[i]; 480 final int result = GETEXPONENT_F_RESULTS[i]; 481 assertEquals("Wrong result of getExponent(float).", result, Math 482 .getExponent(number)); 483 } 484 try { 485 Math.getExponent((Float) null); 486 fail("Should throw NullPointerException"); 487 } catch (NullPointerException e) { 488 // Expected 489 } 490 } 491 492 /** 493 * java.lang.Math#hypot(double, double) 494 */ test_hypot_DD()495 public void test_hypot_DD() { 496 // Test for special cases 497 assertEquals("Should return POSITIVE_INFINITY", 498 Double.POSITIVE_INFINITY, Math.hypot(Double.POSITIVE_INFINITY, 499 1.0), 0D); 500 assertEquals("Should return POSITIVE_INFINITY", 501 Double.POSITIVE_INFINITY, Math.hypot(Double.NEGATIVE_INFINITY, 502 123.324), 0D); 503 assertEquals("Should return POSITIVE_INFINITY", 504 Double.POSITIVE_INFINITY, Math.hypot(-758.2587, 505 Double.POSITIVE_INFINITY), 0D); 506 assertEquals("Should return POSITIVE_INFINITY", 507 Double.POSITIVE_INFINITY, Math.hypot(5687.21, 508 Double.NEGATIVE_INFINITY), 0D); 509 assertEquals("Should return POSITIVE_INFINITY", 510 Double.POSITIVE_INFINITY, Math.hypot(Double.POSITIVE_INFINITY, 511 Double.NEGATIVE_INFINITY), 0D); 512 assertEquals("Should return POSITIVE_INFINITY", 513 Double.POSITIVE_INFINITY, Math.hypot(Double.NEGATIVE_INFINITY, 514 Double.POSITIVE_INFINITY), 0D); 515 assertTrue("Should be NaN", Double.isNaN(Math.hypot(Double.NaN, 516 2342301.89843))); 517 assertTrue("Should be NaN", Double.isNaN(Math.hypot(-345.2680, 518 Double.NaN))); 519 520 assertEquals("Should return 2396424.905416697", 2396424.905416697, Math 521 .hypot(12322.12, -2396393.2258), 0D); 522 assertEquals("Should return 138.16958070558556", 138.16958070558556, 523 Math.hypot(-138.16951162, 0.13817035864), 0D); 524 assertEquals("Should return 1.7976931348623157E308", 525 1.7976931348623157E308, Math.hypot(Double.MAX_VALUE, 211370.35), 0D); 526 assertEquals("Should return 5413.7185", 5413.7185, Math.hypot( 527 -5413.7185, Double.MIN_VALUE), 0D); 528 } 529 530 /** 531 * java.lang.Math#IEEEremainder(double, double) 532 */ test_IEEEremainderDD()533 public void test_IEEEremainderDD() { 534 // Test for method double java.lang.Math.IEEEremainder(double, double) 535 assertEquals("Incorrect remainder returned", 536 0.0, Math.IEEEremainder(1.0, 1.0), 0D); 537 assertTrue("Incorrect remainder returned", Math.IEEEremainder(1.32, 538 89.765) >= 1.4705063220631647E-2 539 || Math.IEEEremainder(1.32, 89.765) >= 1.4705063220631649E-2); 540 } 541 542 /** 543 * java.lang.Math#log(double) 544 */ test_logD()545 public void test_logD() { 546 // Test for method double java.lang.Math.log(double) 547 for (double d = 10; d >= -10; d -= 0.5) { 548 double answer = Math.log(Math.exp(d)); 549 assertTrue("Answer does not equal expected answer for d = " + d 550 + " answer = " + answer, Math.abs(answer - d) <= Math 551 .abs(d * 0.00000001)); 552 } 553 } 554 555 /** 556 * java.lang.Math#log10(double) 557 */ 558 @SuppressWarnings("boxing") test_log10_D()559 public void test_log10_D() { 560 // Test for special cases 561 assertTrue(Double.isNaN(Math.log10(Double.NaN))); 562 assertTrue(Double.isNaN(Math.log10(-2541.05745687234187532))); 563 assertTrue(Double.isNaN(Math.log10(-0.1))); 564 assertEquals(Double.POSITIVE_INFINITY, Math.log10(Double.POSITIVE_INFINITY)); 565 assertEquals(Double.NEGATIVE_INFINITY, Math.log10(0.0)); 566 assertEquals(Double.NEGATIVE_INFINITY, Math.log10(+0.0)); 567 assertEquals(Double.NEGATIVE_INFINITY, Math.log10(-0.0)); 568 569 assertEquals(3.0, Math.log10(1000.0)); 570 assertEquals(14.0, Math.log10(Math.pow(10, 14))); 571 assertEquals(3.7389561269540406, Math.log10(5482.2158)); 572 assertEquals(14.661551142893833, Math.log10(458723662312872.125782332587)); 573 assertEquals(-0.9083828622192334, Math.log10(0.12348583358871)); 574 assertEquals(308.25471555991675, Math.log10(Double.MAX_VALUE)); 575 assertEquals(-323.3062153431158, Math.log10(Double.MIN_VALUE)); 576 } 577 578 /** 579 * java.lang.Math#log1p(double) 580 */ test_log1p_D()581 public void test_log1p_D() { 582 // Test for special cases 583 assertTrue("Should return NaN", Double.isNaN(Math.log1p(Double.NaN))); 584 assertTrue("Should return NaN", Double.isNaN(Math.log1p(-32.0482175))); 585 assertEquals("Should return POSITIVE_INFINITY", 586 Double.POSITIVE_INFINITY, Math.log1p(Double.POSITIVE_INFINITY), 0D); 587 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 588 .log1p(0.0))); 589 assertEquals(Double.doubleToLongBits(+0.0), Double 590 .doubleToLongBits(Math.log1p(+0.0))); 591 assertEquals(Double.doubleToLongBits(-0.0), Double 592 .doubleToLongBits(Math.log1p(-0.0))); 593 594 assertEquals("Should return -0.2941782295312541", -0.2941782295312541, 595 Math.log1p(-0.254856327), 0D); 596 assertEquals("Should return 7.368050685564151", 7.368050685564151, Math 597 .log1p(1583.542), 0D); 598 assertEquals("Should return 0.4633708685409921", 0.4633708685409921, 599 Math.log1p(0.5894227), 0D); 600 assertEquals("Should return 709.782712893384", 709.782712893384, Math 601 .log1p(Double.MAX_VALUE), 0D); 602 assertEquals("Should return Double.MIN_VALUE", Double.MIN_VALUE, Math 603 .log1p(Double.MIN_VALUE), 0D); 604 } 605 test_maxDD_Math()606 public void test_maxDD_Math() { 607 test_maxDD(true /* use Math */); 608 } 609 test_maxDD_Double()610 public void test_maxDD_Double() { 611 test_maxDD(false /* use Math */); 612 } 613 614 /** 615 * java.lang.Math#max(double, double) 616 */ test_maxDD(boolean useMath)617 private static void test_maxDD(boolean useMath) { 618 // Test for method double java.lang.Math.max(double, double) 619 assertEquals("Incorrect double max value", 1908897.6000089, 620 max(-1908897.6000089, 1908897.6000089, useMath), 0D); 621 assertEquals("Incorrect double max value", 622 1908897.6000089, max(2.0, 1908897.6000089, useMath), 0D); 623 assertEquals("Incorrect double max value", -2.0, max(-2.0, -1908897.6000089, useMath), 0D); 624 625 // Compare toString representations here since -0.0 = +0.0, and 626 // NaN != NaN and we need to distinguish 627 assertEquals("Max failed for NaN", 628 Double.toString(Double.NaN), Double.toString(max(Double.NaN, 42.0d, useMath))); 629 assertEquals("Max failed for NaN", 630 Double.toString(Double.NaN), Double.toString(max(42.0d, Double.NaN, useMath))); 631 assertEquals("Max failed for 0.0", 632 Double.toString(+0.0d), Double.toString(max(+0.0d, -0.0d, useMath))); 633 assertEquals("Max failed for 0.0", 634 Double.toString(+0.0d), Double.toString(max(-0.0d, +0.0d, useMath))); 635 assertEquals("Max failed for -0.0d", 636 Double.toString(-0.0d), Double.toString(max(-0.0d, -0.0d, useMath))); 637 assertEquals("Max failed for 0.0", 638 Double.toString(+0.0d), Double.toString(max(+0.0d, +0.0d, useMath))); 639 } 640 641 /** 642 * java.lang.Math#max(float, float) 643 */ test_maxFF()644 public void test_maxFF() { 645 // Test for method float java.lang.Math.max(float, float) 646 assertTrue("Incorrect float max value", Math.max(-1908897.600f, 647 1908897.600f) == 1908897.600f); 648 assertTrue("Incorrect float max value", 649 Math.max(2.0f, 1908897.600f) == 1908897.600f); 650 assertTrue("Incorrect float max value", 651 Math.max(-2.0f, -1908897.600f) == -2.0f); 652 653 // Compare toString representations here since -0.0 = +0.0, and 654 // NaN != NaN and we need to distinguish 655 assertEquals("Max failed for NaN", 656 Float.toString(Float.NaN), Float.toString(Math.max(Float.NaN, 42.0f))); 657 assertEquals("Max failed for NaN", 658 Float.toString(Float.NaN), Float.toString(Math.max(42.0f, Float.NaN))); 659 assertEquals("Max failed for 0.0", 660 Float.toString(+0.0f), Float.toString(Math.max(+0.0f, -0.0f))); 661 assertEquals("Max failed for 0.0", 662 Float.toString(+0.0f), Float.toString(Math.max(-0.0f, +0.0f))); 663 assertEquals("Max failed for -0.0f", 664 Float.toString(-0.0f), Float.toString(Math.max(-0.0f, -0.0f))); 665 assertEquals("Max failed for 0.0", 666 Float.toString(+0.0f), Float.toString(Math.max(+0.0f, +0.0f))); 667 } 668 669 /** 670 * java.lang.Math#max(int, int) 671 */ test_maxII()672 public void test_maxII() { 673 // Test for method int java.lang.Math.max(int, int) 674 assertEquals("Incorrect int max value", 675 19088976, Math.max(-19088976, 19088976)); 676 assertEquals("Incorrect int max value", 677 19088976, Math.max(20, 19088976)); 678 assertEquals("Incorrect int max value", -20, Math.max(-20, -19088976)); 679 } 680 681 /** 682 * java.lang.Math#max(long, long) 683 */ test_maxJJ()684 public void test_maxJJ() { 685 // Test for method long java.lang.Math.max(long, long) 686 assertEquals("Incorrect long max value", 19088976000089L, Math.max(-19088976000089L, 687 19088976000089L)); 688 assertEquals("Incorrect long max value", 689 19088976000089L, Math.max(20, 19088976000089L)); 690 assertEquals("Incorrect long max value", 691 -20, Math.max(-20, -19088976000089L)); 692 } 693 test_minDD_Math()694 public void test_minDD_Math() { 695 test_minDD(true /* useMath */); 696 } 697 test_minDD_Double()698 public void test_minDD_Double() { 699 test_minDD(false /* useMath */); 700 } 701 702 /** 703 * java.lang.Math#min(double, double) 704 */ test_minDD(boolean useMath)705 private static void test_minDD(boolean useMath) { 706 // Test for method double java.lang.Math.min(double, double) 707 assertEquals("Incorrect double min value", -1908897.6000089, 708 min(-1908897.6000089, 1908897.6000089, useMath), 0D); 709 assertEquals("Incorrect double min value", 710 2.0, min(2.0, 1908897.6000089, useMath), 0D); 711 assertEquals("Incorrect double min value", -1908897.6000089, 712 min(-2.0, -1908897.6000089, useMath), 0D); 713 assertEquals("Incorrect double min value", 1.0d, Math.min(1.0d, 1.0d)); 714 715 // Compare toString representations here since -0.0 = +0.0, and 716 // NaN != NaN and we need to distinguish 717 assertEquals("Min failed for NaN", 718 Double.toString(Double.NaN), Double.toString(min(Double.NaN, 42.0d, useMath))); 719 assertEquals("Min failed for NaN", 720 Double.toString(Double.NaN), Double.toString(min(42.0d, Double.NaN, useMath))); 721 assertEquals("Min failed for -0.0", 722 Double.toString(-0.0d), Double.toString(min(+0.0d, -0.0d, useMath))); 723 assertEquals("Min failed for -0.0", 724 Double.toString(-0.0d), Double.toString(min(-0.0d, +0.0d, useMath))); 725 assertEquals("Min failed for -0.0d", 726 Double.toString(-0.0d), Double.toString(min(-0.0d, -0.0d, useMath))); 727 assertEquals("Min failed for 0.0", 728 Double.toString(+0.0d), Double.toString(min(+0.0d, +0.0d, useMath))); 729 } 730 min(double a, double b, boolean useMath)731 private static double min(double a, double b, boolean useMath) { 732 if (useMath) { 733 return Math.min(a, b); 734 } else { 735 return Double.min(a, b); 736 } 737 } 738 max(double a, double b, boolean useMath)739 private static double max(double a, double b, boolean useMath) { 740 if (useMath) { 741 return Math.max(a, b); 742 } else { 743 return Double.max(a, b); 744 } 745 } 746 747 /** 748 * java.lang.Math#min(float, float) 749 */ test_minFF()750 public void test_minFF() { 751 // Test for method float java.lang.Math.min(float, float) 752 assertTrue("Incorrect float min value", Math.min(-1908897.600f, 753 1908897.600f) == -1908897.600f); 754 assertTrue("Incorrect float min value", 755 Math.min(2.0f, 1908897.600f) == 2.0f); 756 assertTrue("Incorrect float min value", 757 Math.min(-2.0f, -1908897.600f) == -1908897.600f); 758 assertEquals("Incorrect float min value", 1.0f, Math.min(1.0f, 1.0f)); 759 760 // Compare toString representations here since -0.0 = +0.0, and 761 // NaN != NaN and we need to distinguish 762 assertEquals("Min failed for NaN", 763 Float.toString(Float.NaN), Float.toString(Math.min(Float.NaN, 42.0f))); 764 assertEquals("Min failed for NaN", 765 Float.toString(Float.NaN), Float.toString(Math.min(42.0f, Float.NaN))); 766 assertEquals("Min failed for -0.0", 767 Float.toString(-0.0f), Float.toString(Math.min(+0.0f, -0.0f))); 768 assertEquals("Min failed for -0.0", 769 Float.toString(-0.0f), Float.toString(Math.min(-0.0f, +0.0f))); 770 assertEquals("Min failed for -0.0f", 771 Float.toString(-0.0f), Float.toString(Math.min(-0.0f, -0.0f))); 772 assertEquals("Min failed for 0.0", 773 Float.toString(+0.0f), Float.toString(Math.min(+0.0f, +0.0f))); 774 } 775 776 /** 777 * java.lang.Math#min(int, int) 778 */ test_minII()779 public void test_minII() { 780 // Test for method int java.lang.Math.min(int, int) 781 assertEquals("Incorrect int min value", 782 -19088976, Math.min(-19088976, 19088976)); 783 assertEquals("Incorrect int min value", 20, Math.min(20, 19088976)); 784 assertEquals("Incorrect int min value", 785 -19088976, Math.min(-20, -19088976)); 786 787 } 788 789 /** 790 * java.lang.Math#min(long, long) 791 */ test_minJJ()792 public void test_minJJ() { 793 // Test for method long java.lang.Math.min(long, long) 794 assertEquals("Incorrect long min value", -19088976000089L, Math.min(-19088976000089L, 795 19088976000089L)); 796 assertEquals("Incorrect long min value", 797 20, Math.min(20, 19088976000089L)); 798 assertEquals("Incorrect long min value", 799 -19088976000089L, Math.min(-20, -19088976000089L)); 800 } 801 802 /** 803 * start number cases for test_nextAfter_DD in MathTest/StrictMathTest 804 * NEXTAFTER_DD_START_CASES[i][0] is the start number 805 * NEXTAFTER_DD_START_CASES[i][1] is the nextUp of start number 806 * NEXTAFTER_DD_START_CASES[i][2] is the nextDown of start number 807 */ 808 static final double NEXTAFTER_DD_START_CASES[][] = new double[][] { 809 { 3.4, 3.4000000000000004, 3.3999999999999995 }, 810 { -3.4, -3.3999999999999995, -3.4000000000000004 }, 811 { 3.4233E109, 3.4233000000000005E109, 3.4232999999999996E109 }, 812 { -3.4233E109, -3.4232999999999996E109, -3.4233000000000005E109 }, 813 { +0.0, Double.MIN_VALUE, -Double.MIN_VALUE }, 814 { 0.0, Double.MIN_VALUE, -Double.MIN_VALUE }, 815 { -0.0, Double.MIN_VALUE, -Double.MIN_VALUE }, 816 { Double.MIN_VALUE, 1.0E-323, +0.0 }, 817 { -Double.MIN_VALUE, -0.0, -1.0E-323 }, 818 { Double.MIN_NORMAL, 2.225073858507202E-308, 2.225073858507201E-308 }, 819 { -Double.MIN_NORMAL, -2.225073858507201E-308, 820 -2.225073858507202E-308 }, 821 { Double.MAX_VALUE, Double.POSITIVE_INFINITY, 822 1.7976931348623155E308 }, 823 { -Double.MAX_VALUE, -1.7976931348623155E308, 824 Double.NEGATIVE_INFINITY }, 825 { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 826 Double.MAX_VALUE }, 827 { Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, 828 Double.NEGATIVE_INFINITY } }; 829 830 /** 831 * direction number cases for test_nextAfter_DD/test_nextAfter_FD in 832 * MathTest/StrictMathTest 833 */ 834 static final double NEXTAFTER_DD_FD_DIRECTION_CASES[] = new double[] { 835 Double.POSITIVE_INFINITY, Double.MAX_VALUE, 8.8, 3.4, 1.4, 836 Double.MIN_NORMAL, Double.MIN_NORMAL / 2, Double.MIN_VALUE, +0.0, 837 0.0, -0.0, -Double.MIN_VALUE, -Double.MIN_NORMAL / 2, 838 -Double.MIN_NORMAL, -1.4, -3.4, -8.8, -Double.MAX_VALUE, 839 Double.NEGATIVE_INFINITY }; 840 841 /** 842 * {@link java.lang.Math#nextAfter(double, double)} 843 * @since 1.6 844 */ 845 @SuppressWarnings("boxing") test_nextAfter_DD()846 public void test_nextAfter_DD() { 847 // test for most cases without exception 848 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 849 final double start = NEXTAFTER_DD_START_CASES[i][0]; 850 final long nextUpBits = Double 851 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][1]); 852 final long nextDownBits = Double 853 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][2]); 854 855 for (int j = 0; j < NEXTAFTER_DD_FD_DIRECTION_CASES.length; j++) { 856 final double direction = NEXTAFTER_DD_FD_DIRECTION_CASES[j]; 857 final long resultBits = Double.doubleToLongBits(Math.nextAfter( 858 start, direction)); 859 final long directionBits = Double.doubleToLongBits(direction); 860 if (direction > start) { 861 assertEquals("Result should be next up-number.", 862 nextUpBits, resultBits); 863 } else if (direction < start) { 864 assertEquals("Result should be next down-number.", 865 nextDownBits, resultBits); 866 } else { 867 assertEquals("Result should be direction.", directionBits, 868 resultBits); 869 } 870 } 871 } 872 873 // test for cases with NaN 874 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 875 assertTrue("The result should be NaN.", Double.isNaN(Math 876 .nextAfter(NEXTAFTER_DD_START_CASES[i][0], Double.NaN))); 877 } 878 for (int i = 0; i < NEXTAFTER_DD_FD_DIRECTION_CASES.length; i++) { 879 assertTrue("The result should be NaN.", Double.isNaN(Math 880 .nextAfter(Double.NaN, NEXTAFTER_DD_FD_DIRECTION_CASES[i]))); 881 } 882 assertTrue("The result should be NaN.", Double.isNaN(Math.nextAfter( 883 Double.NaN, Double.NaN))); 884 885 // test for exception 886 try { 887 Math.nextAfter((Double) null, 2.3); 888 fail("Should throw NullPointerException"); 889 } catch (NullPointerException e) { 890 // Expected 891 } 892 try { 893 Math.nextAfter(2.3, (Double) null); 894 fail("Should throw NullPointerException"); 895 } catch (NullPointerException e) { 896 // Expected 897 } 898 try { 899 Math.nextAfter((Double) null, (Double) null); 900 fail("Should throw NullPointerException"); 901 } catch (NullPointerException e) { 902 // Expected 903 } 904 } 905 906 /** 907 * start number cases for test_nextAfter_FD in MathTest/StrictMathTest 908 * NEXTAFTER_FD_START_CASES[i][0] is the start number 909 * NEXTAFTER_FD_START_CASES[i][1] is the nextUp of start number 910 * NEXTAFTER_FD_START_CASES[i][2] is the nextDown of start number 911 */ 912 static final float NEXTAFTER_FD_START_CASES[][] = new float[][] { 913 { 3.4f, 3.4000003f, 3.3999999f }, 914 { -3.4f, -3.3999999f, -3.4000003f }, 915 { 3.4233E19f, 3.4233002E19f, 3.4232998E19f }, 916 { -3.4233E19f, -3.4232998E19f, -3.4233002E19f }, 917 { +0.0f, Float.MIN_VALUE, -Float.MIN_VALUE }, 918 { 0.0f, Float.MIN_VALUE, -Float.MIN_VALUE }, 919 { -0.0f, Float.MIN_VALUE, -Float.MIN_VALUE }, 920 { Float.MIN_VALUE, 2.8E-45f, +0.0f }, 921 { -Float.MIN_VALUE, -0.0f, -2.8E-45f }, 922 { Float.MIN_NORMAL, 1.1754945E-38f, 1.1754942E-38f }, 923 { -Float.MIN_NORMAL, -1.1754942E-38f, -1.1754945E-38f }, 924 { Float.MAX_VALUE, Float.POSITIVE_INFINITY, 3.4028233E38f }, 925 { -Float.MAX_VALUE, -3.4028233E38f, Float.NEGATIVE_INFINITY }, 926 { Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.MAX_VALUE }, 927 { Float.NEGATIVE_INFINITY, -Float.MAX_VALUE, 928 Float.NEGATIVE_INFINITY } }; 929 930 /** 931 * {@link java.lang.Math#nextAfter(float, double)} 932 * @since 1.6 933 */ 934 @SuppressWarnings("boxing") test_nextAfter_FD()935 public void test_nextAfter_FD() { 936 // test for most cases without exception 937 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 938 final float start = NEXTAFTER_FD_START_CASES[i][0]; 939 final int nextUpBits = Float 940 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][1]); 941 final int nextDownBits = Float 942 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][2]); 943 944 for (int j = 0; j < NEXTAFTER_DD_FD_DIRECTION_CASES.length; j++) { 945 final double direction = NEXTAFTER_DD_FD_DIRECTION_CASES[j]; 946 final int resultBits = Float.floatToIntBits(Math.nextAfter( 947 start, direction)); 948 if (direction > start) { 949 assertEquals("Result should be next up-number.", 950 nextUpBits, resultBits); 951 } else if (direction < start) { 952 assertEquals("Result should be next down-number.", 953 nextDownBits, resultBits); 954 } else { 955 final int equivalentBits = Float.floatToIntBits(new Float( 956 direction)); 957 assertEquals( 958 "Result should be a number equivalent to direction.", 959 equivalentBits, resultBits); 960 } 961 } 962 } 963 964 // test for cases with NaN 965 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 966 assertTrue("The result should be NaN.", Float.isNaN(Math.nextAfter( 967 NEXTAFTER_FD_START_CASES[i][0], Float.NaN))); 968 } 969 for (int i = 0; i < NEXTAFTER_DD_FD_DIRECTION_CASES.length; i++) { 970 assertTrue("The result should be NaN.", Float.isNaN(Math.nextAfter( 971 Float.NaN, NEXTAFTER_DD_FD_DIRECTION_CASES[i]))); 972 } 973 assertTrue("The result should be NaN.", Float.isNaN(Math.nextAfter( 974 Float.NaN, Float.NaN))); 975 976 // test for exception 977 try { 978 Math.nextAfter((Float) null, 2.3); 979 fail("Should throw NullPointerException"); 980 } catch (NullPointerException e) { 981 // Expected 982 } 983 try { 984 Math.nextAfter(2.3, (Float) null); 985 fail("Should throw NullPointerException"); 986 } catch (NullPointerException e) { 987 // Expected 988 } 989 try { 990 Math.nextAfter((Float) null, (Float) null); 991 fail("Should throw NullPointerException"); 992 } catch (NullPointerException e) { 993 // Expected 994 } 995 } 996 997 /** 998 * {@link java.lang.Math#nextUp(double)} 999 * @since 1.6 1000 */ 1001 @SuppressWarnings("boxing") test_nextUp_D()1002 public void test_nextUp_D() { 1003 // This method is semantically equivalent to nextAfter(d, 1004 // Double.POSITIVE_INFINITY), 1005 // so we use the data of test_nextAfter_DD 1006 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 1007 final double start = NEXTAFTER_DD_START_CASES[i][0]; 1008 final long nextUpBits = Double 1009 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][1]); 1010 final long resultBits = Double.doubleToLongBits(Math.nextUp(start)); 1011 assertEquals("Result should be next up-number.", nextUpBits, 1012 resultBits); 1013 } 1014 1015 // test for cases with NaN 1016 assertTrue("The result should be NaN.", Double.isNaN(Math 1017 .nextUp(Double.NaN))); 1018 1019 // test for exception 1020 try { 1021 Math.nextUp((Double) null); 1022 fail("Should throw NullPointerException"); 1023 } catch (NullPointerException e) { 1024 // Expected 1025 } 1026 } 1027 1028 /** 1029 * {@link java.lang.Math#nextUp(float)} 1030 * @since 1.6 1031 */ 1032 @SuppressWarnings("boxing") test_nextUp_F()1033 public void test_nextUp_F() { 1034 // This method is semantically equivalent to nextAfter(f, 1035 // Float.POSITIVE_INFINITY), 1036 // so we use the data of test_nextAfter_FD 1037 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 1038 final float start = NEXTAFTER_FD_START_CASES[i][0]; 1039 final int nextUpBits = Float 1040 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][1]); 1041 final int resultBits = Float.floatToIntBits(Math.nextUp(start)); 1042 assertEquals("Result should be next up-number.", nextUpBits, 1043 resultBits); 1044 } 1045 1046 // test for cases with NaN 1047 assertTrue("The result should be NaN.", Float.isNaN(Math 1048 .nextUp(Float.NaN))); 1049 1050 // test for exception 1051 try { 1052 Math.nextUp((Float) null); 1053 fail("Should throw NullPointerException"); 1054 } catch (NullPointerException e) { 1055 // Expected 1056 } 1057 } 1058 1059 /** 1060 * {@link java.lang.Math#nextDown(double)} 1061 * @since 1.8 1062 */ 1063 @SuppressWarnings("boxing") test_nextDown_D()1064 public void test_nextDown_D() { 1065 // This method is semantically equivalent to nextAfter(d, 1066 // Double.NEGATIVE_INFINITY), 1067 // so we use the data of test_nextAfter_DD 1068 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 1069 final double start = NEXTAFTER_DD_START_CASES[i][0]; 1070 final long nextDownBits = Double 1071 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][2]); 1072 final long resultBits = Double.doubleToLongBits(Math.nextDown(start)); 1073 assertEquals("Result should be next down-number.", nextDownBits, 1074 resultBits); 1075 } 1076 1077 // test for cases with NaN 1078 assertTrue("The result should be NaN.", Double.isNaN(Math 1079 .nextDown(Double.NaN))); 1080 1081 // test for exception 1082 try { 1083 Math.nextDown((Double) null); 1084 fail("Should throw NullPointerException"); 1085 } catch (NullPointerException e) { 1086 // Expected 1087 } 1088 } 1089 1090 /** 1091 * {@link java.lang.Math#nextDown(float)} 1092 * @since 1.8 1093 */ 1094 @SuppressWarnings("boxing") test_nextDown_F()1095 public void test_nextDown_F() { 1096 // This method is semantically equivalent to nextAfter(f, 1097 // Float.NEGATIVE_INFINITY), 1098 // so we use the data of test_nextAfter_FD 1099 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 1100 final float start = NEXTAFTER_FD_START_CASES[i][0]; 1101 final int nextDownBits = Float 1102 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][2]); 1103 final int resultBits = Float.floatToIntBits(Math.nextDown(start)); 1104 assertEquals("Result should be next down-number.", nextDownBits, 1105 resultBits); 1106 } 1107 1108 // test for cases with NaN 1109 assertTrue("The result should be NaN.", Float.isNaN(Math 1110 .nextDown(Float.NaN))); 1111 1112 // test for exception 1113 try { 1114 Math.nextDown((Float) null); 1115 fail("Should throw NullPointerException"); 1116 } catch (NullPointerException e) { 1117 // Expected 1118 } 1119 } 1120 1121 /** 1122 * java.lang.Math#pow(double, double) 1123 */ test_powDD()1124 public void test_powDD() { 1125 // Test for method double java.lang.Math.pow(double, double) 1126 double NZERO = longTodouble(doubleTolong(0.0) ^ 0x8000000000000000L); 1127 double p1 = 1.0; 1128 double p2 = 2.0; 1129 double p3 = 3.0; 1130 double p4 = 4.0; 1131 double p5 = 5.0; 1132 double p6 = 6.0; 1133 double p7 = 7.0; 1134 double p8 = 8.0; 1135 double p9 = 9.0; 1136 double p10 = 10.0; 1137 double p11 = 11.0; 1138 double p12 = 12.0; 1139 double p13 = 13.0; 1140 double p14 = 14.0; 1141 double p15 = 15.0; 1142 double p16 = 16.0; 1143 double[] values = { p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, 1144 p13, p14, p15, p16 }; 1145 1146 for (int x = 0; x < values.length; x++) { 1147 double dval = values[x]; 1148 double negateDval = negateDouble(dval); 1149 1150 // If the second argument is positive or negative zero, then the 1151 // result is 1.0. 1152 assertEquals("Result should be Math.pow(" + dval 1153 + ",-0.0)=+1.0", 1.0, Math.pow(dval, NZERO)); 1154 assertEquals("Result should be Math.pow(" + negateDval 1155 + ",-0.0)=+1.0", 1.0, Math.pow(negateDval, NZERO)); 1156 assertEquals("Result should be Math.pow(" + dval 1157 + ",+0.0)=+1.0", 1.0, Math.pow(dval, +0.0)); 1158 assertEquals("Result should be Math.pow(" + negateDval 1159 + ",+0.0)=+1.0", 1.0, Math.pow(negateDval, +0.0)); 1160 1161 // If the second argument is 1.0, then the result is the same as the 1162 // first argument. 1163 assertEquals("Result should be Math.pow(" + dval + "," + 1.0 + ")=" 1164 + dval, dval, Math.pow(dval, 1.0)); 1165 assertEquals("Result should be Math.pow(" + negateDval + "," + 1.0 1166 + ")=" + negateDval, negateDval, Math.pow(negateDval, 1.0)); 1167 1168 // The libm implementation of pow() has the following set of special case behaviors: 1169 // If the first argument is 1.0, then the result is 1.0. 1170 // i.e. the answer for the following case is not Double.NaN. http://b/11669804 1171 { 1172 double answer = dval == 1.0d ? 1.0d : Double.NaN; 1173 assertEquals("Result should be Math.pow(" + dval + "," + Double.NaN 1174 + ")=" + answer, answer, Math.pow(dval, Double.NaN)); 1175 } 1176 1177 // If the second argument is NaN, then the result is NaN. 1178 assertEquals("Result should be Math.pow(" + negateDval + "," 1179 + Double.NaN + ")=" + Double.NaN, Double.NaN, Math.pow(negateDval, 1180 Double.NaN)); 1181 1182 if (dval > 1) { 1183 // If the first argument is NaN and the second argument is 1184 // nonzero, 1185 // then the result is NaN. 1186 assertEquals("Result should be Math.pow(" + Double.NaN + "," 1187 + dval + ")=" + Double.NaN, Double.NaN, Math.pow(Double.NaN, dval)); 1188 assertEquals("Result should be Math.pow(" + Double.NaN + "," 1189 + negateDval + ")=" + Double.NaN, Double.NaN, Math.pow(Double.NaN, 1190 negateDval)); 1191 1192 /* 1193 * If the first argument is positive zero and the second 1194 * argument is greater than zero, or the first argument is 1195 * positive infinity and the second argument is less than zero, 1196 * then the result is positive zero. 1197 */ 1198 assertEquals("Result should be Math.pow(" + 0.0 + "," + dval 1199 + ")=" + 0.0, +0.0, Math.pow(0.0, dval)); 1200 assertEquals("Result should be Math.pow(" 1201 + Double.POSITIVE_INFINITY + "," + negateDval + ")=" 1202 + 0.0, +0.0, Math.pow(Double.POSITIVE_INFINITY, negateDval)); 1203 1204 /* 1205 * If the first argument is positive zero and the second 1206 * argument is less than zero, or the first argument is positive 1207 * infinity and the second argument is greater than zero, then 1208 * the result is positive infinity. 1209 */ 1210 assertEquals("Result should be Math.pow(" + 0.0 + "," 1211 + negateDval + ")=" + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1212 Math.pow(0.0, negateDval)); 1213 assertEquals("Result should be Math.pow(" 1214 + Double.POSITIVE_INFINITY + "," + dval + ")=" 1215 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow( 1216 Double.POSITIVE_INFINITY, dval)); 1217 1218 // Not a finite odd integer 1219 if (dval % 2 == 0) { 1220 /* 1221 * If the first argument is negative zero and the second 1222 * argument is greater than zero but not a finite odd 1223 * integer, or the first argument is negative infinity and 1224 * the second argument is less than zero but not a finite 1225 * odd integer, then the result is positive zero. 1226 */ 1227 assertEquals("Result should be Math.pow(" + NZERO + "," 1228 + dval + ")=" + 0.0, +0.0, Math.pow(NZERO, dval)); 1229 assertEquals("Result should be Math.pow(" 1230 + Double.NEGATIVE_INFINITY + "," + negateDval 1231 + ")=" + 0.0, +0.0, Math.pow(Double.NEGATIVE_INFINITY, 1232 negateDval)); 1233 1234 /* 1235 * If the first argument is negative zero and the second 1236 * argument is less than zero but not a finite odd integer, 1237 * or the first argument is negative infinity and the second 1238 * argument is greater than zero but not a finite odd 1239 * integer, then the result is positive infinity. 1240 */ 1241 assertEquals("Result should be Math.pow(" + NZERO + "," 1242 + negateDval + ")=" + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1243 Math.pow(NZERO, negateDval)); 1244 assertEquals("Result should be Math.pow(" 1245 + Double.NEGATIVE_INFINITY + "," + dval + ")=" 1246 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow( 1247 Double.NEGATIVE_INFINITY, dval)); 1248 } 1249 1250 // finite odd integer 1251 if (dval % 2 != 0) { 1252 /* 1253 * If the first argument is negative zero and the second 1254 * argument is a positive finite odd integer, or the first 1255 * argument is negative infinity and the second argument is 1256 * a negative finite odd integer, then the result is 1257 * negative zero. 1258 */ 1259 assertEquals("Result should be Math.pow(" + NZERO + "," 1260 + dval + ")=" + NZERO, NZERO, Math.pow(NZERO, dval)); 1261 assertEquals("Result should be Math.pow(" 1262 + Double.NEGATIVE_INFINITY + "," + negateDval 1263 + ")=" + NZERO, NZERO, Math.pow(Double.NEGATIVE_INFINITY, 1264 negateDval)); 1265 /* 1266 * If the first argument is negative zero and the second 1267 * argument is a negative finite odd integer, or the first 1268 * argument is negative infinity and the second argument is 1269 * a positive finite odd integer then the result is negative 1270 * infinity. 1271 */ 1272 assertEquals("Result should be Math.pow(" + NZERO + "," 1273 + negateDval + ")=" + Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1274 Math.pow(NZERO, negateDval)); 1275 assertEquals("Result should be Math.pow(" 1276 + Double.NEGATIVE_INFINITY + "," + dval + ")=" 1277 + Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Math.pow( 1278 Double.NEGATIVE_INFINITY, dval)); 1279 } 1280 1281 /** 1282 * 1. If the first argument is finite and less than zero if the 1283 * second argument is a finite even integer, the result is equal 1284 * to the result of raising the absolute value of the first 1285 * argument to the power of the second argument 1286 * 1287 * 2. if the second argument is a finite odd integer, the result is equal to the 1288 * negative of the result of raising the absolute value of the 1289 * first argument to the power of the second argument 1290 * 1291 * 3. if the second argument is finite and not an integer, then the result 1292 * is NaN. 1293 */ 1294 for (int j = 1; j < values.length; j++) { 1295 double jval = values[j]; 1296 if (jval % 2.0 == 0.0) { 1297 assertEquals("" + negateDval + " " + jval, Math.pow( 1298 dval, jval), Math.pow(negateDval, jval)); 1299 } else { 1300 assertEquals("" + negateDval + " " + jval, -1.0 1301 * Math.pow(dval, jval), Math.pow(negateDval, 1302 jval)); 1303 } 1304 assertEquals(Double.NaN, Math 1305 .pow(negateDval, jval / 0.5467)); 1306 assertEquals(Double.NaN, Math.pow(negateDval, -1.0 * jval 1307 / 0.5467)); 1308 } 1309 } 1310 1311 if (dval == 1) { 1312 // The libm implementation of pow() has the following set of special case behaviors: 1313 // 1.0 or -1.0 to the power of positive or negative infinity is 1.0. 1314 // i.e. the answer for the following cases is not Double.NaN. http://b/11669804 1315 1316 assertEquals("Result should be Math.pow(" + dval + "," 1317 + Double.POSITIVE_INFINITY + ")=" + 1.0d, 1.0d, Math 1318 .pow(dval, Double.POSITIVE_INFINITY)); 1319 assertEquals("Result should be Math.pow(" + dval + "," 1320 + Double.NEGATIVE_INFINITY + ")=" + 1.0d, 1.0d, Math 1321 .pow(dval, Double.NEGATIVE_INFINITY)); 1322 1323 assertEquals("Result should be Math.pow(" + negateDval + "," 1324 + Double.POSITIVE_INFINITY + ")=" + 1.0d, 1.0d, Math 1325 .pow(negateDval, Double.POSITIVE_INFINITY)); 1326 assertEquals("Result should be Math.pow(" + negateDval + "," 1327 + Double.NEGATIVE_INFINITY + ")=" + 1.0d, 1.0d, Math 1328 .pow(negateDval, Double.NEGATIVE_INFINITY)); 1329 } 1330 1331 if (dval > 1) { 1332 /* 1333 * If the absolute value of the first argument is greater than 1 1334 * and the second argument is positive infinity, or the absolute 1335 * value of the first argument is less than 1 and the second 1336 * argument is negative infinity, then the result is positive 1337 * infinity. 1338 */ 1339 assertEquals("Result should be Math.pow(" + dval + "," 1340 + Double.POSITIVE_INFINITY + ")=" 1341 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow(dval, 1342 Double.POSITIVE_INFINITY)); 1343 1344 assertEquals("Result should be Math.pow(" + negateDval + "," 1345 + Double.NEGATIVE_INFINITY + ")=" 1346 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow(-0.13456, 1347 Double.NEGATIVE_INFINITY)); 1348 1349 /* 1350 * If the absolute value of the first argument is greater than 1 1351 * and the second argument is negative infinity, or the absolute 1352 * value of the first argument is less than 1 and the second 1353 * argument is positive infinity, then the result is positive 1354 * zero. 1355 */ 1356 assertEquals("Result should be Math.pow(" + dval + "," 1357 + Double.NEGATIVE_INFINITY + ")= +0.0", +0.0, Math.pow(dval, 1358 Double.NEGATIVE_INFINITY)); 1359 assertEquals("Result should be Math.pow(" + negateDval + "," 1360 + Double.POSITIVE_INFINITY + ")= +0.0", +0.0, Math.pow( 1361 -0.13456, Double.POSITIVE_INFINITY)); 1362 } 1363 1364 assertEquals("Result should be Math.pow(" + 0.0 + "," + dval + ")=" 1365 + 0.0, 0.0, Math.pow(0.0, dval)); 1366 assertEquals("Result should be Math.pow(" + Double.NaN + "," + dval 1367 + ")=" + Double.NaN, Double.NaN, Math.pow(Double.NaN, dval)); 1368 } 1369 assertTrue("pow returned incorrect value", 1370 (long) Math.pow(2, 8) == 256l); 1371 assertTrue("pow returned incorrect value", 1372 Math.pow(2, -8) == 0.00390625d); 1373 assertEquals("Incorrect root returned1", 1374 2, Math.sqrt(Math.pow(Math.sqrt(2), 4)), 0); 1375 1376 assertEquals(Double.NEGATIVE_INFINITY, Math.pow(-10.0, 3.093403029238847E15)); 1377 assertEquals(Double.POSITIVE_INFINITY, Math.pow(10.0, 3.093403029238847E15)); 1378 } 1379 longTodouble(long longvalue)1380 private double longTodouble(long longvalue) { 1381 return Double.longBitsToDouble(longvalue); 1382 } 1383 doubleTolong(double doublevalue)1384 private long doubleTolong(double doublevalue) { 1385 return Double.doubleToLongBits(doublevalue); 1386 } 1387 negateDouble(double doublevalue)1388 private double negateDouble(double doublevalue) { 1389 return doublevalue * -1.0; 1390 } 1391 1392 /** 1393 * java.lang.Math#rint(double) 1394 */ test_rintD()1395 public void test_rintD() { 1396 // Test for method double java.lang.Math.rint(double) 1397 assertEquals("Failed to round properly - up to odd", 1398 3.0, Math.rint(2.9), 0D); 1399 assertTrue("Failed to round properly - NaN", Double.isNaN(Math 1400 .rint(Double.NaN))); 1401 assertEquals("Failed to round properly down to even", 1402 2.0, Math.rint(2.1), 0D); 1403 assertTrue("Failed to round properly " + 2.5 + " to even", Math 1404 .rint(2.5) == 2.0); 1405 assertTrue("Failed to round properly " + (+0.0d), 1406 Math.rint(+0.0d) == +0.0d); 1407 assertTrue("Failed to round properly " + (-0.0d), 1408 Math.rint(-0.0d) == -0.0d); 1409 } 1410 1411 /** 1412 * java.lang.Math#round(double) 1413 */ test_roundD()1414 public void test_roundD() { 1415 // Test for method long java.lang.Math.round(double) 1416 assertEquals("Incorrect rounding of a float", -91, Math.round(-90.89d)); 1417 } 1418 1419 /** 1420 * java.lang.Math#round(float) 1421 */ test_roundF()1422 public void test_roundF() { 1423 // Test for method int java.lang.Math.round(float) 1424 assertEquals("Incorrect rounding of a float", -91, Math.round(-90.89f)); 1425 } 1426 1427 /** 1428 * {@link java.lang.Math#scalb(double, int)} 1429 * @since 1.6 1430 */ 1431 @SuppressWarnings("boxing") test_scalb_DI()1432 public void test_scalb_DI() { 1433 // result is normal 1434 assertEquals(4.1422946304E7, Math.scalb(1.2345, 25)); 1435 assertEquals(3.679096698760986E-8, Math.scalb(1.2345, -25)); 1436 assertEquals(1.2345, Math.scalb(1.2345, 0)); 1437 assertEquals(7868514.304, Math.scalb(0.2345, 25)); 1438 1439 double normal = Math.scalb(0.2345, -25); 1440 assertEquals(6.98864459991455E-9, normal); 1441 // precision kept 1442 assertEquals(0.2345, Math.scalb(normal, 25)); 1443 1444 assertEquals(0.2345, Math.scalb(0.2345, 0)); 1445 assertEquals(-4.1422946304E7, Math.scalb(-1.2345, 25)); 1446 assertEquals(-6.98864459991455E-9, Math.scalb(-0.2345, -25)); 1447 assertEquals(2.0, Math.scalb(Double.MIN_NORMAL / 2, 1024)); 1448 assertEquals(64.0, Math.scalb(Double.MIN_VALUE, 1080)); 1449 assertEquals(234, Math.getExponent(Math.scalb(1.0, 234))); 1450 assertEquals(3.9999999999999996, Math.scalb(Double.MAX_VALUE, 1451 Double.MIN_EXPONENT)); 1452 1453 // result is near infinity 1454 double halfMax = Math.scalb(1.0, Double.MAX_EXPONENT); 1455 assertEquals(8.98846567431158E307, halfMax); 1456 assertEquals(Double.MAX_VALUE, halfMax - Math.ulp(halfMax) + halfMax); 1457 assertEquals(Double.POSITIVE_INFINITY, halfMax + halfMax); 1458 assertEquals(1.7976931348623155E308, Math.scalb(1.0 - Math.ulp(1.0), 1459 Double.MAX_EXPONENT + 1)); 1460 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(1.0 - Math.ulp(1.0), 1461 Double.MAX_EXPONENT + 2)); 1462 1463 halfMax = Math.scalb(-1.0, Double.MAX_EXPONENT); 1464 assertEquals(-8.98846567431158E307, halfMax); 1465 assertEquals(-Double.MAX_VALUE, halfMax + Math.ulp(halfMax) + halfMax); 1466 assertEquals(Double.NEGATIVE_INFINITY, halfMax + halfMax); 1467 1468 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(0.345, 1234)); 1469 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(44.345E102, 934)); 1470 assertEquals(Double.NEGATIVE_INFINITY, Math.scalb(-44.345E102, 934)); 1471 1472 assertEquals(Double.POSITIVE_INFINITY, Math.scalb( 1473 Double.MIN_NORMAL / 2, 4000)); 1474 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(Double.MIN_VALUE, 1475 8000)); 1476 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(Double.MAX_VALUE, 1)); 1477 assertEquals(Double.POSITIVE_INFINITY, Math.scalb( 1478 Double.POSITIVE_INFINITY, 0)); 1479 assertEquals(Double.POSITIVE_INFINITY, Math.scalb( 1480 Double.POSITIVE_INFINITY, -1)); 1481 assertEquals(Double.NEGATIVE_INFINITY, Math.scalb( 1482 Double.NEGATIVE_INFINITY, -1)); 1483 assertEquals(Double.NEGATIVE_INFINITY, Math.scalb( 1484 Double.NEGATIVE_INFINITY, Double.MIN_EXPONENT)); 1485 1486 // result is subnormal/zero 1487 long posZeroBits = Double.doubleToLongBits(+0.0); 1488 long negZeroBits = Double.doubleToLongBits(-0.0); 1489 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb(+0.0, 1490 Integer.MAX_VALUE))); 1491 assertEquals(posZeroBits, Double.doubleToLongBits(Math 1492 .scalb(+0.0, -123))); 1493 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb(+0.0, 0))); 1494 assertEquals(negZeroBits, Double 1495 .doubleToLongBits(Math.scalb(-0.0, 123))); 1496 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb(-0.0, 1497 Integer.MIN_VALUE))); 1498 1499 assertEquals(Double.MIN_VALUE, Math.scalb(1.0, -1074)); 1500 assertEquals(posZeroBits, Double.doubleToLongBits(Math 1501 .scalb(1.0, -1075))); 1502 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb(-1.0, 1503 -1075))); 1504 1505 // precision lost 1506 assertEquals(Math.scalb(21.405, -1078), Math.scalb(21.405, -1079)); 1507 assertEquals(Double.MIN_VALUE, Math.scalb(21.405, -1079)); 1508 assertEquals(-Double.MIN_VALUE, Math.scalb(-21.405, -1079)); 1509 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb(21.405, 1510 -1080))); 1511 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb(-21.405, 1512 -1080))); 1513 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1514 Double.MIN_VALUE, -1))); 1515 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1516 -Double.MIN_VALUE, -1))); 1517 assertEquals(Double.MIN_VALUE, Math.scalb(Double.MIN_NORMAL, -52)); 1518 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1519 Double.MIN_NORMAL, -53))); 1520 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1521 -Double.MIN_NORMAL, -53))); 1522 assertEquals(Double.MIN_VALUE, Math.scalb(Double.MAX_VALUE, -2098)); 1523 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1524 Double.MAX_VALUE, -2099))); 1525 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1526 -Double.MAX_VALUE, -2099))); 1527 assertEquals(Double.MIN_VALUE, Math.scalb(Double.MIN_NORMAL / 3, -51)); 1528 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1529 Double.MIN_NORMAL / 3, -52))); 1530 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1531 -Double.MIN_NORMAL / 3, -52))); 1532 double subnormal = Math.scalb(Double.MIN_NORMAL / 3, -25); 1533 assertEquals(2.2104123E-316, subnormal); 1534 // precision lost 1535 assertFalse(Double.MIN_NORMAL / 3 == Math.scalb(subnormal, 25)); 1536 1537 // NaN 1538 assertTrue(Double.isNaN(Math.scalb(Double.NaN, 1))); 1539 assertTrue(Double.isNaN(Math.scalb(Double.NaN, 0))); 1540 assertTrue(Double.isNaN(Math.scalb(Double.NaN, -120))); 1541 1542 assertEquals(1283457024, Double.doubleToLongBits(Math.scalb( 1543 Double.MIN_VALUE * 153, 23))); 1544 assertEquals(-9223372035571318784L, Double.doubleToLongBits(Math.scalb( 1545 -Double.MIN_VALUE * 153, 23))); 1546 assertEquals(36908406321184768L, Double.doubleToLongBits(Math.scalb( 1547 Double.MIN_VALUE * 153, 52))); 1548 assertEquals(-9186463630533591040L, Double.doubleToLongBits(Math.scalb( 1549 -Double.MIN_VALUE * 153, 52))); 1550 1551 // test for exception 1552 try { 1553 Math.scalb((Double) null, (Integer) null); 1554 fail("Should throw NullPointerException"); 1555 } catch (NullPointerException e) { 1556 // Expected 1557 } 1558 try { 1559 Math.scalb(1.0, (Integer) null); 1560 fail("Should throw NullPointerException"); 1561 } catch (NullPointerException e) { 1562 // Expected 1563 } 1564 try { 1565 Math.scalb((Double) null, 1); 1566 fail("Should throw NullPointerException"); 1567 } catch (NullPointerException e) { 1568 // Expected 1569 } 1570 1571 long b1em1022 = 0x0010000000000000L; // bit representation of 1572 // Double.MIN_NORMAL 1573 long b1em1023 = 0x0008000000000000L; // bit representation of half of 1574 // Double.MIN_NORMAL 1575 // assert exact identity 1576 assertEquals(b1em1023, Double.doubleToLongBits(Math.scalb(Double 1577 .longBitsToDouble(b1em1022), -1))); 1578 } 1579 1580 /** 1581 * {@link java.lang.Math#scalb(float, int)} 1582 * @since 1.6 1583 */ 1584 @SuppressWarnings("boxing") test_scalb_FI()1585 public void test_scalb_FI() { 1586 // result is normal 1587 assertEquals(4.1422946304E7f, Math.scalb(1.2345f, 25)); 1588 assertEquals(3.679096698760986E-8f, Math.scalb(1.2345f, -25)); 1589 assertEquals(1.2345f, Math.scalb(1.2345f, 0)); 1590 assertEquals(7868514.304f, Math.scalb(0.2345f, 25)); 1591 1592 float normal = Math.scalb(0.2345f, -25); 1593 assertEquals(6.98864459991455E-9f, normal); 1594 // precision kept 1595 assertEquals(0.2345f, Math.scalb(normal, 25)); 1596 1597 assertEquals(0.2345f, Math.scalb(0.2345f, 0)); 1598 assertEquals(-4.1422946304E7f, Math.scalb(-1.2345f, 25)); 1599 assertEquals(-6.98864459991455E-9f, Math.scalb(-0.2345f, -25)); 1600 assertEquals(2.0f, Math.scalb(Float.MIN_NORMAL / 2, 128)); 1601 assertEquals(64.0f, Math.scalb(Float.MIN_VALUE, 155)); 1602 assertEquals(34, Math.getExponent(Math.scalb(1.0f, 34))); 1603 assertEquals(3.9999998f, Math 1604 .scalb(Float.MAX_VALUE, Float.MIN_EXPONENT)); 1605 1606 // result is near infinity 1607 float halfMax = Math.scalb(1.0f, Float.MAX_EXPONENT); 1608 assertEquals(1.7014118E38f, halfMax); 1609 assertEquals(Float.MAX_VALUE, halfMax - Math.ulp(halfMax) + halfMax); 1610 assertEquals(Float.POSITIVE_INFINITY, halfMax + halfMax); 1611 assertEquals(3.4028233E38f, Math.scalb(1.0f - Math.ulp(1.0f), 1612 Float.MAX_EXPONENT + 1)); 1613 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(1.0f - Math.ulp(1.0f), 1614 Float.MAX_EXPONENT + 2)); 1615 1616 halfMax = Math.scalb(-1.0f, Float.MAX_EXPONENT); 1617 assertEquals(-1.7014118E38f, halfMax); 1618 assertEquals(-Float.MAX_VALUE, halfMax + Math.ulp(halfMax) + halfMax); 1619 assertEquals(Float.NEGATIVE_INFINITY, halfMax + halfMax); 1620 1621 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(0.345f, 1234)); 1622 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(44.345E10f, 934)); 1623 assertEquals(Float.NEGATIVE_INFINITY, Math.scalb(-44.345E10f, 934)); 1624 1625 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(Float.MIN_NORMAL / 2, 1626 400)); 1627 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(Float.MIN_VALUE, 800)); 1628 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(Float.MAX_VALUE, 1)); 1629 assertEquals(Float.POSITIVE_INFINITY, Math.scalb( 1630 Float.POSITIVE_INFINITY, 0)); 1631 assertEquals(Float.POSITIVE_INFINITY, Math.scalb( 1632 Float.POSITIVE_INFINITY, -1)); 1633 assertEquals(Float.NEGATIVE_INFINITY, Math.scalb( 1634 Float.NEGATIVE_INFINITY, -1)); 1635 assertEquals(Float.NEGATIVE_INFINITY, Math.scalb( 1636 Float.NEGATIVE_INFINITY, Float.MIN_EXPONENT)); 1637 1638 // result is subnormal/zero 1639 int posZeroBits = Float.floatToIntBits(+0.0f); 1640 int negZeroBits = Float.floatToIntBits(-0.0f); 1641 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(+0.0f, 1642 Integer.MAX_VALUE))); 1643 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(+0.0f, -123))); 1644 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(+0.0f, 0))); 1645 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-0.0f, 123))); 1646 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-0.0f, 1647 Integer.MIN_VALUE))); 1648 1649 assertEquals(Float.MIN_VALUE, Math.scalb(1.0f, -149)); 1650 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(1.0f, -150))); 1651 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-1.0f, -150))); 1652 1653 // precision lost 1654 assertEquals(Math.scalb(21.405f, -154), Math.scalb(21.405f, -153)); 1655 assertEquals(Float.MIN_VALUE, Math.scalb(21.405f, -154)); 1656 assertEquals(-Float.MIN_VALUE, Math.scalb(-21.405f, -154)); 1657 assertEquals(posZeroBits, Float.floatToIntBits(Math 1658 .scalb(21.405f, -155))); 1659 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-21.405f, 1660 -155))); 1661 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1662 Float.MIN_VALUE, -1))); 1663 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1664 -Float.MIN_VALUE, -1))); 1665 assertEquals(Float.MIN_VALUE, Math.scalb(Float.MIN_NORMAL, -23)); 1666 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1667 Float.MIN_NORMAL, -24))); 1668 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1669 -Float.MIN_NORMAL, -24))); 1670 assertEquals(Float.MIN_VALUE, Math.scalb(Float.MAX_VALUE, -277)); 1671 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1672 Float.MAX_VALUE, -278))); 1673 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1674 -Float.MAX_VALUE, -278))); 1675 assertEquals(Float.MIN_VALUE, Math.scalb(Float.MIN_NORMAL / 3, -22)); 1676 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1677 Float.MIN_NORMAL / 3, -23))); 1678 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1679 -Float.MIN_NORMAL / 3, -23))); 1680 float subnormal = Math.scalb(Float.MIN_NORMAL / 3, -11); 1681 assertEquals(1.913E-42f, subnormal); 1682 // precision lost 1683 assertFalse(Float.MIN_NORMAL / 3 == Math.scalb(subnormal, 11)); 1684 1685 assertEquals(68747264, Float.floatToIntBits(Math.scalb( 1686 Float.MIN_VALUE * 153, 23))); 1687 assertEquals(-2078736384, Float.floatToIntBits(Math.scalb( 1688 -Float.MIN_VALUE * 153, 23))); 1689 1690 assertEquals(4896, Float.floatToIntBits(Math.scalb( 1691 Float.MIN_VALUE * 153, 5))); 1692 assertEquals(-2147478752, Float.floatToIntBits(Math.scalb( 1693 -Float.MIN_VALUE * 153, 5))); 1694 1695 // NaN 1696 assertTrue(Float.isNaN(Math.scalb(Float.NaN, 1))); 1697 assertTrue(Float.isNaN(Math.scalb(Float.NaN, 0))); 1698 assertTrue(Float.isNaN(Math.scalb(Float.NaN, -120))); 1699 1700 // test for exception 1701 try { 1702 Math.scalb((Float) null, (Integer) null); 1703 fail("Should throw NullPointerException"); 1704 } catch (NullPointerException e) { 1705 // Expected 1706 } 1707 try { 1708 Math.scalb(1.0f, (Integer) null); 1709 fail("Should throw NullPointerException"); 1710 } catch (NullPointerException e) { 1711 // Expected 1712 } 1713 try { 1714 Math.scalb((Float) null, 1); 1715 fail("Should throw NullPointerException"); 1716 } catch (NullPointerException e) { 1717 // Expected 1718 } 1719 1720 int b1em126 = 0x00800000; // bit representation of Float.MIN_NORMAL 1721 int b1em127 = 0x00400000; // bit representation of half 1722 // Float.MIN_NORMAL 1723 // assert exact identity 1724 assertEquals(b1em127, Float.floatToIntBits(Math.scalb(Float 1725 .intBitsToFloat(b1em126), -1))); 1726 } 1727 1728 /** 1729 * java.lang.Math#signum(double) 1730 */ test_signum_D()1731 public void test_signum_D() { 1732 assertTrue(Double.isNaN(Math.signum(Double.NaN))); 1733 assertTrue(Double.isNaN(Math.signum(Double.NaN))); 1734 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 1735 .signum(0.0))); 1736 assertEquals(Double.doubleToLongBits(+0.0), Double 1737 .doubleToLongBits(Math.signum(+0.0))); 1738 assertEquals(Double.doubleToLongBits(-0.0), Double 1739 .doubleToLongBits(Math.signum(-0.0))); 1740 1741 assertEquals(1.0, Math.signum(253681.2187962), 0D); 1742 assertEquals(-1.0, Math.signum(-125874693.56), 0D); 1743 assertEquals(1.0, Math.signum(1.2587E-308), 0D); 1744 assertEquals(-1.0, Math.signum(-1.2587E-308), 0D); 1745 1746 assertEquals(1.0, Math.signum(Double.MAX_VALUE), 0D); 1747 assertEquals(1.0, Math.signum(Double.MIN_VALUE), 0D); 1748 assertEquals(-1.0, Math.signum(-Double.MAX_VALUE), 0D); 1749 assertEquals(-1.0, Math.signum(-Double.MIN_VALUE), 0D); 1750 assertEquals(1.0, Math.signum(Double.POSITIVE_INFINITY), 0D); 1751 assertEquals(-1.0, Math.signum(Double.NEGATIVE_INFINITY), 0D); 1752 } 1753 1754 /** 1755 * java.lang.Math#signum(float) 1756 */ test_signum_F()1757 public void test_signum_F() { 1758 assertTrue(Float.isNaN(Math.signum(Float.NaN))); 1759 assertEquals(Float.floatToIntBits(0.0f), Float 1760 .floatToIntBits(Math.signum(0.0f))); 1761 assertEquals(Float.floatToIntBits(+0.0f), Float 1762 .floatToIntBits(Math.signum(+0.0f))); 1763 assertEquals(Float.floatToIntBits(-0.0f), Float 1764 .floatToIntBits(Math.signum(-0.0f))); 1765 1766 assertEquals(1.0f, Math.signum(253681.2187962f), 0f); 1767 assertEquals(-1.0f, Math.signum(-125874693.56f), 0f); 1768 assertEquals(1.0f, Math.signum(1.2587E-11f), 0f); 1769 assertEquals(-1.0f, Math.signum(-1.2587E-11f), 0f); 1770 1771 assertEquals(1.0f, Math.signum(Float.MAX_VALUE), 0f); 1772 assertEquals(1.0f, Math.signum(Float.MIN_VALUE), 0f); 1773 assertEquals(-1.0f, Math.signum(-Float.MAX_VALUE), 0f); 1774 assertEquals(-1.0f, Math.signum(-Float.MIN_VALUE), 0f); 1775 assertEquals(1.0f, Math.signum(Float.POSITIVE_INFINITY), 0f); 1776 assertEquals(-1.0f, Math.signum(Float.NEGATIVE_INFINITY), 0f); 1777 } 1778 1779 /** 1780 * java.lang.Math#sin(double) 1781 */ test_sinD()1782 public void test_sinD() { 1783 // Test for method double java.lang.Math.sin(double) 1784 assertEquals("Incorrect answer", 0.0, Math.sin(0), 0D); 1785 assertEquals("Incorrect answer", 0.8414709848078965, Math.sin(1), 0D); 1786 } 1787 1788 /** 1789 * java.lang.Math#sinh(double) 1790 */ test_sinh_D()1791 public void test_sinh_D() { 1792 // Test for special situations 1793 assertTrue(Double.isNaN(Math.sinh(Double.NaN))); 1794 assertEquals(Double.POSITIVE_INFINITY, Math.sinh(Double.POSITIVE_INFINITY), 0D); 1795 assertEquals(Double.NEGATIVE_INFINITY, Math.sinh(Double.NEGATIVE_INFINITY), 0D); 1796 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math.sinh(0.0))); 1797 assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math.sinh(+0.0))); 1798 assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math.sinh(-0.0))); 1799 1800 assertEquals(Double.POSITIVE_INFINITY, Math.sinh(1234.56), 0D); 1801 assertEquals(Double.NEGATIVE_INFINITY, Math.sinh(-1234.56), 0D); 1802 assertEquals(1.0000000000001666E-6, Math.sinh(0.000001), 0D); 1803 assertEquals(-1.0000000000001666E-6, Math.sinh(-0.000001), 0D); 1804 assertEquals(5.115386441963859, Math.sinh(2.33482), Math.ulp(5.115386441963859)); 1805 assertEquals(Double.POSITIVE_INFINITY, Math.sinh(Double.MAX_VALUE), 0D); 1806 assertEquals(4.9E-324, Math.sinh(Double.MIN_VALUE), 0D); 1807 } 1808 1809 /** 1810 * java.lang.Math#sqrt(double) 1811 */ test_sqrtD()1812 public void test_sqrtD() { 1813 // Test for method double java.lang.Math.sqrt(double) 1814 assertEquals("Incorrect root returned2", 7, Math.sqrt(49), 0); 1815 } 1816 1817 /** 1818 * java.lang.Math#tan(double) 1819 */ test_tanD()1820 public void test_tanD() { 1821 // Test for method double java.lang.Math.tan(double) 1822 assertEquals("Incorrect answer", 0.0, Math.tan(0), 0D); 1823 assertEquals("Incorrect answer", 1.5574077246549023, Math.tan(1), 0D); 1824 1825 } 1826 1827 /** 1828 * java.lang.Math#tanh(double) 1829 */ test_tanh_D()1830 public void test_tanh_D() { 1831 // Test for special situations 1832 assertTrue("Should return NaN", Double.isNaN(Math.tanh(Double.NaN))); 1833 assertEquals("Should return +1.0", +1.0, Math 1834 .tanh(Double.POSITIVE_INFINITY), 0D); 1835 assertEquals("Should return -1.0", -1.0, Math 1836 .tanh(Double.NEGATIVE_INFINITY), 0D); 1837 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 1838 .tanh(0.0))); 1839 assertEquals(Double.doubleToLongBits(+0.0), Double 1840 .doubleToLongBits(Math.tanh(+0.0))); 1841 assertEquals(Double.doubleToLongBits(-0.0), Double 1842 .doubleToLongBits(Math.tanh(-0.0))); 1843 1844 assertEquals("Should return 1.0", 1.0, Math.tanh(1234.56), 0D); 1845 assertEquals("Should return -1.0", -1.0, Math.tanh(-1234.56), 0D); 1846 assertEquals("Should return 9.999999999996666E-7", 1847 9.999999999996666E-7, Math.tanh(0.000001), 0D); 1848 assertEquals("Should return 0.981422884124941", 0.981422884124941, Math 1849 .tanh(2.33482), 0D); 1850 assertEquals("Should return 1.0", 1.0, Math.tanh(Double.MAX_VALUE), 0D); 1851 assertEquals("Should return 4.9E-324", 4.9E-324, Math 1852 .tanh(Double.MIN_VALUE), 0D); 1853 } 1854 1855 /** 1856 * java.lang.Math#random() 1857 */ test_random()1858 public void test_random() { 1859 // There isn't a place for these tests so just stick them here 1860 assertEquals("Wrong value E", 1861 4613303445314885481L, Double.doubleToLongBits(Math.E)); 1862 assertEquals("Wrong value PI", 1863 4614256656552045848L, Double.doubleToLongBits(Math.PI)); 1864 1865 for (int i = 500; i >= 0; i--) { 1866 double d = Math.random(); 1867 assertTrue("Generated number is out of range: " + d, d >= 0.0 1868 && d < 1.0); 1869 } 1870 } 1871 1872 /** 1873 * java.lang.Math#toRadians(double) 1874 */ test_toRadiansD()1875 public void test_toRadiansD() { 1876 for (double d = 500; d >= 0; d -= 1.0) { 1877 double converted = Math.toDegrees(Math.toRadians(d)); 1878 assertTrue("Converted number not equal to original. d = " + d, 1879 converted >= d * 0.99999999 && converted <= d * 1.00000001); 1880 } 1881 } 1882 1883 /** 1884 * java.lang.Math#toDegrees(double) 1885 */ test_toDegreesD()1886 public void test_toDegreesD() { 1887 for (double d = 500; d >= 0; d -= 1.0) { 1888 double converted = Math.toRadians(Math.toDegrees(d)); 1889 assertTrue("Converted number not equal to original. d = " + d, 1890 converted >= d * 0.99999999 && converted <= d * 1.00000001); 1891 } 1892 } 1893 1894 /** 1895 * java.lang.Math#ulp(double) 1896 */ 1897 @SuppressWarnings("boxing") test_ulp_D()1898 public void test_ulp_D() { 1899 // Test for special cases 1900 assertTrue("Should return NaN", Double.isNaN(Math.ulp(Double.NaN))); 1901 assertEquals("Returned incorrect value", Double.POSITIVE_INFINITY, Math 1902 .ulp(Double.POSITIVE_INFINITY), 0D); 1903 assertEquals("Returned incorrect value", Double.POSITIVE_INFINITY, Math 1904 .ulp(Double.NEGATIVE_INFINITY), 0D); 1905 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1906 .ulp(0.0), 0D); 1907 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1908 .ulp(+0.0), 0D); 1909 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1910 .ulp(-0.0), 0D); 1911 assertEquals("Returned incorrect value", Math.pow(2, 971), Math 1912 .ulp(Double.MAX_VALUE), 0D); 1913 assertEquals("Returned incorrect value", Math.pow(2, 971), Math 1914 .ulp(-Double.MAX_VALUE), 0D); 1915 1916 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1917 .ulp(Double.MIN_VALUE), 0D); 1918 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1919 .ulp(-Double.MIN_VALUE), 0D); 1920 1921 assertEquals("Returned incorrect value", 2.220446049250313E-16, Math 1922 .ulp(1.0), 0D); 1923 assertEquals("Returned incorrect value", 2.220446049250313E-16, Math 1924 .ulp(-1.0), 0D); 1925 assertEquals("Returned incorrect value", 2.2737367544323206E-13, Math 1926 .ulp(1153.0), 0D); 1927 } 1928 1929 /** 1930 * java.lang.Math#ulp(float) 1931 */ 1932 @SuppressWarnings("boxing") test_ulp_f()1933 public void test_ulp_f() { 1934 // Test for special cases 1935 assertTrue("Should return NaN", Float.isNaN(Math.ulp(Float.NaN))); 1936 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, Math 1937 .ulp(Float.POSITIVE_INFINITY), 0f); 1938 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, Math 1939 .ulp(Float.NEGATIVE_INFINITY), 0f); 1940 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 1941 .ulp(0.0f), 0f); 1942 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 1943 .ulp(+0.0f), 0f); 1944 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 1945 .ulp(-0.0f), 0f); 1946 assertEquals("Returned incorrect value", 2.028241E31f, Math 1947 .ulp(Float.MAX_VALUE), 0f); 1948 assertEquals("Returned incorrect value", 2.028241E31f, Math 1949 .ulp(-Float.MAX_VALUE), 0f); 1950 1951 assertEquals("Returned incorrect value", 1.4E-45f, Math 1952 .ulp(Float.MIN_VALUE), 0f); 1953 assertEquals("Returned incorrect value", 1.4E-45f, Math 1954 .ulp(-Float.MIN_VALUE), 0f); 1955 1956 assertEquals("Returned incorrect value", 1.1920929E-7f, Math.ulp(1.0f), 1957 0f); 1958 assertEquals("Returned incorrect value", 1.1920929E-7f, 1959 Math.ulp(-1.0f), 0f); 1960 assertEquals("Returned incorrect value", 1.2207031E-4f, Math 1961 .ulp(1153.0f), 0f); 1962 assertEquals("Returned incorrect value", 5.6E-45f, Math 1963 .ulp(9.403954E-38f), 0f); 1964 } 1965 1966 /** 1967 * {@link java.lang.Math#shiftIntBits(int, int)} 1968 * @since 1.6 1969 */ test_shiftIntBits_II()1970 public void test_shiftIntBits_II() { 1971 class Tuple { 1972 public int result; 1973 1974 public int value; 1975 1976 public int factor; 1977 1978 public Tuple(int result, int value, int factor) { 1979 this.result = result; 1980 this.value = value; 1981 this.factor = factor; 1982 } 1983 } 1984 final Tuple[] TUPLES = new Tuple[] { 1985 // sub-normal to sub-normal 1986 new Tuple(0x00000000, 0x00000001, -1), 1987 // round to even 1988 new Tuple(0x00000002, 0x00000003, -1), 1989 // round to even 1990 new Tuple(0x00000001, 0x00000005, -3), 1991 // round to infinity 1992 new Tuple(0x00000002, 0x0000000d, -3), 1993 // round to infinity 1994 1995 // normal to sub-normal 1996 new Tuple(0x00000002, 0x01a00000, -24), 1997 // round to even 1998 new Tuple(0x00000004, 0x01e00000, -24), 1999 // round to even 2000 new Tuple(0x00000003, 0x01c80000, -24), 2001 // round to infinity 2002 new Tuple(0x00000004, 0x01e80000, -24), 2003 // round to infinity 2004 }; 2005 for (int i = 0; i < TUPLES.length; ++i) { 2006 Tuple tuple = TUPLES[i]; 2007 assertEquals(tuple.result, Float.floatToIntBits(Math.scalb(Float 2008 .intBitsToFloat(tuple.value), tuple.factor))); 2009 assertEquals(tuple.result, Float.floatToIntBits(-Math.scalb(-Float 2010 .intBitsToFloat(tuple.value), tuple.factor))); 2011 } 2012 } 2013 2014 /** 2015 * {@link java.lang.Math#shiftLongBits(long, long)} 2016 * <p/> 2017 * Round result to nearest value on precision lost. 2018 * @since 1.6 2019 */ test_shiftLongBits_LL()2020 public void test_shiftLongBits_LL() { 2021 class Tuple { 2022 public long result; 2023 2024 public long value; 2025 2026 public int factor; 2027 2028 public Tuple(long result, long value, int factor) { 2029 this.result = result; 2030 this.value = value; 2031 this.factor = factor; 2032 } 2033 } 2034 final Tuple[] TUPLES = new Tuple[] { 2035 // sub-normal to sub-normal 2036 new Tuple(0x00000000L, 0x00000001L, -1), 2037 //round to even 2038 new Tuple(0x00000002L, 0x00000003L, -1), 2039 //round to even 2040 new Tuple(0x00000001L, 0x00000005L, -3), 2041 //round to infinity 2042 new Tuple(0x00000002L, 0x0000000dL, -3), 2043 //round to infinity 2044 2045 // normal to sub-normal 2046 new Tuple(0x0000000000000002L, 0x0034000000000000L, -53), // round to even 2047 new Tuple(0x0000000000000004L, 0x003c000000000000L, -53), // round to even 2048 new Tuple(0x0000000000000003L, 0x0035000000000000L, -53), // round to infinity 2049 new Tuple(0x0000000000000004L, 0x003d000000000000L, -53), // round to infinity 2050 }; 2051 for (int i = 0; i < TUPLES.length; ++i) { 2052 Tuple tuple = TUPLES[i]; 2053 assertEquals(tuple.result, Double.doubleToLongBits(Math.scalb( 2054 Double.longBitsToDouble(tuple.value), tuple.factor))); 2055 assertEquals(tuple.result, Double.doubleToLongBits(-Math.scalb( 2056 -Double.longBitsToDouble(tuple.value), tuple.factor))); 2057 } 2058 } 2059 } 2060