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 nagateDval = negateDouble(dval); 1149 if (nagateDval == Double.NaN) { 1150 continue; 1151 } 1152 1153 // If the second argument is positive or negative zero, then the 1154 // result is 1.0. 1155 assertEquals("Result should be Math.pow(" + dval 1156 + ",-0.0)=+1.0", 1.0, Math.pow(dval, NZERO)); 1157 assertEquals("Result should be Math.pow(" + nagateDval 1158 + ",-0.0)=+1.0", 1.0, Math.pow(nagateDval, NZERO)); 1159 assertEquals("Result should be Math.pow(" + dval 1160 + ",+0.0)=+1.0", 1.0, Math.pow(dval, +0.0)); 1161 assertEquals("Result should be Math.pow(" + nagateDval 1162 + ",+0.0)=+1.0", 1.0, Math.pow(nagateDval, +0.0)); 1163 1164 // If the second argument is 1.0, then the result is the same as the 1165 // first argument. 1166 assertEquals("Result should be Math.pow(" + dval + "," + 1.0 + ")=" 1167 + dval, dval, Math.pow(dval, 1.0)); 1168 assertEquals("Result should be Math.pow(" + nagateDval + "," + 1.0 1169 + ")=" + nagateDval, nagateDval, Math.pow(nagateDval, 1.0)); 1170 1171 // If the second argument is NaN, then the result is NaN. 1172 assertEquals("Result should be Math.pow(" + dval + "," + Double.NaN 1173 + ")=" + Double.NaN, Double.NaN, Math.pow(dval, Double.NaN)); 1174 assertEquals("Result should be Math.pow(" + nagateDval + "," 1175 + Double.NaN + ")=" + Double.NaN, Double.NaN, Math.pow(nagateDval, 1176 Double.NaN)); 1177 1178 if (dval > 1) { 1179 // If the first argument is NaN and the second argument is 1180 // nonzero, 1181 // then the result is NaN. 1182 assertEquals("Result should be Math.pow(" + Double.NaN + "," 1183 + dval + ")=" + Double.NaN, Double.NaN, Math.pow(Double.NaN, dval)); 1184 assertEquals("Result should be Math.pow(" + Double.NaN + "," 1185 + nagateDval + ")=" + Double.NaN, Double.NaN, Math.pow(Double.NaN, 1186 nagateDval)); 1187 1188 /* 1189 * If the first argument is positive zero and the second 1190 * argument is greater than zero, or the first argument is 1191 * positive infinity and the second argument is less than zero, 1192 * then the result is positive zero. 1193 */ 1194 assertEquals("Result should be Math.pow(" + 0.0 + "," + dval 1195 + ")=" + 0.0, +0.0, Math.pow(0.0, dval)); 1196 assertEquals("Result should be Math.pow(" 1197 + Double.POSITIVE_INFINITY + "," + nagateDval + ")=" 1198 + 0.0, +0.0, Math.pow(Double.POSITIVE_INFINITY, nagateDval)); 1199 1200 /* 1201 * If the first argument is positive zero and the second 1202 * argument is less than zero, or the first argument is positive 1203 * infinity and the second argument is greater than zero, then 1204 * the result is positive infinity. 1205 */ 1206 assertEquals("Result should be Math.pow(" + 0.0 + "," 1207 + nagateDval + ")=" + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1208 Math.pow(0.0, nagateDval)); 1209 assertEquals("Result should be Math.pow(" 1210 + Double.POSITIVE_INFINITY + "," + dval + ")=" 1211 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow( 1212 Double.POSITIVE_INFINITY, dval)); 1213 1214 // Not a finite odd integer 1215 if (dval % 2 == 0) { 1216 /* 1217 * If the first argument is negative zero and the second 1218 * argument is greater than zero but not a finite odd 1219 * integer, or the first argument is negative infinity and 1220 * the second argument is less than zero but not a finite 1221 * odd integer, then the result is positive zero. 1222 */ 1223 assertEquals("Result should be Math.pow(" + NZERO + "," 1224 + dval + ")=" + 0.0, +0.0, Math.pow(NZERO, dval)); 1225 assertEquals("Result should be Math.pow(" 1226 + Double.NEGATIVE_INFINITY + "," + nagateDval 1227 + ")=" + 0.0, +0.0, Math.pow(Double.NEGATIVE_INFINITY, 1228 nagateDval)); 1229 1230 /* 1231 * If the first argument is negative zero and the second 1232 * argument is less than zero but not a finite odd integer, 1233 * or the first argument is negative infinity and the second 1234 * argument is greater than zero but not a finite odd 1235 * integer, then the result is positive infinity. 1236 */ 1237 assertEquals("Result should be Math.pow(" + NZERO + "," 1238 + nagateDval + ")=" + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1239 Math.pow(NZERO, nagateDval)); 1240 assertEquals("Result should be Math.pow(" 1241 + Double.NEGATIVE_INFINITY + "," + dval + ")=" 1242 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow( 1243 Double.NEGATIVE_INFINITY, dval)); 1244 } 1245 1246 // finite odd integer 1247 if (dval % 2 != 0) { 1248 /* 1249 * If the first argument is negative zero and the second 1250 * argument is a positive finite odd integer, or the first 1251 * argument is negative infinity and the second argument is 1252 * a negative finite odd integer, then the result is 1253 * negative zero. 1254 */ 1255 assertEquals("Result should be Math.pow(" + NZERO + "," 1256 + dval + ")=" + NZERO, NZERO, Math.pow(NZERO, dval)); 1257 assertEquals("Result should be Math.pow(" 1258 + Double.NEGATIVE_INFINITY + "," + nagateDval 1259 + ")=" + NZERO, NZERO, Math.pow(Double.NEGATIVE_INFINITY, 1260 nagateDval)); 1261 /* 1262 * If the first argument is negative zero and the second 1263 * argument is a negative finite odd integer, or the first 1264 * argument is negative infinity and the second argument is 1265 * a positive finite odd integer then the result is negative 1266 * infinity. 1267 */ 1268 assertEquals("Result should be Math.pow(" + NZERO + "," 1269 + nagateDval + ")=" + Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1270 Math.pow(NZERO, nagateDval)); 1271 assertEquals("Result should be Math.pow(" 1272 + Double.NEGATIVE_INFINITY + "," + dval + ")=" 1273 + Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Math.pow( 1274 Double.NEGATIVE_INFINITY, dval)); 1275 } 1276 1277 /** 1278 * 1. If the first argument is finite and less than zero if the 1279 * second argument is a finite even integer, the result is equal 1280 * to the result of raising the absolute value of the first 1281 * argument to the power of the second argument 1282 * 1283 * 2. if the second argument is a finite odd integer, the result is equal to the 1284 * negative of the result of raising the absolute value of the 1285 * first argument to the power of the second argument 1286 * 1287 * 3. if the second argument is finite and not an integer, then the result 1288 * is NaN. 1289 */ 1290 for (int j = 1; j < values.length; j++) { 1291 double jval = values[j]; 1292 if (jval % 2.0 == 0.0) { 1293 assertEquals("" + nagateDval + " " + jval, Math.pow( 1294 dval, jval), Math.pow(nagateDval, jval)); 1295 } else { 1296 assertEquals("" + nagateDval + " " + jval, -1.0 1297 * Math.pow(dval, jval), Math.pow(nagateDval, 1298 jval)); 1299 } 1300 assertEquals(Double.NaN, Math 1301 .pow(nagateDval, jval / 0.5467)); 1302 assertEquals(Double.NaN, Math.pow(nagateDval, -1.0 * jval 1303 / 0.5467)); 1304 } 1305 } 1306 1307 // If the absolute value of the first argument equals 1 and the 1308 // second argument is infinite, then the result is NaN. 1309 if (dval == 1) { 1310 assertEquals("Result should be Math.pow(" + dval + "," 1311 + Double.POSITIVE_INFINITY + ")=" + Double.NaN, Double.NaN, Math 1312 .pow(dval, Double.POSITIVE_INFINITY)); 1313 assertEquals("Result should be Math.pow(" + dval + "," 1314 + Double.NEGATIVE_INFINITY + ")=" + Double.NaN, Double.NaN, Math 1315 .pow(dval, Double.NEGATIVE_INFINITY)); 1316 1317 assertEquals("Result should be Math.pow(" + nagateDval + "," 1318 + Double.POSITIVE_INFINITY + ")=" + Double.NaN, Double.NaN, Math 1319 .pow(nagateDval, Double.POSITIVE_INFINITY)); 1320 assertEquals("Result should be Math.pow(" + nagateDval + "," 1321 + Double.NEGATIVE_INFINITY + ")=" + Double.NaN, Double.NaN, Math 1322 .pow(nagateDval, Double.NEGATIVE_INFINITY)); 1323 } 1324 1325 if (dval > 1) { 1326 /* 1327 * If the absolute value of the first argument is greater than 1 1328 * and the second argument is positive infinity, or the absolute 1329 * value of the first argument is less than 1 and the second 1330 * argument is negative infinity, then the result is positive 1331 * infinity. 1332 */ 1333 assertEquals("Result should be Math.pow(" + dval + "," 1334 + Double.POSITIVE_INFINITY + ")=" 1335 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow(dval, 1336 Double.POSITIVE_INFINITY)); 1337 1338 assertEquals("Result should be Math.pow(" + nagateDval + "," 1339 + Double.NEGATIVE_INFINITY + ")=" 1340 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow(-0.13456, 1341 Double.NEGATIVE_INFINITY)); 1342 1343 /* 1344 * If the absolute value of the first argument is greater than 1 1345 * and the second argument is negative infinity, or the absolute 1346 * value of the first argument is less than 1 and the second 1347 * argument is positive infinity, then the result is positive 1348 * zero. 1349 */ 1350 assertEquals("Result should be Math.pow(" + dval + "," 1351 + Double.NEGATIVE_INFINITY + ")= +0.0", +0.0, Math.pow(dval, 1352 Double.NEGATIVE_INFINITY)); 1353 assertEquals("Result should be Math.pow(" + nagateDval + "," 1354 + Double.POSITIVE_INFINITY + ")= +0.0", +0.0, Math.pow( 1355 -0.13456, Double.POSITIVE_INFINITY)); 1356 } 1357 1358 assertEquals("Result should be Math.pow(" + 0.0 + "," + dval + ")=" 1359 + 0.0, 0.0, Math.pow(0.0, dval)); 1360 assertEquals("Result should be Math.pow(" + Double.NaN + "," + dval 1361 + ")=" + Double.NaN, Double.NaN, Math.pow(Double.NaN, dval)); 1362 } 1363 assertTrue("pow returned incorrect value", 1364 (long) Math.pow(2, 8) == 256l); 1365 assertTrue("pow returned incorrect value", 1366 Math.pow(2, -8) == 0.00390625d); 1367 assertEquals("Incorrect root returned1", 1368 2, Math.sqrt(Math.pow(Math.sqrt(2), 4)), 0); 1369 1370 assertEquals(Double.NEGATIVE_INFINITY, Math.pow(-10.0, 3.093403029238847E15)); 1371 assertEquals(Double.POSITIVE_INFINITY, Math.pow(10.0, 3.093403029238847E15)); 1372 } 1373 longTodouble(long longvalue)1374 private double longTodouble(long longvalue) { 1375 return Double.longBitsToDouble(longvalue); 1376 } 1377 doubleTolong(double doublevalue)1378 private long doubleTolong(double doublevalue) { 1379 return Double.doubleToLongBits(doublevalue); 1380 } 1381 negateDouble(double doublevalue)1382 private double negateDouble(double doublevalue) { 1383 return doublevalue * -1.0; 1384 } 1385 1386 /** 1387 * java.lang.Math#rint(double) 1388 */ test_rintD()1389 public void test_rintD() { 1390 // Test for method double java.lang.Math.rint(double) 1391 assertEquals("Failed to round properly - up to odd", 1392 3.0, Math.rint(2.9), 0D); 1393 assertTrue("Failed to round properly - NaN", Double.isNaN(Math 1394 .rint(Double.NaN))); 1395 assertEquals("Failed to round properly down to even", 1396 2.0, Math.rint(2.1), 0D); 1397 assertTrue("Failed to round properly " + 2.5 + " to even", Math 1398 .rint(2.5) == 2.0); 1399 assertTrue("Failed to round properly " + (+0.0d), 1400 Math.rint(+0.0d) == +0.0d); 1401 assertTrue("Failed to round properly " + (-0.0d), 1402 Math.rint(-0.0d) == -0.0d); 1403 } 1404 1405 /** 1406 * java.lang.Math#round(double) 1407 */ test_roundD()1408 public void test_roundD() { 1409 // Test for method long java.lang.Math.round(double) 1410 assertEquals("Incorrect rounding of a float", -91, Math.round(-90.89d)); 1411 } 1412 1413 /** 1414 * java.lang.Math#round(float) 1415 */ test_roundF()1416 public void test_roundF() { 1417 // Test for method int java.lang.Math.round(float) 1418 assertEquals("Incorrect rounding of a float", -91, Math.round(-90.89f)); 1419 } 1420 1421 /** 1422 * {@link java.lang.Math#scalb(double, int)} 1423 * @since 1.6 1424 */ 1425 @SuppressWarnings("boxing") test_scalb_DI()1426 public void test_scalb_DI() { 1427 // result is normal 1428 assertEquals(4.1422946304E7, Math.scalb(1.2345, 25)); 1429 assertEquals(3.679096698760986E-8, Math.scalb(1.2345, -25)); 1430 assertEquals(1.2345, Math.scalb(1.2345, 0)); 1431 assertEquals(7868514.304, Math.scalb(0.2345, 25)); 1432 1433 double normal = Math.scalb(0.2345, -25); 1434 assertEquals(6.98864459991455E-9, normal); 1435 // precision kept 1436 assertEquals(0.2345, Math.scalb(normal, 25)); 1437 1438 assertEquals(0.2345, Math.scalb(0.2345, 0)); 1439 assertEquals(-4.1422946304E7, Math.scalb(-1.2345, 25)); 1440 assertEquals(-6.98864459991455E-9, Math.scalb(-0.2345, -25)); 1441 assertEquals(2.0, Math.scalb(Double.MIN_NORMAL / 2, 1024)); 1442 assertEquals(64.0, Math.scalb(Double.MIN_VALUE, 1080)); 1443 assertEquals(234, Math.getExponent(Math.scalb(1.0, 234))); 1444 assertEquals(3.9999999999999996, Math.scalb(Double.MAX_VALUE, 1445 Double.MIN_EXPONENT)); 1446 1447 // result is near infinity 1448 double halfMax = Math.scalb(1.0, Double.MAX_EXPONENT); 1449 assertEquals(8.98846567431158E307, halfMax); 1450 assertEquals(Double.MAX_VALUE, halfMax - Math.ulp(halfMax) + halfMax); 1451 assertEquals(Double.POSITIVE_INFINITY, halfMax + halfMax); 1452 assertEquals(1.7976931348623155E308, Math.scalb(1.0 - Math.ulp(1.0), 1453 Double.MAX_EXPONENT + 1)); 1454 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(1.0 - Math.ulp(1.0), 1455 Double.MAX_EXPONENT + 2)); 1456 1457 halfMax = Math.scalb(-1.0, Double.MAX_EXPONENT); 1458 assertEquals(-8.98846567431158E307, halfMax); 1459 assertEquals(-Double.MAX_VALUE, halfMax + Math.ulp(halfMax) + halfMax); 1460 assertEquals(Double.NEGATIVE_INFINITY, halfMax + halfMax); 1461 1462 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(0.345, 1234)); 1463 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(44.345E102, 934)); 1464 assertEquals(Double.NEGATIVE_INFINITY, Math.scalb(-44.345E102, 934)); 1465 1466 assertEquals(Double.POSITIVE_INFINITY, Math.scalb( 1467 Double.MIN_NORMAL / 2, 4000)); 1468 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(Double.MIN_VALUE, 1469 8000)); 1470 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(Double.MAX_VALUE, 1)); 1471 assertEquals(Double.POSITIVE_INFINITY, Math.scalb( 1472 Double.POSITIVE_INFINITY, 0)); 1473 assertEquals(Double.POSITIVE_INFINITY, Math.scalb( 1474 Double.POSITIVE_INFINITY, -1)); 1475 assertEquals(Double.NEGATIVE_INFINITY, Math.scalb( 1476 Double.NEGATIVE_INFINITY, -1)); 1477 assertEquals(Double.NEGATIVE_INFINITY, Math.scalb( 1478 Double.NEGATIVE_INFINITY, Double.MIN_EXPONENT)); 1479 1480 // result is subnormal/zero 1481 long posZeroBits = Double.doubleToLongBits(+0.0); 1482 long negZeroBits = Double.doubleToLongBits(-0.0); 1483 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb(+0.0, 1484 Integer.MAX_VALUE))); 1485 assertEquals(posZeroBits, Double.doubleToLongBits(Math 1486 .scalb(+0.0, -123))); 1487 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb(+0.0, 0))); 1488 assertEquals(negZeroBits, Double 1489 .doubleToLongBits(Math.scalb(-0.0, 123))); 1490 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb(-0.0, 1491 Integer.MIN_VALUE))); 1492 1493 assertEquals(Double.MIN_VALUE, Math.scalb(1.0, -1074)); 1494 assertEquals(posZeroBits, Double.doubleToLongBits(Math 1495 .scalb(1.0, -1075))); 1496 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb(-1.0, 1497 -1075))); 1498 1499 // precision lost 1500 assertEquals(Math.scalb(21.405, -1078), Math.scalb(21.405, -1079)); 1501 assertEquals(Double.MIN_VALUE, Math.scalb(21.405, -1079)); 1502 assertEquals(-Double.MIN_VALUE, Math.scalb(-21.405, -1079)); 1503 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb(21.405, 1504 -1080))); 1505 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb(-21.405, 1506 -1080))); 1507 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1508 Double.MIN_VALUE, -1))); 1509 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1510 -Double.MIN_VALUE, -1))); 1511 assertEquals(Double.MIN_VALUE, Math.scalb(Double.MIN_NORMAL, -52)); 1512 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1513 Double.MIN_NORMAL, -53))); 1514 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1515 -Double.MIN_NORMAL, -53))); 1516 assertEquals(Double.MIN_VALUE, Math.scalb(Double.MAX_VALUE, -2098)); 1517 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1518 Double.MAX_VALUE, -2099))); 1519 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1520 -Double.MAX_VALUE, -2099))); 1521 assertEquals(Double.MIN_VALUE, Math.scalb(Double.MIN_NORMAL / 3, -51)); 1522 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1523 Double.MIN_NORMAL / 3, -52))); 1524 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1525 -Double.MIN_NORMAL / 3, -52))); 1526 double subnormal = Math.scalb(Double.MIN_NORMAL / 3, -25); 1527 assertEquals(2.2104123E-316, subnormal); 1528 // precision lost 1529 assertFalse(Double.MIN_NORMAL / 3 == Math.scalb(subnormal, 25)); 1530 1531 // NaN 1532 assertTrue(Double.isNaN(Math.scalb(Double.NaN, 1))); 1533 assertTrue(Double.isNaN(Math.scalb(Double.NaN, 0))); 1534 assertTrue(Double.isNaN(Math.scalb(Double.NaN, -120))); 1535 1536 assertEquals(1283457024, Double.doubleToLongBits(Math.scalb( 1537 Double.MIN_VALUE * 153, 23))); 1538 assertEquals(-9223372035571318784L, Double.doubleToLongBits(Math.scalb( 1539 -Double.MIN_VALUE * 153, 23))); 1540 assertEquals(36908406321184768L, Double.doubleToLongBits(Math.scalb( 1541 Double.MIN_VALUE * 153, 52))); 1542 assertEquals(-9186463630533591040L, Double.doubleToLongBits(Math.scalb( 1543 -Double.MIN_VALUE * 153, 52))); 1544 1545 // test for exception 1546 try { 1547 Math.scalb((Double) null, (Integer) null); 1548 fail("Should throw NullPointerException"); 1549 } catch (NullPointerException e) { 1550 // Expected 1551 } 1552 try { 1553 Math.scalb(1.0, (Integer) null); 1554 fail("Should throw NullPointerException"); 1555 } catch (NullPointerException e) { 1556 // Expected 1557 } 1558 try { 1559 Math.scalb((Double) null, 1); 1560 fail("Should throw NullPointerException"); 1561 } catch (NullPointerException e) { 1562 // Expected 1563 } 1564 1565 long b1em1022 = 0x0010000000000000L; // bit representation of 1566 // Double.MIN_NORMAL 1567 long b1em1023 = 0x0008000000000000L; // bit representation of half of 1568 // Double.MIN_NORMAL 1569 // assert exact identity 1570 assertEquals(b1em1023, Double.doubleToLongBits(Math.scalb(Double 1571 .longBitsToDouble(b1em1022), -1))); 1572 } 1573 1574 /** 1575 * {@link java.lang.Math#scalb(float, int)} 1576 * @since 1.6 1577 */ 1578 @SuppressWarnings("boxing") test_scalb_FI()1579 public void test_scalb_FI() { 1580 // result is normal 1581 assertEquals(4.1422946304E7f, Math.scalb(1.2345f, 25)); 1582 assertEquals(3.679096698760986E-8f, Math.scalb(1.2345f, -25)); 1583 assertEquals(1.2345f, Math.scalb(1.2345f, 0)); 1584 assertEquals(7868514.304f, Math.scalb(0.2345f, 25)); 1585 1586 float normal = Math.scalb(0.2345f, -25); 1587 assertEquals(6.98864459991455E-9f, normal); 1588 // precision kept 1589 assertEquals(0.2345f, Math.scalb(normal, 25)); 1590 1591 assertEquals(0.2345f, Math.scalb(0.2345f, 0)); 1592 assertEquals(-4.1422946304E7f, Math.scalb(-1.2345f, 25)); 1593 assertEquals(-6.98864459991455E-9f, Math.scalb(-0.2345f, -25)); 1594 assertEquals(2.0f, Math.scalb(Float.MIN_NORMAL / 2, 128)); 1595 assertEquals(64.0f, Math.scalb(Float.MIN_VALUE, 155)); 1596 assertEquals(34, Math.getExponent(Math.scalb(1.0f, 34))); 1597 assertEquals(3.9999998f, Math 1598 .scalb(Float.MAX_VALUE, Float.MIN_EXPONENT)); 1599 1600 // result is near infinity 1601 float halfMax = Math.scalb(1.0f, Float.MAX_EXPONENT); 1602 assertEquals(1.7014118E38f, halfMax); 1603 assertEquals(Float.MAX_VALUE, halfMax - Math.ulp(halfMax) + halfMax); 1604 assertEquals(Float.POSITIVE_INFINITY, halfMax + halfMax); 1605 assertEquals(3.4028233E38f, Math.scalb(1.0f - Math.ulp(1.0f), 1606 Float.MAX_EXPONENT + 1)); 1607 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(1.0f - Math.ulp(1.0f), 1608 Float.MAX_EXPONENT + 2)); 1609 1610 halfMax = Math.scalb(-1.0f, Float.MAX_EXPONENT); 1611 assertEquals(-1.7014118E38f, halfMax); 1612 assertEquals(-Float.MAX_VALUE, halfMax + Math.ulp(halfMax) + halfMax); 1613 assertEquals(Float.NEGATIVE_INFINITY, halfMax + halfMax); 1614 1615 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(0.345f, 1234)); 1616 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(44.345E10f, 934)); 1617 assertEquals(Float.NEGATIVE_INFINITY, Math.scalb(-44.345E10f, 934)); 1618 1619 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(Float.MIN_NORMAL / 2, 1620 400)); 1621 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(Float.MIN_VALUE, 800)); 1622 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(Float.MAX_VALUE, 1)); 1623 assertEquals(Float.POSITIVE_INFINITY, Math.scalb( 1624 Float.POSITIVE_INFINITY, 0)); 1625 assertEquals(Float.POSITIVE_INFINITY, Math.scalb( 1626 Float.POSITIVE_INFINITY, -1)); 1627 assertEquals(Float.NEGATIVE_INFINITY, Math.scalb( 1628 Float.NEGATIVE_INFINITY, -1)); 1629 assertEquals(Float.NEGATIVE_INFINITY, Math.scalb( 1630 Float.NEGATIVE_INFINITY, Float.MIN_EXPONENT)); 1631 1632 // result is subnormal/zero 1633 int posZeroBits = Float.floatToIntBits(+0.0f); 1634 int negZeroBits = Float.floatToIntBits(-0.0f); 1635 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(+0.0f, 1636 Integer.MAX_VALUE))); 1637 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(+0.0f, -123))); 1638 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(+0.0f, 0))); 1639 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-0.0f, 123))); 1640 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-0.0f, 1641 Integer.MIN_VALUE))); 1642 1643 assertEquals(Float.MIN_VALUE, Math.scalb(1.0f, -149)); 1644 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(1.0f, -150))); 1645 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-1.0f, -150))); 1646 1647 // precision lost 1648 assertEquals(Math.scalb(21.405f, -154), Math.scalb(21.405f, -153)); 1649 assertEquals(Float.MIN_VALUE, Math.scalb(21.405f, -154)); 1650 assertEquals(-Float.MIN_VALUE, Math.scalb(-21.405f, -154)); 1651 assertEquals(posZeroBits, Float.floatToIntBits(Math 1652 .scalb(21.405f, -155))); 1653 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-21.405f, 1654 -155))); 1655 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1656 Float.MIN_VALUE, -1))); 1657 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1658 -Float.MIN_VALUE, -1))); 1659 assertEquals(Float.MIN_VALUE, Math.scalb(Float.MIN_NORMAL, -23)); 1660 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1661 Float.MIN_NORMAL, -24))); 1662 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1663 -Float.MIN_NORMAL, -24))); 1664 assertEquals(Float.MIN_VALUE, Math.scalb(Float.MAX_VALUE, -277)); 1665 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1666 Float.MAX_VALUE, -278))); 1667 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1668 -Float.MAX_VALUE, -278))); 1669 assertEquals(Float.MIN_VALUE, Math.scalb(Float.MIN_NORMAL / 3, -22)); 1670 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1671 Float.MIN_NORMAL / 3, -23))); 1672 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1673 -Float.MIN_NORMAL / 3, -23))); 1674 float subnormal = Math.scalb(Float.MIN_NORMAL / 3, -11); 1675 assertEquals(1.913E-42f, subnormal); 1676 // precision lost 1677 assertFalse(Float.MIN_NORMAL / 3 == Math.scalb(subnormal, 11)); 1678 1679 assertEquals(68747264, Float.floatToIntBits(Math.scalb( 1680 Float.MIN_VALUE * 153, 23))); 1681 assertEquals(-2078736384, Float.floatToIntBits(Math.scalb( 1682 -Float.MIN_VALUE * 153, 23))); 1683 1684 assertEquals(4896, Float.floatToIntBits(Math.scalb( 1685 Float.MIN_VALUE * 153, 5))); 1686 assertEquals(-2147478752, Float.floatToIntBits(Math.scalb( 1687 -Float.MIN_VALUE * 153, 5))); 1688 1689 // NaN 1690 assertTrue(Float.isNaN(Math.scalb(Float.NaN, 1))); 1691 assertTrue(Float.isNaN(Math.scalb(Float.NaN, 0))); 1692 assertTrue(Float.isNaN(Math.scalb(Float.NaN, -120))); 1693 1694 // test for exception 1695 try { 1696 Math.scalb((Float) null, (Integer) null); 1697 fail("Should throw NullPointerException"); 1698 } catch (NullPointerException e) { 1699 // Expected 1700 } 1701 try { 1702 Math.scalb(1.0f, (Integer) null); 1703 fail("Should throw NullPointerException"); 1704 } catch (NullPointerException e) { 1705 // Expected 1706 } 1707 try { 1708 Math.scalb((Float) null, 1); 1709 fail("Should throw NullPointerException"); 1710 } catch (NullPointerException e) { 1711 // Expected 1712 } 1713 1714 int b1em126 = 0x00800000; // bit representation of Float.MIN_NORMAL 1715 int b1em127 = 0x00400000; // bit representation of half 1716 // Float.MIN_NORMAL 1717 // assert exact identity 1718 assertEquals(b1em127, Float.floatToIntBits(Math.scalb(Float 1719 .intBitsToFloat(b1em126), -1))); 1720 } 1721 1722 /** 1723 * java.lang.Math#signum(double) 1724 */ test_signum_D()1725 public void test_signum_D() { 1726 assertTrue(Double.isNaN(Math.signum(Double.NaN))); 1727 assertTrue(Double.isNaN(Math.signum(Double.NaN))); 1728 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 1729 .signum(0.0))); 1730 assertEquals(Double.doubleToLongBits(+0.0), Double 1731 .doubleToLongBits(Math.signum(+0.0))); 1732 assertEquals(Double.doubleToLongBits(-0.0), Double 1733 .doubleToLongBits(Math.signum(-0.0))); 1734 1735 assertEquals(1.0, Math.signum(253681.2187962), 0D); 1736 assertEquals(-1.0, Math.signum(-125874693.56), 0D); 1737 assertEquals(1.0, Math.signum(1.2587E-308), 0D); 1738 assertEquals(-1.0, Math.signum(-1.2587E-308), 0D); 1739 1740 assertEquals(1.0, Math.signum(Double.MAX_VALUE), 0D); 1741 assertEquals(1.0, Math.signum(Double.MIN_VALUE), 0D); 1742 assertEquals(-1.0, Math.signum(-Double.MAX_VALUE), 0D); 1743 assertEquals(-1.0, Math.signum(-Double.MIN_VALUE), 0D); 1744 assertEquals(1.0, Math.signum(Double.POSITIVE_INFINITY), 0D); 1745 assertEquals(-1.0, Math.signum(Double.NEGATIVE_INFINITY), 0D); 1746 } 1747 1748 /** 1749 * java.lang.Math#signum(float) 1750 */ test_signum_F()1751 public void test_signum_F() { 1752 assertTrue(Float.isNaN(Math.signum(Float.NaN))); 1753 assertEquals(Float.floatToIntBits(0.0f), Float 1754 .floatToIntBits(Math.signum(0.0f))); 1755 assertEquals(Float.floatToIntBits(+0.0f), Float 1756 .floatToIntBits(Math.signum(+0.0f))); 1757 assertEquals(Float.floatToIntBits(-0.0f), Float 1758 .floatToIntBits(Math.signum(-0.0f))); 1759 1760 assertEquals(1.0f, Math.signum(253681.2187962f), 0f); 1761 assertEquals(-1.0f, Math.signum(-125874693.56f), 0f); 1762 assertEquals(1.0f, Math.signum(1.2587E-11f), 0f); 1763 assertEquals(-1.0f, Math.signum(-1.2587E-11f), 0f); 1764 1765 assertEquals(1.0f, Math.signum(Float.MAX_VALUE), 0f); 1766 assertEquals(1.0f, Math.signum(Float.MIN_VALUE), 0f); 1767 assertEquals(-1.0f, Math.signum(-Float.MAX_VALUE), 0f); 1768 assertEquals(-1.0f, Math.signum(-Float.MIN_VALUE), 0f); 1769 assertEquals(1.0f, Math.signum(Float.POSITIVE_INFINITY), 0f); 1770 assertEquals(-1.0f, Math.signum(Float.NEGATIVE_INFINITY), 0f); 1771 } 1772 1773 /** 1774 * java.lang.Math#sin(double) 1775 */ test_sinD()1776 public void test_sinD() { 1777 // Test for method double java.lang.Math.sin(double) 1778 assertEquals("Incorrect answer", 0.0, Math.sin(0), 0D); 1779 assertEquals("Incorrect answer", 0.8414709848078965, Math.sin(1), 0D); 1780 } 1781 1782 /** 1783 * java.lang.Math#sinh(double) 1784 */ test_sinh_D()1785 public void test_sinh_D() { 1786 // Test for special situations 1787 assertTrue(Double.isNaN(Math.sinh(Double.NaN))); 1788 assertEquals(Double.POSITIVE_INFINITY, Math.sinh(Double.POSITIVE_INFINITY), 0D); 1789 assertEquals(Double.NEGATIVE_INFINITY, Math.sinh(Double.NEGATIVE_INFINITY), 0D); 1790 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math.sinh(0.0))); 1791 assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math.sinh(+0.0))); 1792 assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math.sinh(-0.0))); 1793 1794 assertEquals(Double.POSITIVE_INFINITY, Math.sinh(1234.56), 0D); 1795 assertEquals(Double.NEGATIVE_INFINITY, Math.sinh(-1234.56), 0D); 1796 assertEquals(1.0000000000001666E-6, Math.sinh(0.000001), 0D); 1797 assertEquals(-1.0000000000001666E-6, Math.sinh(-0.000001), 0D); 1798 assertEquals(5.115386441963859, Math.sinh(2.33482), Math.ulp(5.115386441963859)); 1799 assertEquals(Double.POSITIVE_INFINITY, Math.sinh(Double.MAX_VALUE), 0D); 1800 assertEquals(4.9E-324, Math.sinh(Double.MIN_VALUE), 0D); 1801 } 1802 1803 /** 1804 * java.lang.Math#sqrt(double) 1805 */ test_sqrtD()1806 public void test_sqrtD() { 1807 // Test for method double java.lang.Math.sqrt(double) 1808 assertEquals("Incorrect root returned2", 7, Math.sqrt(49), 0); 1809 } 1810 1811 /** 1812 * java.lang.Math#tan(double) 1813 */ test_tanD()1814 public void test_tanD() { 1815 // Test for method double java.lang.Math.tan(double) 1816 assertEquals("Incorrect answer", 0.0, Math.tan(0), 0D); 1817 assertEquals("Incorrect answer", 1.5574077246549023, Math.tan(1), 0D); 1818 1819 } 1820 1821 /** 1822 * java.lang.Math#tanh(double) 1823 */ test_tanh_D()1824 public void test_tanh_D() { 1825 // Test for special situations 1826 assertTrue("Should return NaN", Double.isNaN(Math.tanh(Double.NaN))); 1827 assertEquals("Should return +1.0", +1.0, Math 1828 .tanh(Double.POSITIVE_INFINITY), 0D); 1829 assertEquals("Should return -1.0", -1.0, Math 1830 .tanh(Double.NEGATIVE_INFINITY), 0D); 1831 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 1832 .tanh(0.0))); 1833 assertEquals(Double.doubleToLongBits(+0.0), Double 1834 .doubleToLongBits(Math.tanh(+0.0))); 1835 assertEquals(Double.doubleToLongBits(-0.0), Double 1836 .doubleToLongBits(Math.tanh(-0.0))); 1837 1838 assertEquals("Should return 1.0", 1.0, Math.tanh(1234.56), 0D); 1839 assertEquals("Should return -1.0", -1.0, Math.tanh(-1234.56), 0D); 1840 assertEquals("Should return 9.999999999996666E-7", 1841 9.999999999996666E-7, Math.tanh(0.000001), 0D); 1842 assertEquals("Should return 0.981422884124941", 0.981422884124941, Math 1843 .tanh(2.33482), 0D); 1844 assertEquals("Should return 1.0", 1.0, Math.tanh(Double.MAX_VALUE), 0D); 1845 assertEquals("Should return 4.9E-324", 4.9E-324, Math 1846 .tanh(Double.MIN_VALUE), 0D); 1847 } 1848 1849 /** 1850 * java.lang.Math#random() 1851 */ test_random()1852 public void test_random() { 1853 // There isn't a place for these tests so just stick them here 1854 assertEquals("Wrong value E", 1855 4613303445314885481L, Double.doubleToLongBits(Math.E)); 1856 assertEquals("Wrong value PI", 1857 4614256656552045848L, Double.doubleToLongBits(Math.PI)); 1858 1859 for (int i = 500; i >= 0; i--) { 1860 double d = Math.random(); 1861 assertTrue("Generated number is out of range: " + d, d >= 0.0 1862 && d < 1.0); 1863 } 1864 } 1865 1866 /** 1867 * java.lang.Math#toRadians(double) 1868 */ test_toRadiansD()1869 public void test_toRadiansD() { 1870 for (double d = 500; d >= 0; d -= 1.0) { 1871 double converted = Math.toDegrees(Math.toRadians(d)); 1872 assertTrue("Converted number not equal to original. d = " + d, 1873 converted >= d * 0.99999999 && converted <= d * 1.00000001); 1874 } 1875 } 1876 1877 /** 1878 * java.lang.Math#toDegrees(double) 1879 */ test_toDegreesD()1880 public void test_toDegreesD() { 1881 for (double d = 500; d >= 0; d -= 1.0) { 1882 double converted = Math.toRadians(Math.toDegrees(d)); 1883 assertTrue("Converted number not equal to original. d = " + d, 1884 converted >= d * 0.99999999 && converted <= d * 1.00000001); 1885 } 1886 } 1887 1888 /** 1889 * java.lang.Math#ulp(double) 1890 */ 1891 @SuppressWarnings("boxing") test_ulp_D()1892 public void test_ulp_D() { 1893 // Test for special cases 1894 assertTrue("Should return NaN", Double.isNaN(Math.ulp(Double.NaN))); 1895 assertEquals("Returned incorrect value", Double.POSITIVE_INFINITY, Math 1896 .ulp(Double.POSITIVE_INFINITY), 0D); 1897 assertEquals("Returned incorrect value", Double.POSITIVE_INFINITY, Math 1898 .ulp(Double.NEGATIVE_INFINITY), 0D); 1899 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1900 .ulp(0.0), 0D); 1901 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1902 .ulp(+0.0), 0D); 1903 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1904 .ulp(-0.0), 0D); 1905 assertEquals("Returned incorrect value", Math.pow(2, 971), Math 1906 .ulp(Double.MAX_VALUE), 0D); 1907 assertEquals("Returned incorrect value", Math.pow(2, 971), Math 1908 .ulp(-Double.MAX_VALUE), 0D); 1909 1910 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1911 .ulp(Double.MIN_VALUE), 0D); 1912 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1913 .ulp(-Double.MIN_VALUE), 0D); 1914 1915 assertEquals("Returned incorrect value", 2.220446049250313E-16, Math 1916 .ulp(1.0), 0D); 1917 assertEquals("Returned incorrect value", 2.220446049250313E-16, Math 1918 .ulp(-1.0), 0D); 1919 assertEquals("Returned incorrect value", 2.2737367544323206E-13, Math 1920 .ulp(1153.0), 0D); 1921 } 1922 1923 /** 1924 * java.lang.Math#ulp(float) 1925 */ 1926 @SuppressWarnings("boxing") test_ulp_f()1927 public void test_ulp_f() { 1928 // Test for special cases 1929 assertTrue("Should return NaN", Float.isNaN(Math.ulp(Float.NaN))); 1930 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, Math 1931 .ulp(Float.POSITIVE_INFINITY), 0f); 1932 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, Math 1933 .ulp(Float.NEGATIVE_INFINITY), 0f); 1934 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 1935 .ulp(0.0f), 0f); 1936 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 1937 .ulp(+0.0f), 0f); 1938 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 1939 .ulp(-0.0f), 0f); 1940 assertEquals("Returned incorrect value", 2.028241E31f, Math 1941 .ulp(Float.MAX_VALUE), 0f); 1942 assertEquals("Returned incorrect value", 2.028241E31f, Math 1943 .ulp(-Float.MAX_VALUE), 0f); 1944 1945 assertEquals("Returned incorrect value", 1.4E-45f, Math 1946 .ulp(Float.MIN_VALUE), 0f); 1947 assertEquals("Returned incorrect value", 1.4E-45f, Math 1948 .ulp(-Float.MIN_VALUE), 0f); 1949 1950 assertEquals("Returned incorrect value", 1.1920929E-7f, Math.ulp(1.0f), 1951 0f); 1952 assertEquals("Returned incorrect value", 1.1920929E-7f, 1953 Math.ulp(-1.0f), 0f); 1954 assertEquals("Returned incorrect value", 1.2207031E-4f, Math 1955 .ulp(1153.0f), 0f); 1956 assertEquals("Returned incorrect value", 5.6E-45f, Math 1957 .ulp(9.403954E-38f), 0f); 1958 } 1959 1960 /** 1961 * {@link java.lang.Math#shiftIntBits(int, int)} 1962 * @since 1.6 1963 */ test_shiftIntBits_II()1964 public void test_shiftIntBits_II() { 1965 class Tuple { 1966 public int result; 1967 1968 public int value; 1969 1970 public int factor; 1971 1972 public Tuple(int result, int value, int factor) { 1973 this.result = result; 1974 this.value = value; 1975 this.factor = factor; 1976 } 1977 } 1978 final Tuple[] TUPLES = new Tuple[] { 1979 // sub-normal to sub-normal 1980 new Tuple(0x00000000, 0x00000001, -1), 1981 // round to even 1982 new Tuple(0x00000002, 0x00000003, -1), 1983 // round to even 1984 new Tuple(0x00000001, 0x00000005, -3), 1985 // round to infinity 1986 new Tuple(0x00000002, 0x0000000d, -3), 1987 // round to infinity 1988 1989 // normal to sub-normal 1990 new Tuple(0x00000002, 0x01a00000, -24), 1991 // round to even 1992 new Tuple(0x00000004, 0x01e00000, -24), 1993 // round to even 1994 new Tuple(0x00000003, 0x01c80000, -24), 1995 // round to infinity 1996 new Tuple(0x00000004, 0x01e80000, -24), 1997 // round to infinity 1998 }; 1999 for (int i = 0; i < TUPLES.length; ++i) { 2000 Tuple tuple = TUPLES[i]; 2001 assertEquals(tuple.result, Float.floatToIntBits(Math.scalb(Float 2002 .intBitsToFloat(tuple.value), tuple.factor))); 2003 assertEquals(tuple.result, Float.floatToIntBits(-Math.scalb(-Float 2004 .intBitsToFloat(tuple.value), tuple.factor))); 2005 } 2006 } 2007 2008 /** 2009 * {@link java.lang.Math#shiftLongBits(long, long)} 2010 * <p/> 2011 * Round result to nearest value on precision lost. 2012 * @since 1.6 2013 */ test_shiftLongBits_LL()2014 public void test_shiftLongBits_LL() { 2015 class Tuple { 2016 public long result; 2017 2018 public long value; 2019 2020 public int factor; 2021 2022 public Tuple(long result, long value, int factor) { 2023 this.result = result; 2024 this.value = value; 2025 this.factor = factor; 2026 } 2027 } 2028 final Tuple[] TUPLES = new Tuple[] { 2029 // sub-normal to sub-normal 2030 new Tuple(0x00000000L, 0x00000001L, -1), 2031 //round to even 2032 new Tuple(0x00000002L, 0x00000003L, -1), 2033 //round to even 2034 new Tuple(0x00000001L, 0x00000005L, -3), 2035 //round to infinity 2036 new Tuple(0x00000002L, 0x0000000dL, -3), 2037 //round to infinity 2038 2039 // normal to sub-normal 2040 new Tuple(0x0000000000000002L, 0x0034000000000000L, -53), // round to even 2041 new Tuple(0x0000000000000004L, 0x003c000000000000L, -53), // round to even 2042 new Tuple(0x0000000000000003L, 0x0035000000000000L, -53), // round to infinity 2043 new Tuple(0x0000000000000004L, 0x003d000000000000L, -53), // round to infinity 2044 }; 2045 for (int i = 0; i < TUPLES.length; ++i) { 2046 Tuple tuple = TUPLES[i]; 2047 assertEquals(tuple.result, Double.doubleToLongBits(Math.scalb( 2048 Double.longBitsToDouble(tuple.value), tuple.factor))); 2049 assertEquals(tuple.result, Double.doubleToLongBits(-Math.scalb( 2050 -Double.longBitsToDouble(tuple.value), tuple.factor))); 2051 } 2052 } 2053 } 2054