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 606 /** 607 * java.lang.Math#max(double, double) 608 */ test_maxDD()609 public void test_maxDD() { 610 // Test for method double java.lang.Math.max(double, double) 611 assertEquals("Incorrect double max value", 1908897.6000089, Math.max(-1908897.6000089, 612 1908897.6000089), 0D); 613 assertEquals("Incorrect double max value", 614 1908897.6000089, Math.max(2.0, 1908897.6000089), 0D); 615 assertEquals("Incorrect double max value", -2.0, Math.max(-2.0, 616 -1908897.6000089), 0D); 617 618 // Compare toString representations here since -0.0 = +0.0, and 619 // NaN != NaN and we need to distinguish 620 assertEquals("Max failed for NaN", 621 Double.toString(Double.NaN), Double.toString(Math.max(Double.NaN, 42.0d))); 622 assertEquals("Max failed for NaN", 623 Double.toString(Double.NaN), Double.toString(Math.max(42.0d, Double.NaN))); 624 assertEquals("Max failed for 0.0", 625 Double.toString(+0.0d), Double.toString(Math.max(+0.0d, -0.0d))); 626 assertEquals("Max failed for 0.0", 627 Double.toString(+0.0d), Double.toString(Math.max(-0.0d, +0.0d))); 628 assertEquals("Max failed for -0.0d", 629 Double.toString(-0.0d), Double.toString(Math.max(-0.0d, -0.0d))); 630 assertEquals("Max failed for 0.0", 631 Double.toString(+0.0d), Double.toString(Math.max(+0.0d, +0.0d))); 632 } 633 634 /** 635 * java.lang.Math#max(float, float) 636 */ test_maxFF()637 public void test_maxFF() { 638 // Test for method float java.lang.Math.max(float, float) 639 assertTrue("Incorrect float max value", Math.max(-1908897.600f, 640 1908897.600f) == 1908897.600f); 641 assertTrue("Incorrect float max value", 642 Math.max(2.0f, 1908897.600f) == 1908897.600f); 643 assertTrue("Incorrect float max value", 644 Math.max(-2.0f, -1908897.600f) == -2.0f); 645 646 // Compare toString representations here since -0.0 = +0.0, and 647 // NaN != NaN and we need to distinguish 648 assertEquals("Max failed for NaN", 649 Float.toString(Float.NaN), Float.toString(Math.max(Float.NaN, 42.0f))); 650 assertEquals("Max failed for NaN", 651 Float.toString(Float.NaN), Float.toString(Math.max(42.0f, Float.NaN))); 652 assertEquals("Max failed for 0.0", 653 Float.toString(+0.0f), Float.toString(Math.max(+0.0f, -0.0f))); 654 assertEquals("Max failed for 0.0", 655 Float.toString(+0.0f), Float.toString(Math.max(-0.0f, +0.0f))); 656 assertEquals("Max failed for -0.0f", 657 Float.toString(-0.0f), Float.toString(Math.max(-0.0f, -0.0f))); 658 assertEquals("Max failed for 0.0", 659 Float.toString(+0.0f), Float.toString(Math.max(+0.0f, +0.0f))); 660 } 661 662 /** 663 * java.lang.Math#max(int, int) 664 */ test_maxII()665 public void test_maxII() { 666 // Test for method int java.lang.Math.max(int, int) 667 assertEquals("Incorrect int max value", 668 19088976, Math.max(-19088976, 19088976)); 669 assertEquals("Incorrect int max value", 670 19088976, Math.max(20, 19088976)); 671 assertEquals("Incorrect int max value", -20, Math.max(-20, -19088976)); 672 } 673 674 /** 675 * java.lang.Math#max(long, long) 676 */ test_maxJJ()677 public void test_maxJJ() { 678 // Test for method long java.lang.Math.max(long, long) 679 assertEquals("Incorrect long max value", 19088976000089L, Math.max(-19088976000089L, 680 19088976000089L)); 681 assertEquals("Incorrect long max value", 682 19088976000089L, Math.max(20, 19088976000089L)); 683 assertEquals("Incorrect long max value", 684 -20, Math.max(-20, -19088976000089L)); 685 } 686 687 /** 688 * java.lang.Math#min(double, double) 689 */ test_minDD()690 public void test_minDD() { 691 // Test for method double java.lang.Math.min(double, double) 692 assertEquals("Incorrect double min value", -1908897.6000089, Math.min(-1908897.6000089, 693 1908897.6000089), 0D); 694 assertEquals("Incorrect double min value", 695 2.0, Math.min(2.0, 1908897.6000089), 0D); 696 assertEquals("Incorrect double min value", -1908897.6000089, Math.min(-2.0, 697 -1908897.6000089), 0D); 698 assertEquals("Incorrect double min value", 1.0d, Math.min(1.0d, 1.0d)); 699 700 // Compare toString representations here since -0.0 = +0.0, and 701 // NaN != NaN and we need to distinguish 702 assertEquals("Min failed for NaN", 703 Double.toString(Double.NaN), Double.toString(Math.min(Double.NaN, 42.0d))); 704 assertEquals("Min failed for NaN", 705 Double.toString(Double.NaN), Double.toString(Math.min(42.0d, Double.NaN))); 706 assertEquals("Min failed for -0.0", 707 Double.toString(-0.0d), Double.toString(Math.min(+0.0d, -0.0d))); 708 assertEquals("Min failed for -0.0", 709 Double.toString(-0.0d), Double.toString(Math.min(-0.0d, +0.0d))); 710 assertEquals("Min failed for -0.0d", 711 Double.toString(-0.0d), Double.toString(Math.min(-0.0d, -0.0d))); 712 assertEquals("Min failed for 0.0", 713 Double.toString(+0.0d), Double.toString(Math.min(+0.0d, +0.0d))); 714 } 715 716 /** 717 * java.lang.Math#min(float, float) 718 */ test_minFF()719 public void test_minFF() { 720 // Test for method float java.lang.Math.min(float, float) 721 assertTrue("Incorrect float min value", Math.min(-1908897.600f, 722 1908897.600f) == -1908897.600f); 723 assertTrue("Incorrect float min value", 724 Math.min(2.0f, 1908897.600f) == 2.0f); 725 assertTrue("Incorrect float min value", 726 Math.min(-2.0f, -1908897.600f) == -1908897.600f); 727 assertEquals("Incorrect float min value", 1.0f, Math.min(1.0f, 1.0f)); 728 729 // Compare toString representations here since -0.0 = +0.0, and 730 // NaN != NaN and we need to distinguish 731 assertEquals("Min failed for NaN", 732 Float.toString(Float.NaN), Float.toString(Math.min(Float.NaN, 42.0f))); 733 assertEquals("Min failed for NaN", 734 Float.toString(Float.NaN), Float.toString(Math.min(42.0f, Float.NaN))); 735 assertEquals("Min failed for -0.0", 736 Float.toString(-0.0f), Float.toString(Math.min(+0.0f, -0.0f))); 737 assertEquals("Min failed for -0.0", 738 Float.toString(-0.0f), Float.toString(Math.min(-0.0f, +0.0f))); 739 assertEquals("Min failed for -0.0f", 740 Float.toString(-0.0f), Float.toString(Math.min(-0.0f, -0.0f))); 741 assertEquals("Min failed for 0.0", 742 Float.toString(+0.0f), Float.toString(Math.min(+0.0f, +0.0f))); 743 } 744 745 /** 746 * java.lang.Math#min(int, int) 747 */ test_minII()748 public void test_minII() { 749 // Test for method int java.lang.Math.min(int, int) 750 assertEquals("Incorrect int min value", 751 -19088976, Math.min(-19088976, 19088976)); 752 assertEquals("Incorrect int min value", 20, Math.min(20, 19088976)); 753 assertEquals("Incorrect int min value", 754 -19088976, Math.min(-20, -19088976)); 755 756 } 757 758 /** 759 * java.lang.Math#min(long, long) 760 */ test_minJJ()761 public void test_minJJ() { 762 // Test for method long java.lang.Math.min(long, long) 763 assertEquals("Incorrect long min value", -19088976000089L, Math.min(-19088976000089L, 764 19088976000089L)); 765 assertEquals("Incorrect long min value", 766 20, Math.min(20, 19088976000089L)); 767 assertEquals("Incorrect long min value", 768 -19088976000089L, Math.min(-20, -19088976000089L)); 769 } 770 771 /** 772 * start number cases for test_nextAfter_DD in MathTest/StrictMathTest 773 * NEXTAFTER_DD_START_CASES[i][0] is the start number 774 * NEXTAFTER_DD_START_CASES[i][1] is the nextUp of start number 775 * NEXTAFTER_DD_START_CASES[i][2] is the nextDown of start number 776 */ 777 static final double NEXTAFTER_DD_START_CASES[][] = new double[][] { 778 { 3.4, 3.4000000000000004, 3.3999999999999995 }, 779 { -3.4, -3.3999999999999995, -3.4000000000000004 }, 780 { 3.4233E109, 3.4233000000000005E109, 3.4232999999999996E109 }, 781 { -3.4233E109, -3.4232999999999996E109, -3.4233000000000005E109 }, 782 { +0.0, Double.MIN_VALUE, -Double.MIN_VALUE }, 783 { 0.0, Double.MIN_VALUE, -Double.MIN_VALUE }, 784 { -0.0, Double.MIN_VALUE, -Double.MIN_VALUE }, 785 { Double.MIN_VALUE, 1.0E-323, +0.0 }, 786 { -Double.MIN_VALUE, -0.0, -1.0E-323 }, 787 { Double.MIN_NORMAL, 2.225073858507202E-308, 2.225073858507201E-308 }, 788 { -Double.MIN_NORMAL, -2.225073858507201E-308, 789 -2.225073858507202E-308 }, 790 { Double.MAX_VALUE, Double.POSITIVE_INFINITY, 791 1.7976931348623155E308 }, 792 { -Double.MAX_VALUE, -1.7976931348623155E308, 793 Double.NEGATIVE_INFINITY }, 794 { Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 795 Double.MAX_VALUE }, 796 { Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, 797 Double.NEGATIVE_INFINITY } }; 798 799 /** 800 * direction number cases for test_nextAfter_DD/test_nextAfter_FD in 801 * MathTest/StrictMathTest 802 */ 803 static final double NEXTAFTER_DD_FD_DIRECTION_CASES[] = new double[] { 804 Double.POSITIVE_INFINITY, Double.MAX_VALUE, 8.8, 3.4, 1.4, 805 Double.MIN_NORMAL, Double.MIN_NORMAL / 2, Double.MIN_VALUE, +0.0, 806 0.0, -0.0, -Double.MIN_VALUE, -Double.MIN_NORMAL / 2, 807 -Double.MIN_NORMAL, -1.4, -3.4, -8.8, -Double.MAX_VALUE, 808 Double.NEGATIVE_INFINITY }; 809 810 /** 811 * {@link java.lang.Math#nextAfter(double, double)} 812 * @since 1.6 813 */ 814 @SuppressWarnings("boxing") test_nextAfter_DD()815 public void test_nextAfter_DD() { 816 // test for most cases without exception 817 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 818 final double start = NEXTAFTER_DD_START_CASES[i][0]; 819 final long nextUpBits = Double 820 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][1]); 821 final long nextDownBits = Double 822 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][2]); 823 824 for (int j = 0; j < NEXTAFTER_DD_FD_DIRECTION_CASES.length; j++) { 825 final double direction = NEXTAFTER_DD_FD_DIRECTION_CASES[j]; 826 final long resultBits = Double.doubleToLongBits(Math.nextAfter( 827 start, direction)); 828 final long directionBits = Double.doubleToLongBits(direction); 829 if (direction > start) { 830 assertEquals("Result should be next up-number.", 831 nextUpBits, resultBits); 832 } else if (direction < start) { 833 assertEquals("Result should be next down-number.", 834 nextDownBits, resultBits); 835 } else { 836 assertEquals("Result should be direction.", directionBits, 837 resultBits); 838 } 839 } 840 } 841 842 // test for cases with NaN 843 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 844 assertTrue("The result should be NaN.", Double.isNaN(Math 845 .nextAfter(NEXTAFTER_DD_START_CASES[i][0], Double.NaN))); 846 } 847 for (int i = 0; i < NEXTAFTER_DD_FD_DIRECTION_CASES.length; i++) { 848 assertTrue("The result should be NaN.", Double.isNaN(Math 849 .nextAfter(Double.NaN, NEXTAFTER_DD_FD_DIRECTION_CASES[i]))); 850 } 851 assertTrue("The result should be NaN.", Double.isNaN(Math.nextAfter( 852 Double.NaN, Double.NaN))); 853 854 // test for exception 855 try { 856 Math.nextAfter((Double) null, 2.3); 857 fail("Should throw NullPointerException"); 858 } catch (NullPointerException e) { 859 // Expected 860 } 861 try { 862 Math.nextAfter(2.3, (Double) null); 863 fail("Should throw NullPointerException"); 864 } catch (NullPointerException e) { 865 // Expected 866 } 867 try { 868 Math.nextAfter((Double) null, (Double) null); 869 fail("Should throw NullPointerException"); 870 } catch (NullPointerException e) { 871 // Expected 872 } 873 } 874 875 /** 876 * start number cases for test_nextAfter_FD in MathTest/StrictMathTest 877 * NEXTAFTER_FD_START_CASES[i][0] is the start number 878 * NEXTAFTER_FD_START_CASES[i][1] is the nextUp of start number 879 * NEXTAFTER_FD_START_CASES[i][2] is the nextDown of start number 880 */ 881 static final float NEXTAFTER_FD_START_CASES[][] = new float[][] { 882 { 3.4f, 3.4000003f, 3.3999999f }, 883 { -3.4f, -3.3999999f, -3.4000003f }, 884 { 3.4233E19f, 3.4233002E19f, 3.4232998E19f }, 885 { -3.4233E19f, -3.4232998E19f, -3.4233002E19f }, 886 { +0.0f, Float.MIN_VALUE, -Float.MIN_VALUE }, 887 { 0.0f, Float.MIN_VALUE, -Float.MIN_VALUE }, 888 { -0.0f, Float.MIN_VALUE, -Float.MIN_VALUE }, 889 { Float.MIN_VALUE, 2.8E-45f, +0.0f }, 890 { -Float.MIN_VALUE, -0.0f, -2.8E-45f }, 891 { Float.MIN_NORMAL, 1.1754945E-38f, 1.1754942E-38f }, 892 { -Float.MIN_NORMAL, -1.1754942E-38f, -1.1754945E-38f }, 893 { Float.MAX_VALUE, Float.POSITIVE_INFINITY, 3.4028233E38f }, 894 { -Float.MAX_VALUE, -3.4028233E38f, Float.NEGATIVE_INFINITY }, 895 { Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.MAX_VALUE }, 896 { Float.NEGATIVE_INFINITY, -Float.MAX_VALUE, 897 Float.NEGATIVE_INFINITY } }; 898 899 /** 900 * {@link java.lang.Math#nextAfter(float, double)} 901 * @since 1.6 902 */ 903 @SuppressWarnings("boxing") test_nextAfter_FD()904 public void test_nextAfter_FD() { 905 // test for most cases without exception 906 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 907 final float start = NEXTAFTER_FD_START_CASES[i][0]; 908 final int nextUpBits = Float 909 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][1]); 910 final int nextDownBits = Float 911 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][2]); 912 913 for (int j = 0; j < NEXTAFTER_DD_FD_DIRECTION_CASES.length; j++) { 914 final double direction = NEXTAFTER_DD_FD_DIRECTION_CASES[j]; 915 final int resultBits = Float.floatToIntBits(Math.nextAfter( 916 start, direction)); 917 if (direction > start) { 918 assertEquals("Result should be next up-number.", 919 nextUpBits, resultBits); 920 } else if (direction < start) { 921 assertEquals("Result should be next down-number.", 922 nextDownBits, resultBits); 923 } else { 924 final int equivalentBits = Float.floatToIntBits(new Float( 925 direction)); 926 assertEquals( 927 "Result should be a number equivalent to direction.", 928 equivalentBits, resultBits); 929 } 930 } 931 } 932 933 // test for cases with NaN 934 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 935 assertTrue("The result should be NaN.", Float.isNaN(Math.nextAfter( 936 NEXTAFTER_FD_START_CASES[i][0], Float.NaN))); 937 } 938 for (int i = 0; i < NEXTAFTER_DD_FD_DIRECTION_CASES.length; i++) { 939 assertTrue("The result should be NaN.", Float.isNaN(Math.nextAfter( 940 Float.NaN, NEXTAFTER_DD_FD_DIRECTION_CASES[i]))); 941 } 942 assertTrue("The result should be NaN.", Float.isNaN(Math.nextAfter( 943 Float.NaN, Float.NaN))); 944 945 // test for exception 946 try { 947 Math.nextAfter((Float) null, 2.3); 948 fail("Should throw NullPointerException"); 949 } catch (NullPointerException e) { 950 // Expected 951 } 952 try { 953 Math.nextAfter(2.3, (Float) null); 954 fail("Should throw NullPointerException"); 955 } catch (NullPointerException e) { 956 // Expected 957 } 958 try { 959 Math.nextAfter((Float) null, (Float) null); 960 fail("Should throw NullPointerException"); 961 } catch (NullPointerException e) { 962 // Expected 963 } 964 } 965 966 /** 967 * {@link java.lang.Math#nextUp(double)} 968 * @since 1.6 969 */ 970 @SuppressWarnings("boxing") test_nextUp_D()971 public void test_nextUp_D() { 972 // This method is semantically equivalent to nextAfter(d, 973 // Double.POSITIVE_INFINITY), 974 // so we use the data of test_nextAfter_DD 975 for (int i = 0; i < NEXTAFTER_DD_START_CASES.length; i++) { 976 final double start = NEXTAFTER_DD_START_CASES[i][0]; 977 final long nextUpBits = Double 978 .doubleToLongBits(NEXTAFTER_DD_START_CASES[i][1]); 979 final long resultBits = Double.doubleToLongBits(Math.nextUp(start)); 980 assertEquals("Result should be next up-number.", nextUpBits, 981 resultBits); 982 } 983 984 // test for cases with NaN 985 assertTrue("The result should be NaN.", Double.isNaN(Math 986 .nextUp(Double.NaN))); 987 988 // test for exception 989 try { 990 Math.nextUp((Double) null); 991 fail("Should throw NullPointerException"); 992 } catch (NullPointerException e) { 993 // Expected 994 } 995 } 996 997 /** 998 * {@link java.lang.Math#nextUp(float)} 999 * @since 1.6 1000 */ 1001 @SuppressWarnings("boxing") test_nextUp_F()1002 public void test_nextUp_F() { 1003 // This method is semantically equivalent to nextAfter(f, 1004 // Float.POSITIVE_INFINITY), 1005 // so we use the data of test_nextAfter_FD 1006 for (int i = 0; i < NEXTAFTER_FD_START_CASES.length; i++) { 1007 final float start = NEXTAFTER_FD_START_CASES[i][0]; 1008 final int nextUpBits = Float 1009 .floatToIntBits(NEXTAFTER_FD_START_CASES[i][1]); 1010 final int resultBits = Float.floatToIntBits(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.", Float.isNaN(Math 1017 .nextUp(Float.NaN))); 1018 1019 // test for exception 1020 try { 1021 Math.nextUp((Float) null); 1022 fail("Should throw NullPointerException"); 1023 } catch (NullPointerException e) { 1024 // Expected 1025 } 1026 } 1027 1028 /** 1029 * java.lang.Math#pow(double, double) 1030 */ test_powDD()1031 public void test_powDD() { 1032 // Test for method double java.lang.Math.pow(double, double) 1033 double NZERO = longTodouble(doubleTolong(0.0) ^ 0x8000000000000000L); 1034 double p1 = 1.0; 1035 double p2 = 2.0; 1036 double p3 = 3.0; 1037 double p4 = 4.0; 1038 double p5 = 5.0; 1039 double p6 = 6.0; 1040 double p7 = 7.0; 1041 double p8 = 8.0; 1042 double p9 = 9.0; 1043 double p10 = 10.0; 1044 double p11 = 11.0; 1045 double p12 = 12.0; 1046 double p13 = 13.0; 1047 double p14 = 14.0; 1048 double p15 = 15.0; 1049 double p16 = 16.0; 1050 double[] values = { p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, 1051 p13, p14, p15, p16 }; 1052 1053 for (int x = 0; x < values.length; x++) { 1054 double dval = values[x]; 1055 double nagateDval = negateDouble(dval); 1056 if (nagateDval == Double.NaN) { 1057 continue; 1058 } 1059 1060 // If the second argument is positive or negative zero, then the 1061 // result is 1.0. 1062 assertEquals("Result should be Math.pow(" + dval 1063 + ",-0.0)=+1.0", 1.0, Math.pow(dval, NZERO)); 1064 assertEquals("Result should be Math.pow(" + nagateDval 1065 + ",-0.0)=+1.0", 1.0, Math.pow(nagateDval, NZERO)); 1066 assertEquals("Result should be Math.pow(" + dval 1067 + ",+0.0)=+1.0", 1.0, Math.pow(dval, +0.0)); 1068 assertEquals("Result should be Math.pow(" + nagateDval 1069 + ",+0.0)=+1.0", 1.0, Math.pow(nagateDval, +0.0)); 1070 1071 // If the second argument is 1.0, then the result is the same as the 1072 // first argument. 1073 assertEquals("Result should be Math.pow(" + dval + "," + 1.0 + ")=" 1074 + dval, dval, Math.pow(dval, 1.0)); 1075 assertEquals("Result should be Math.pow(" + nagateDval + "," + 1.0 1076 + ")=" + nagateDval, nagateDval, Math.pow(nagateDval, 1.0)); 1077 1078 // If the second argument is NaN, then the result is NaN. 1079 assertEquals("Result should be Math.pow(" + dval + "," + Double.NaN 1080 + ")=" + Double.NaN, Double.NaN, Math.pow(dval, Double.NaN)); 1081 assertEquals("Result should be Math.pow(" + nagateDval + "," 1082 + Double.NaN + ")=" + Double.NaN, Double.NaN, Math.pow(nagateDval, 1083 Double.NaN)); 1084 1085 if (dval > 1) { 1086 // If the first argument is NaN and the second argument is 1087 // nonzero, 1088 // then the result is NaN. 1089 assertEquals("Result should be Math.pow(" + Double.NaN + "," 1090 + dval + ")=" + Double.NaN, Double.NaN, Math.pow(Double.NaN, dval)); 1091 assertEquals("Result should be Math.pow(" + Double.NaN + "," 1092 + nagateDval + ")=" + Double.NaN, Double.NaN, Math.pow(Double.NaN, 1093 nagateDval)); 1094 1095 /* 1096 * If the first argument is positive zero and the second 1097 * argument is greater than zero, or the first argument is 1098 * positive infinity and the second argument is less than zero, 1099 * then the result is positive zero. 1100 */ 1101 assertEquals("Result should be Math.pow(" + 0.0 + "," + dval 1102 + ")=" + 0.0, +0.0, Math.pow(0.0, dval)); 1103 assertEquals("Result should be Math.pow(" 1104 + Double.POSITIVE_INFINITY + "," + nagateDval + ")=" 1105 + 0.0, +0.0, Math.pow(Double.POSITIVE_INFINITY, nagateDval)); 1106 1107 /* 1108 * If the first argument is positive zero and the second 1109 * argument is less than zero, or the first argument is positive 1110 * infinity and the second argument is greater than zero, then 1111 * the result is positive infinity. 1112 */ 1113 assertEquals("Result should be Math.pow(" + 0.0 + "," 1114 + nagateDval + ")=" + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1115 Math.pow(0.0, nagateDval)); 1116 assertEquals("Result should be Math.pow(" 1117 + Double.POSITIVE_INFINITY + "," + dval + ")=" 1118 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow( 1119 Double.POSITIVE_INFINITY, dval)); 1120 1121 // Not a finite odd integer 1122 if (dval % 2 == 0) { 1123 /* 1124 * If the first argument is negative zero and the second 1125 * argument is greater than zero but not a finite odd 1126 * integer, or the first argument is negative infinity and 1127 * the second argument is less than zero but not a finite 1128 * odd integer, then the result is positive zero. 1129 */ 1130 assertEquals("Result should be Math.pow(" + NZERO + "," 1131 + dval + ")=" + 0.0, +0.0, Math.pow(NZERO, dval)); 1132 assertEquals("Result should be Math.pow(" 1133 + Double.NEGATIVE_INFINITY + "," + nagateDval 1134 + ")=" + 0.0, +0.0, Math.pow(Double.NEGATIVE_INFINITY, 1135 nagateDval)); 1136 1137 /* 1138 * If the first argument is negative zero and the second 1139 * argument is less than zero but not a finite odd integer, 1140 * or the first argument is negative infinity and the second 1141 * argument is greater than zero but not a finite odd 1142 * integer, then the result is positive infinity. 1143 */ 1144 assertEquals("Result should be Math.pow(" + NZERO + "," 1145 + nagateDval + ")=" + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1146 Math.pow(NZERO, nagateDval)); 1147 assertEquals("Result should be Math.pow(" 1148 + Double.NEGATIVE_INFINITY + "," + dval + ")=" 1149 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow( 1150 Double.NEGATIVE_INFINITY, dval)); 1151 } 1152 1153 // finite odd integer 1154 if (dval % 2 != 0) { 1155 /* 1156 * If the first argument is negative zero and the second 1157 * argument is a positive finite odd integer, or the first 1158 * argument is negative infinity and the second argument is 1159 * a negative finite odd integer, then the result is 1160 * negative zero. 1161 */ 1162 assertEquals("Result should be Math.pow(" + NZERO + "," 1163 + dval + ")=" + NZERO, NZERO, Math.pow(NZERO, dval)); 1164 assertEquals("Result should be Math.pow(" 1165 + Double.NEGATIVE_INFINITY + "," + nagateDval 1166 + ")=" + NZERO, NZERO, Math.pow(Double.NEGATIVE_INFINITY, 1167 nagateDval)); 1168 /* 1169 * If the first argument is negative zero and the second 1170 * argument is a negative finite odd integer, or the first 1171 * argument is negative infinity and the second argument is 1172 * a positive finite odd integer then the result is negative 1173 * infinity. 1174 */ 1175 assertEquals("Result should be Math.pow(" + NZERO + "," 1176 + nagateDval + ")=" + Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1177 Math.pow(NZERO, nagateDval)); 1178 assertEquals("Result should be Math.pow(" 1179 + Double.NEGATIVE_INFINITY + "," + dval + ")=" 1180 + Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Math.pow( 1181 Double.NEGATIVE_INFINITY, dval)); 1182 } 1183 1184 /** 1185 * 1. If the first argument is finite and less than zero if the 1186 * second argument is a finite even integer, the result is equal 1187 * to the result of raising the absolute value of the first 1188 * argument to the power of the second argument 1189 * 1190 * 2. if the second argument is a finite odd integer, the result is equal to the 1191 * negative of the result of raising the absolute value of the 1192 * first argument to the power of the second argument 1193 * 1194 * 3. if the second argument is finite and not an integer, then the result 1195 * is NaN. 1196 */ 1197 for (int j = 1; j < values.length; j++) { 1198 double jval = values[j]; 1199 if (jval % 2.0 == 0.0) { 1200 assertEquals("" + nagateDval + " " + jval, Math.pow( 1201 dval, jval), Math.pow(nagateDval, jval)); 1202 } else { 1203 assertEquals("" + nagateDval + " " + jval, -1.0 1204 * Math.pow(dval, jval), Math.pow(nagateDval, 1205 jval)); 1206 } 1207 assertEquals(Double.NaN, Math 1208 .pow(nagateDval, jval / 0.5467)); 1209 assertEquals(Double.NaN, Math.pow(nagateDval, -1.0 * jval 1210 / 0.5467)); 1211 } 1212 } 1213 1214 // If the absolute value of the first argument equals 1 and the 1215 // second argument is infinite, then the result is NaN. 1216 if (dval == 1) { 1217 assertEquals("Result should be Math.pow(" + dval + "," 1218 + Double.POSITIVE_INFINITY + ")=" + Double.NaN, Double.NaN, Math 1219 .pow(dval, Double.POSITIVE_INFINITY)); 1220 assertEquals("Result should be Math.pow(" + dval + "," 1221 + Double.NEGATIVE_INFINITY + ")=" + Double.NaN, Double.NaN, Math 1222 .pow(dval, Double.NEGATIVE_INFINITY)); 1223 1224 assertEquals("Result should be Math.pow(" + nagateDval + "," 1225 + Double.POSITIVE_INFINITY + ")=" + Double.NaN, Double.NaN, Math 1226 .pow(nagateDval, Double.POSITIVE_INFINITY)); 1227 assertEquals("Result should be Math.pow(" + nagateDval + "," 1228 + Double.NEGATIVE_INFINITY + ")=" + Double.NaN, Double.NaN, Math 1229 .pow(nagateDval, Double.NEGATIVE_INFINITY)); 1230 } 1231 1232 if (dval > 1) { 1233 /* 1234 * If the absolute value of the first argument is greater than 1 1235 * and the second argument is positive infinity, or the absolute 1236 * value of the first argument is less than 1 and the second 1237 * argument is negative infinity, then the result is positive 1238 * infinity. 1239 */ 1240 assertEquals("Result should be Math.pow(" + dval + "," 1241 + Double.POSITIVE_INFINITY + ")=" 1242 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow(dval, 1243 Double.POSITIVE_INFINITY)); 1244 1245 assertEquals("Result should be Math.pow(" + nagateDval + "," 1246 + Double.NEGATIVE_INFINITY + ")=" 1247 + Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Math.pow(-0.13456, 1248 Double.NEGATIVE_INFINITY)); 1249 1250 /* 1251 * If the absolute value of the first argument is greater than 1 1252 * and the second argument is negative infinity, or the absolute 1253 * value of the first argument is less than 1 and the second 1254 * argument is positive infinity, then the result is positive 1255 * zero. 1256 */ 1257 assertEquals("Result should be Math.pow(" + dval + "," 1258 + Double.NEGATIVE_INFINITY + ")= +0.0", +0.0, Math.pow(dval, 1259 Double.NEGATIVE_INFINITY)); 1260 assertEquals("Result should be Math.pow(" + nagateDval + "," 1261 + Double.POSITIVE_INFINITY + ")= +0.0", +0.0, Math.pow( 1262 -0.13456, Double.POSITIVE_INFINITY)); 1263 } 1264 1265 assertEquals("Result should be Math.pow(" + 0.0 + "," + dval + ")=" 1266 + 0.0, 0.0, Math.pow(0.0, dval)); 1267 assertEquals("Result should be Math.pow(" + Double.NaN + "," + dval 1268 + ")=" + Double.NaN, Double.NaN, Math.pow(Double.NaN, dval)); 1269 } 1270 assertTrue("pow returned incorrect value", 1271 (long) Math.pow(2, 8) == 256l); 1272 assertTrue("pow returned incorrect value", 1273 Math.pow(2, -8) == 0.00390625d); 1274 assertEquals("Incorrect root returned1", 1275 2, Math.sqrt(Math.pow(Math.sqrt(2), 4)), 0); 1276 1277 assertEquals(Double.NEGATIVE_INFINITY, Math.pow(-10.0, 3.093403029238847E15)); 1278 assertEquals(Double.POSITIVE_INFINITY, Math.pow(10.0, 3.093403029238847E15)); 1279 } 1280 longTodouble(long longvalue)1281 private double longTodouble(long longvalue) { 1282 return Double.longBitsToDouble(longvalue); 1283 } 1284 doubleTolong(double doublevalue)1285 private long doubleTolong(double doublevalue) { 1286 return Double.doubleToLongBits(doublevalue); 1287 } 1288 negateDouble(double doublevalue)1289 private double negateDouble(double doublevalue) { 1290 return doublevalue * -1.0; 1291 } 1292 1293 /** 1294 * java.lang.Math#rint(double) 1295 */ test_rintD()1296 public void test_rintD() { 1297 // Test for method double java.lang.Math.rint(double) 1298 assertEquals("Failed to round properly - up to odd", 1299 3.0, Math.rint(2.9), 0D); 1300 assertTrue("Failed to round properly - NaN", Double.isNaN(Math 1301 .rint(Double.NaN))); 1302 assertEquals("Failed to round properly down to even", 1303 2.0, Math.rint(2.1), 0D); 1304 assertTrue("Failed to round properly " + 2.5 + " to even", Math 1305 .rint(2.5) == 2.0); 1306 assertTrue("Failed to round properly " + (+0.0d), 1307 Math.rint(+0.0d) == +0.0d); 1308 assertTrue("Failed to round properly " + (-0.0d), 1309 Math.rint(-0.0d) == -0.0d); 1310 } 1311 1312 /** 1313 * java.lang.Math#round(double) 1314 */ test_roundD()1315 public void test_roundD() { 1316 // Test for method long java.lang.Math.round(double) 1317 assertEquals("Incorrect rounding of a float", -91, Math.round(-90.89d)); 1318 } 1319 1320 /** 1321 * java.lang.Math#round(float) 1322 */ test_roundF()1323 public void test_roundF() { 1324 // Test for method int java.lang.Math.round(float) 1325 assertEquals("Incorrect rounding of a float", -91, Math.round(-90.89f)); 1326 } 1327 1328 /** 1329 * {@link java.lang.Math#scalb(double, int)} 1330 * @since 1.6 1331 */ 1332 @SuppressWarnings("boxing") test_scalb_DI()1333 public void test_scalb_DI() { 1334 // result is normal 1335 assertEquals(4.1422946304E7, Math.scalb(1.2345, 25)); 1336 assertEquals(3.679096698760986E-8, Math.scalb(1.2345, -25)); 1337 assertEquals(1.2345, Math.scalb(1.2345, 0)); 1338 assertEquals(7868514.304, Math.scalb(0.2345, 25)); 1339 1340 double normal = Math.scalb(0.2345, -25); 1341 assertEquals(6.98864459991455E-9, normal); 1342 // precision kept 1343 assertEquals(0.2345, Math.scalb(normal, 25)); 1344 1345 assertEquals(0.2345, Math.scalb(0.2345, 0)); 1346 assertEquals(-4.1422946304E7, Math.scalb(-1.2345, 25)); 1347 assertEquals(-6.98864459991455E-9, Math.scalb(-0.2345, -25)); 1348 assertEquals(2.0, Math.scalb(Double.MIN_NORMAL / 2, 1024)); 1349 assertEquals(64.0, Math.scalb(Double.MIN_VALUE, 1080)); 1350 assertEquals(234, Math.getExponent(Math.scalb(1.0, 234))); 1351 assertEquals(3.9999999999999996, Math.scalb(Double.MAX_VALUE, 1352 Double.MIN_EXPONENT)); 1353 1354 // result is near infinity 1355 double halfMax = Math.scalb(1.0, Double.MAX_EXPONENT); 1356 assertEquals(8.98846567431158E307, halfMax); 1357 assertEquals(Double.MAX_VALUE, halfMax - Math.ulp(halfMax) + halfMax); 1358 assertEquals(Double.POSITIVE_INFINITY, halfMax + halfMax); 1359 assertEquals(1.7976931348623155E308, Math.scalb(1.0 - Math.ulp(1.0), 1360 Double.MAX_EXPONENT + 1)); 1361 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(1.0 - Math.ulp(1.0), 1362 Double.MAX_EXPONENT + 2)); 1363 1364 halfMax = Math.scalb(-1.0, Double.MAX_EXPONENT); 1365 assertEquals(-8.98846567431158E307, halfMax); 1366 assertEquals(-Double.MAX_VALUE, halfMax + Math.ulp(halfMax) + halfMax); 1367 assertEquals(Double.NEGATIVE_INFINITY, halfMax + halfMax); 1368 1369 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(0.345, 1234)); 1370 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(44.345E102, 934)); 1371 assertEquals(Double.NEGATIVE_INFINITY, Math.scalb(-44.345E102, 934)); 1372 1373 assertEquals(Double.POSITIVE_INFINITY, Math.scalb( 1374 Double.MIN_NORMAL / 2, 4000)); 1375 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(Double.MIN_VALUE, 1376 8000)); 1377 assertEquals(Double.POSITIVE_INFINITY, Math.scalb(Double.MAX_VALUE, 1)); 1378 assertEquals(Double.POSITIVE_INFINITY, Math.scalb( 1379 Double.POSITIVE_INFINITY, 0)); 1380 assertEquals(Double.POSITIVE_INFINITY, Math.scalb( 1381 Double.POSITIVE_INFINITY, -1)); 1382 assertEquals(Double.NEGATIVE_INFINITY, Math.scalb( 1383 Double.NEGATIVE_INFINITY, -1)); 1384 assertEquals(Double.NEGATIVE_INFINITY, Math.scalb( 1385 Double.NEGATIVE_INFINITY, Double.MIN_EXPONENT)); 1386 1387 // result is subnormal/zero 1388 long posZeroBits = Double.doubleToLongBits(+0.0); 1389 long negZeroBits = Double.doubleToLongBits(-0.0); 1390 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb(+0.0, 1391 Integer.MAX_VALUE))); 1392 assertEquals(posZeroBits, Double.doubleToLongBits(Math 1393 .scalb(+0.0, -123))); 1394 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb(+0.0, 0))); 1395 assertEquals(negZeroBits, Double 1396 .doubleToLongBits(Math.scalb(-0.0, 123))); 1397 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb(-0.0, 1398 Integer.MIN_VALUE))); 1399 1400 assertEquals(Double.MIN_VALUE, Math.scalb(1.0, -1074)); 1401 assertEquals(posZeroBits, Double.doubleToLongBits(Math 1402 .scalb(1.0, -1075))); 1403 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb(-1.0, 1404 -1075))); 1405 1406 // precision lost 1407 assertEquals(Math.scalb(21.405, -1078), Math.scalb(21.405, -1079)); 1408 assertEquals(Double.MIN_VALUE, Math.scalb(21.405, -1079)); 1409 assertEquals(-Double.MIN_VALUE, Math.scalb(-21.405, -1079)); 1410 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb(21.405, 1411 -1080))); 1412 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb(-21.405, 1413 -1080))); 1414 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1415 Double.MIN_VALUE, -1))); 1416 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1417 -Double.MIN_VALUE, -1))); 1418 assertEquals(Double.MIN_VALUE, Math.scalb(Double.MIN_NORMAL, -52)); 1419 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1420 Double.MIN_NORMAL, -53))); 1421 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1422 -Double.MIN_NORMAL, -53))); 1423 assertEquals(Double.MIN_VALUE, Math.scalb(Double.MAX_VALUE, -2098)); 1424 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1425 Double.MAX_VALUE, -2099))); 1426 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1427 -Double.MAX_VALUE, -2099))); 1428 assertEquals(Double.MIN_VALUE, Math.scalb(Double.MIN_NORMAL / 3, -51)); 1429 assertEquals(posZeroBits, Double.doubleToLongBits(Math.scalb( 1430 Double.MIN_NORMAL / 3, -52))); 1431 assertEquals(negZeroBits, Double.doubleToLongBits(Math.scalb( 1432 -Double.MIN_NORMAL / 3, -52))); 1433 double subnormal = Math.scalb(Double.MIN_NORMAL / 3, -25); 1434 assertEquals(2.2104123E-316, subnormal); 1435 // precision lost 1436 assertFalse(Double.MIN_NORMAL / 3 == Math.scalb(subnormal, 25)); 1437 1438 // NaN 1439 assertTrue(Double.isNaN(Math.scalb(Double.NaN, 1))); 1440 assertTrue(Double.isNaN(Math.scalb(Double.NaN, 0))); 1441 assertTrue(Double.isNaN(Math.scalb(Double.NaN, -120))); 1442 1443 assertEquals(1283457024, Double.doubleToLongBits(Math.scalb( 1444 Double.MIN_VALUE * 153, 23))); 1445 assertEquals(-9223372035571318784L, Double.doubleToLongBits(Math.scalb( 1446 -Double.MIN_VALUE * 153, 23))); 1447 assertEquals(36908406321184768L, Double.doubleToLongBits(Math.scalb( 1448 Double.MIN_VALUE * 153, 52))); 1449 assertEquals(-9186463630533591040L, Double.doubleToLongBits(Math.scalb( 1450 -Double.MIN_VALUE * 153, 52))); 1451 1452 // test for exception 1453 try { 1454 Math.scalb((Double) null, (Integer) null); 1455 fail("Should throw NullPointerException"); 1456 } catch (NullPointerException e) { 1457 // Expected 1458 } 1459 try { 1460 Math.scalb(1.0, (Integer) null); 1461 fail("Should throw NullPointerException"); 1462 } catch (NullPointerException e) { 1463 // Expected 1464 } 1465 try { 1466 Math.scalb((Double) null, 1); 1467 fail("Should throw NullPointerException"); 1468 } catch (NullPointerException e) { 1469 // Expected 1470 } 1471 1472 long b1em1022 = 0x0010000000000000L; // bit representation of 1473 // Double.MIN_NORMAL 1474 long b1em1023 = 0x0008000000000000L; // bit representation of half of 1475 // Double.MIN_NORMAL 1476 // assert exact identity 1477 assertEquals(b1em1023, Double.doubleToLongBits(Math.scalb(Double 1478 .longBitsToDouble(b1em1022), -1))); 1479 } 1480 1481 /** 1482 * {@link java.lang.Math#scalb(float, int)} 1483 * @since 1.6 1484 */ 1485 @SuppressWarnings("boxing") test_scalb_FI()1486 public void test_scalb_FI() { 1487 // result is normal 1488 assertEquals(4.1422946304E7f, Math.scalb(1.2345f, 25)); 1489 assertEquals(3.679096698760986E-8f, Math.scalb(1.2345f, -25)); 1490 assertEquals(1.2345f, Math.scalb(1.2345f, 0)); 1491 assertEquals(7868514.304f, Math.scalb(0.2345f, 25)); 1492 1493 float normal = Math.scalb(0.2345f, -25); 1494 assertEquals(6.98864459991455E-9f, normal); 1495 // precision kept 1496 assertEquals(0.2345f, Math.scalb(normal, 25)); 1497 1498 assertEquals(0.2345f, Math.scalb(0.2345f, 0)); 1499 assertEquals(-4.1422946304E7f, Math.scalb(-1.2345f, 25)); 1500 assertEquals(-6.98864459991455E-9f, Math.scalb(-0.2345f, -25)); 1501 assertEquals(2.0f, Math.scalb(Float.MIN_NORMAL / 2, 128)); 1502 assertEquals(64.0f, Math.scalb(Float.MIN_VALUE, 155)); 1503 assertEquals(34, Math.getExponent(Math.scalb(1.0f, 34))); 1504 assertEquals(3.9999998f, Math 1505 .scalb(Float.MAX_VALUE, Float.MIN_EXPONENT)); 1506 1507 // result is near infinity 1508 float halfMax = Math.scalb(1.0f, Float.MAX_EXPONENT); 1509 assertEquals(1.7014118E38f, halfMax); 1510 assertEquals(Float.MAX_VALUE, halfMax - Math.ulp(halfMax) + halfMax); 1511 assertEquals(Float.POSITIVE_INFINITY, halfMax + halfMax); 1512 assertEquals(3.4028233E38f, Math.scalb(1.0f - Math.ulp(1.0f), 1513 Float.MAX_EXPONENT + 1)); 1514 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(1.0f - Math.ulp(1.0f), 1515 Float.MAX_EXPONENT + 2)); 1516 1517 halfMax = Math.scalb(-1.0f, Float.MAX_EXPONENT); 1518 assertEquals(-1.7014118E38f, halfMax); 1519 assertEquals(-Float.MAX_VALUE, halfMax + Math.ulp(halfMax) + halfMax); 1520 assertEquals(Float.NEGATIVE_INFINITY, halfMax + halfMax); 1521 1522 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(0.345f, 1234)); 1523 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(44.345E10f, 934)); 1524 assertEquals(Float.NEGATIVE_INFINITY, Math.scalb(-44.345E10f, 934)); 1525 1526 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(Float.MIN_NORMAL / 2, 1527 400)); 1528 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(Float.MIN_VALUE, 800)); 1529 assertEquals(Float.POSITIVE_INFINITY, Math.scalb(Float.MAX_VALUE, 1)); 1530 assertEquals(Float.POSITIVE_INFINITY, Math.scalb( 1531 Float.POSITIVE_INFINITY, 0)); 1532 assertEquals(Float.POSITIVE_INFINITY, Math.scalb( 1533 Float.POSITIVE_INFINITY, -1)); 1534 assertEquals(Float.NEGATIVE_INFINITY, Math.scalb( 1535 Float.NEGATIVE_INFINITY, -1)); 1536 assertEquals(Float.NEGATIVE_INFINITY, Math.scalb( 1537 Float.NEGATIVE_INFINITY, Float.MIN_EXPONENT)); 1538 1539 // result is subnormal/zero 1540 int posZeroBits = Float.floatToIntBits(+0.0f); 1541 int negZeroBits = Float.floatToIntBits(-0.0f); 1542 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(+0.0f, 1543 Integer.MAX_VALUE))); 1544 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(+0.0f, -123))); 1545 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(+0.0f, 0))); 1546 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-0.0f, 123))); 1547 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-0.0f, 1548 Integer.MIN_VALUE))); 1549 1550 assertEquals(Float.MIN_VALUE, Math.scalb(1.0f, -149)); 1551 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb(1.0f, -150))); 1552 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-1.0f, -150))); 1553 1554 // precision lost 1555 assertEquals(Math.scalb(21.405f, -154), Math.scalb(21.405f, -153)); 1556 assertEquals(Float.MIN_VALUE, Math.scalb(21.405f, -154)); 1557 assertEquals(-Float.MIN_VALUE, Math.scalb(-21.405f, -154)); 1558 assertEquals(posZeroBits, Float.floatToIntBits(Math 1559 .scalb(21.405f, -155))); 1560 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb(-21.405f, 1561 -155))); 1562 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1563 Float.MIN_VALUE, -1))); 1564 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1565 -Float.MIN_VALUE, -1))); 1566 assertEquals(Float.MIN_VALUE, Math.scalb(Float.MIN_NORMAL, -23)); 1567 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1568 Float.MIN_NORMAL, -24))); 1569 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1570 -Float.MIN_NORMAL, -24))); 1571 assertEquals(Float.MIN_VALUE, Math.scalb(Float.MAX_VALUE, -277)); 1572 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1573 Float.MAX_VALUE, -278))); 1574 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1575 -Float.MAX_VALUE, -278))); 1576 assertEquals(Float.MIN_VALUE, Math.scalb(Float.MIN_NORMAL / 3, -22)); 1577 assertEquals(posZeroBits, Float.floatToIntBits(Math.scalb( 1578 Float.MIN_NORMAL / 3, -23))); 1579 assertEquals(negZeroBits, Float.floatToIntBits(Math.scalb( 1580 -Float.MIN_NORMAL / 3, -23))); 1581 float subnormal = Math.scalb(Float.MIN_NORMAL / 3, -11); 1582 assertEquals(1.913E-42f, subnormal); 1583 // precision lost 1584 assertFalse(Float.MIN_NORMAL / 3 == Math.scalb(subnormal, 11)); 1585 1586 assertEquals(68747264, Float.floatToIntBits(Math.scalb( 1587 Float.MIN_VALUE * 153, 23))); 1588 assertEquals(-2078736384, Float.floatToIntBits(Math.scalb( 1589 -Float.MIN_VALUE * 153, 23))); 1590 1591 assertEquals(4896, Float.floatToIntBits(Math.scalb( 1592 Float.MIN_VALUE * 153, 5))); 1593 assertEquals(-2147478752, Float.floatToIntBits(Math.scalb( 1594 -Float.MIN_VALUE * 153, 5))); 1595 1596 // NaN 1597 assertTrue(Float.isNaN(Math.scalb(Float.NaN, 1))); 1598 assertTrue(Float.isNaN(Math.scalb(Float.NaN, 0))); 1599 assertTrue(Float.isNaN(Math.scalb(Float.NaN, -120))); 1600 1601 // test for exception 1602 try { 1603 Math.scalb((Float) null, (Integer) null); 1604 fail("Should throw NullPointerException"); 1605 } catch (NullPointerException e) { 1606 // Expected 1607 } 1608 try { 1609 Math.scalb(1.0f, (Integer) null); 1610 fail("Should throw NullPointerException"); 1611 } catch (NullPointerException e) { 1612 // Expected 1613 } 1614 try { 1615 Math.scalb((Float) null, 1); 1616 fail("Should throw NullPointerException"); 1617 } catch (NullPointerException e) { 1618 // Expected 1619 } 1620 1621 int b1em126 = 0x00800000; // bit representation of Float.MIN_NORMAL 1622 int b1em127 = 0x00400000; // bit representation of half 1623 // Float.MIN_NORMAL 1624 // assert exact identity 1625 assertEquals(b1em127, Float.floatToIntBits(Math.scalb(Float 1626 .intBitsToFloat(b1em126), -1))); 1627 } 1628 1629 /** 1630 * java.lang.Math#signum(double) 1631 */ test_signum_D()1632 public void test_signum_D() { 1633 assertTrue(Double.isNaN(Math.signum(Double.NaN))); 1634 assertTrue(Double.isNaN(Math.signum(Double.NaN))); 1635 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 1636 .signum(0.0))); 1637 assertEquals(Double.doubleToLongBits(+0.0), Double 1638 .doubleToLongBits(Math.signum(+0.0))); 1639 assertEquals(Double.doubleToLongBits(-0.0), Double 1640 .doubleToLongBits(Math.signum(-0.0))); 1641 1642 assertEquals(1.0, Math.signum(253681.2187962), 0D); 1643 assertEquals(-1.0, Math.signum(-125874693.56), 0D); 1644 assertEquals(1.0, Math.signum(1.2587E-308), 0D); 1645 assertEquals(-1.0, Math.signum(-1.2587E-308), 0D); 1646 1647 assertEquals(1.0, Math.signum(Double.MAX_VALUE), 0D); 1648 assertEquals(1.0, Math.signum(Double.MIN_VALUE), 0D); 1649 assertEquals(-1.0, Math.signum(-Double.MAX_VALUE), 0D); 1650 assertEquals(-1.0, Math.signum(-Double.MIN_VALUE), 0D); 1651 assertEquals(1.0, Math.signum(Double.POSITIVE_INFINITY), 0D); 1652 assertEquals(-1.0, Math.signum(Double.NEGATIVE_INFINITY), 0D); 1653 } 1654 1655 /** 1656 * java.lang.Math#signum(float) 1657 */ test_signum_F()1658 public void test_signum_F() { 1659 assertTrue(Float.isNaN(Math.signum(Float.NaN))); 1660 assertEquals(Float.floatToIntBits(0.0f), Float 1661 .floatToIntBits(Math.signum(0.0f))); 1662 assertEquals(Float.floatToIntBits(+0.0f), Float 1663 .floatToIntBits(Math.signum(+0.0f))); 1664 assertEquals(Float.floatToIntBits(-0.0f), Float 1665 .floatToIntBits(Math.signum(-0.0f))); 1666 1667 assertEquals(1.0f, Math.signum(253681.2187962f), 0f); 1668 assertEquals(-1.0f, Math.signum(-125874693.56f), 0f); 1669 assertEquals(1.0f, Math.signum(1.2587E-11f), 0f); 1670 assertEquals(-1.0f, Math.signum(-1.2587E-11f), 0f); 1671 1672 assertEquals(1.0f, Math.signum(Float.MAX_VALUE), 0f); 1673 assertEquals(1.0f, Math.signum(Float.MIN_VALUE), 0f); 1674 assertEquals(-1.0f, Math.signum(-Float.MAX_VALUE), 0f); 1675 assertEquals(-1.0f, Math.signum(-Float.MIN_VALUE), 0f); 1676 assertEquals(1.0f, Math.signum(Float.POSITIVE_INFINITY), 0f); 1677 assertEquals(-1.0f, Math.signum(Float.NEGATIVE_INFINITY), 0f); 1678 } 1679 1680 /** 1681 * java.lang.Math#sin(double) 1682 */ test_sinD()1683 public void test_sinD() { 1684 // Test for method double java.lang.Math.sin(double) 1685 assertEquals("Incorrect answer", 0.0, Math.sin(0), 0D); 1686 assertEquals("Incorrect answer", 0.8414709848078965, Math.sin(1), 0D); 1687 } 1688 1689 /** 1690 * java.lang.Math#sinh(double) 1691 */ test_sinh_D()1692 public void test_sinh_D() { 1693 // Test for special situations 1694 assertTrue(Double.isNaN(Math.sinh(Double.NaN))); 1695 assertEquals(Double.POSITIVE_INFINITY, Math.sinh(Double.POSITIVE_INFINITY), 0D); 1696 assertEquals(Double.NEGATIVE_INFINITY, Math.sinh(Double.NEGATIVE_INFINITY), 0D); 1697 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math.sinh(0.0))); 1698 assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math.sinh(+0.0))); 1699 assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math.sinh(-0.0))); 1700 1701 assertEquals(Double.POSITIVE_INFINITY, Math.sinh(1234.56), 0D); 1702 assertEquals(Double.NEGATIVE_INFINITY, Math.sinh(-1234.56), 0D); 1703 assertEquals(1.0000000000001666E-6, Math.sinh(0.000001), 0D); 1704 assertEquals(-1.0000000000001666E-6, Math.sinh(-0.000001), 0D); 1705 assertEquals(5.115386441963859, Math.sinh(2.33482), Math.ulp(5.115386441963859)); 1706 assertEquals(Double.POSITIVE_INFINITY, Math.sinh(Double.MAX_VALUE), 0D); 1707 assertEquals(4.9E-324, Math.sinh(Double.MIN_VALUE), 0D); 1708 } 1709 1710 /** 1711 * java.lang.Math#sqrt(double) 1712 */ test_sqrtD()1713 public void test_sqrtD() { 1714 // Test for method double java.lang.Math.sqrt(double) 1715 assertEquals("Incorrect root returned2", 7, Math.sqrt(49), 0); 1716 } 1717 1718 /** 1719 * java.lang.Math#tan(double) 1720 */ test_tanD()1721 public void test_tanD() { 1722 // Test for method double java.lang.Math.tan(double) 1723 assertEquals("Incorrect answer", 0.0, Math.tan(0), 0D); 1724 assertEquals("Incorrect answer", 1.5574077246549023, Math.tan(1), 0D); 1725 1726 } 1727 1728 /** 1729 * java.lang.Math#tanh(double) 1730 */ test_tanh_D()1731 public void test_tanh_D() { 1732 // Test for special situations 1733 assertTrue("Should return NaN", Double.isNaN(Math.tanh(Double.NaN))); 1734 assertEquals("Should return +1.0", +1.0, Math 1735 .tanh(Double.POSITIVE_INFINITY), 0D); 1736 assertEquals("Should return -1.0", -1.0, Math 1737 .tanh(Double.NEGATIVE_INFINITY), 0D); 1738 assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math 1739 .tanh(0.0))); 1740 assertEquals(Double.doubleToLongBits(+0.0), Double 1741 .doubleToLongBits(Math.tanh(+0.0))); 1742 assertEquals(Double.doubleToLongBits(-0.0), Double 1743 .doubleToLongBits(Math.tanh(-0.0))); 1744 1745 assertEquals("Should return 1.0", 1.0, Math.tanh(1234.56), 0D); 1746 assertEquals("Should return -1.0", -1.0, Math.tanh(-1234.56), 0D); 1747 assertEquals("Should return 9.999999999996666E-7", 1748 9.999999999996666E-7, Math.tanh(0.000001), 0D); 1749 assertEquals("Should return 0.981422884124941", 0.981422884124941, Math 1750 .tanh(2.33482), 0D); 1751 assertEquals("Should return 1.0", 1.0, Math.tanh(Double.MAX_VALUE), 0D); 1752 assertEquals("Should return 4.9E-324", 4.9E-324, Math 1753 .tanh(Double.MIN_VALUE), 0D); 1754 } 1755 1756 /** 1757 * java.lang.Math#random() 1758 */ test_random()1759 public void test_random() { 1760 // There isn't a place for these tests so just stick them here 1761 assertEquals("Wrong value E", 1762 4613303445314885481L, Double.doubleToLongBits(Math.E)); 1763 assertEquals("Wrong value PI", 1764 4614256656552045848L, Double.doubleToLongBits(Math.PI)); 1765 1766 for (int i = 500; i >= 0; i--) { 1767 double d = Math.random(); 1768 assertTrue("Generated number is out of range: " + d, d >= 0.0 1769 && d < 1.0); 1770 } 1771 } 1772 1773 /** 1774 * java.lang.Math#toRadians(double) 1775 */ test_toRadiansD()1776 public void test_toRadiansD() { 1777 for (double d = 500; d >= 0; d -= 1.0) { 1778 double converted = Math.toDegrees(Math.toRadians(d)); 1779 assertTrue("Converted number not equal to original. d = " + d, 1780 converted >= d * 0.99999999 && converted <= d * 1.00000001); 1781 } 1782 } 1783 1784 /** 1785 * java.lang.Math#toDegrees(double) 1786 */ test_toDegreesD()1787 public void test_toDegreesD() { 1788 for (double d = 500; d >= 0; d -= 1.0) { 1789 double converted = Math.toRadians(Math.toDegrees(d)); 1790 assertTrue("Converted number not equal to original. d = " + d, 1791 converted >= d * 0.99999999 && converted <= d * 1.00000001); 1792 } 1793 } 1794 1795 /** 1796 * java.lang.Math#ulp(double) 1797 */ 1798 @SuppressWarnings("boxing") test_ulp_D()1799 public void test_ulp_D() { 1800 // Test for special cases 1801 assertTrue("Should return NaN", Double.isNaN(Math.ulp(Double.NaN))); 1802 assertEquals("Returned incorrect value", Double.POSITIVE_INFINITY, Math 1803 .ulp(Double.POSITIVE_INFINITY), 0D); 1804 assertEquals("Returned incorrect value", Double.POSITIVE_INFINITY, Math 1805 .ulp(Double.NEGATIVE_INFINITY), 0D); 1806 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1807 .ulp(0.0), 0D); 1808 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1809 .ulp(+0.0), 0D); 1810 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1811 .ulp(-0.0), 0D); 1812 assertEquals("Returned incorrect value", Math.pow(2, 971), Math 1813 .ulp(Double.MAX_VALUE), 0D); 1814 assertEquals("Returned incorrect value", Math.pow(2, 971), Math 1815 .ulp(-Double.MAX_VALUE), 0D); 1816 1817 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1818 .ulp(Double.MIN_VALUE), 0D); 1819 assertEquals("Returned incorrect value", Double.MIN_VALUE, Math 1820 .ulp(-Double.MIN_VALUE), 0D); 1821 1822 assertEquals("Returned incorrect value", 2.220446049250313E-16, Math 1823 .ulp(1.0), 0D); 1824 assertEquals("Returned incorrect value", 2.220446049250313E-16, Math 1825 .ulp(-1.0), 0D); 1826 assertEquals("Returned incorrect value", 2.2737367544323206E-13, Math 1827 .ulp(1153.0), 0D); 1828 } 1829 1830 /** 1831 * java.lang.Math#ulp(float) 1832 */ 1833 @SuppressWarnings("boxing") test_ulp_f()1834 public void test_ulp_f() { 1835 // Test for special cases 1836 assertTrue("Should return NaN", Float.isNaN(Math.ulp(Float.NaN))); 1837 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, Math 1838 .ulp(Float.POSITIVE_INFINITY), 0f); 1839 assertEquals("Returned incorrect value", Float.POSITIVE_INFINITY, Math 1840 .ulp(Float.NEGATIVE_INFINITY), 0f); 1841 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 1842 .ulp(0.0f), 0f); 1843 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 1844 .ulp(+0.0f), 0f); 1845 assertEquals("Returned incorrect value", Float.MIN_VALUE, Math 1846 .ulp(-0.0f), 0f); 1847 assertEquals("Returned incorrect value", 2.028241E31f, Math 1848 .ulp(Float.MAX_VALUE), 0f); 1849 assertEquals("Returned incorrect value", 2.028241E31f, Math 1850 .ulp(-Float.MAX_VALUE), 0f); 1851 1852 assertEquals("Returned incorrect value", 1.4E-45f, Math 1853 .ulp(Float.MIN_VALUE), 0f); 1854 assertEquals("Returned incorrect value", 1.4E-45f, Math 1855 .ulp(-Float.MIN_VALUE), 0f); 1856 1857 assertEquals("Returned incorrect value", 1.1920929E-7f, Math.ulp(1.0f), 1858 0f); 1859 assertEquals("Returned incorrect value", 1.1920929E-7f, 1860 Math.ulp(-1.0f), 0f); 1861 assertEquals("Returned incorrect value", 1.2207031E-4f, Math 1862 .ulp(1153.0f), 0f); 1863 assertEquals("Returned incorrect value", 5.6E-45f, Math 1864 .ulp(9.403954E-38f), 0f); 1865 } 1866 1867 /** 1868 * {@link java.lang.Math#shiftIntBits(int, int)} 1869 * @since 1.6 1870 */ test_shiftIntBits_II()1871 public void test_shiftIntBits_II() { 1872 class Tuple { 1873 public int result; 1874 1875 public int value; 1876 1877 public int factor; 1878 1879 public Tuple(int result, int value, int factor) { 1880 this.result = result; 1881 this.value = value; 1882 this.factor = factor; 1883 } 1884 } 1885 final Tuple[] TUPLES = new Tuple[] { 1886 // sub-normal to sub-normal 1887 new Tuple(0x00000000, 0x00000001, -1), 1888 // round to even 1889 new Tuple(0x00000002, 0x00000003, -1), 1890 // round to even 1891 new Tuple(0x00000001, 0x00000005, -3), 1892 // round to infinity 1893 new Tuple(0x00000002, 0x0000000d, -3), 1894 // round to infinity 1895 1896 // normal to sub-normal 1897 new Tuple(0x00000002, 0x01a00000, -24), 1898 // round to even 1899 new Tuple(0x00000004, 0x01e00000, -24), 1900 // round to even 1901 new Tuple(0x00000003, 0x01c80000, -24), 1902 // round to infinity 1903 new Tuple(0x00000004, 0x01e80000, -24), 1904 // round to infinity 1905 }; 1906 for (int i = 0; i < TUPLES.length; ++i) { 1907 Tuple tuple = TUPLES[i]; 1908 assertEquals(tuple.result, Float.floatToIntBits(Math.scalb(Float 1909 .intBitsToFloat(tuple.value), tuple.factor))); 1910 assertEquals(tuple.result, Float.floatToIntBits(-Math.scalb(-Float 1911 .intBitsToFloat(tuple.value), tuple.factor))); 1912 } 1913 } 1914 1915 /** 1916 * {@link java.lang.Math#shiftLongBits(long, long)} 1917 * <p/> 1918 * Round result to nearest value on precision lost. 1919 * @since 1.6 1920 */ test_shiftLongBits_LL()1921 public void test_shiftLongBits_LL() { 1922 class Tuple { 1923 public long result; 1924 1925 public long value; 1926 1927 public int factor; 1928 1929 public Tuple(long result, long value, int factor) { 1930 this.result = result; 1931 this.value = value; 1932 this.factor = factor; 1933 } 1934 } 1935 final Tuple[] TUPLES = new Tuple[] { 1936 // sub-normal to sub-normal 1937 new Tuple(0x00000000L, 0x00000001L, -1), 1938 //round to even 1939 new Tuple(0x00000002L, 0x00000003L, -1), 1940 //round to even 1941 new Tuple(0x00000001L, 0x00000005L, -3), 1942 //round to infinity 1943 new Tuple(0x00000002L, 0x0000000dL, -3), 1944 //round to infinity 1945 1946 // normal to sub-normal 1947 new Tuple(0x0000000000000002L, 0x0034000000000000L, -53), // round to even 1948 new Tuple(0x0000000000000004L, 0x003c000000000000L, -53), // round to even 1949 new Tuple(0x0000000000000003L, 0x0035000000000000L, -53), // round to infinity 1950 new Tuple(0x0000000000000004L, 0x003d000000000000L, -53), // round to infinity 1951 }; 1952 for (int i = 0; i < TUPLES.length; ++i) { 1953 Tuple tuple = TUPLES[i]; 1954 assertEquals(tuple.result, Double.doubleToLongBits(Math.scalb( 1955 Double.longBitsToDouble(tuple.value), tuple.factor))); 1956 assertEquals(tuple.result, Double.doubleToLongBits(-Math.scalb( 1957 -Double.longBitsToDouble(tuple.value), tuple.factor))); 1958 } 1959 } 1960 } 1961