1 /* 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.lang; 27 import java.util.Random; 28 import sun.misc.DoubleConsts; 29 30 /** 31 * The class {@code StrictMath} contains methods for performing basic 32 * numeric operations such as the elementary exponential, logarithm, 33 * square root, and trigonometric functions. 34 * 35 * <p>To help ensure portability of Java programs, the definitions of 36 * some of the numeric functions in this package require that they 37 * produce the same results as certain published algorithms. These 38 * algorithms are available from the well-known network library 39 * {@code netlib} as the package "Freely Distributable Math 40 * Library," <a 41 * href="ftp://ftp.netlib.org/fdlibm.tar">{@code fdlibm}</a>. These 42 * algorithms, which are written in the C programming language, are 43 * then to be understood as executed with all floating-point 44 * operations following the rules of Java floating-point arithmetic. 45 * 46 * <p>The Java math library is defined with respect to 47 * {@code fdlibm} version 5.3. Where {@code fdlibm} provides 48 * more than one definition for a function (such as 49 * {@code acos}), use the "IEEE 754 core function" version 50 * (residing in a file whose name begins with the letter 51 * {@code e}). The methods which require {@code fdlibm} 52 * semantics are {@code sin}, {@code cos}, {@code tan}, 53 * {@code asin}, {@code acos}, {@code atan}, 54 * {@code exp}, {@code log}, {@code log10}, 55 * {@code cbrt}, {@code atan2}, {@code pow}, 56 * {@code sinh}, {@code cosh}, {@code tanh}, 57 * {@code hypot}, {@code expm1}, and {@code log1p}. 58 * 59 * <p> 60 * The platform uses signed two's complement integer arithmetic with 61 * int and long primitive types. The developer should choose 62 * the primitive type to ensure that arithmetic operations consistently 63 * produce correct results, which in some cases means the operations 64 * will not overflow the range of values of the computation. 65 * The best practice is to choose the primitive type and algorithm to avoid 66 * overflow. In cases where the size is {@code int} or {@code long} and 67 * overflow errors need to be detected, the methods {@code addExact}, 68 * {@code subtractExact}, {@code multiplyExact}, and {@code toIntExact} 69 * throw an {@code ArithmeticException} when the results overflow. 70 * For other arithmetic operations such as divide, absolute value, 71 * increment, decrement, and negation overflow occurs only with 72 * a specific minimum or maximum value and should be checked against 73 * the minimum or maximum as appropriate. 74 * 75 * @author unascribed 76 * @author Joseph D. Darcy 77 * @since 1.3 78 */ 79 80 public final class StrictMath { 81 82 /** 83 * Don't let anyone instantiate this class. 84 */ 85 private StrictMath() {} 86 87 /** 88 * The {@code double} value that is closer than any other to 89 * <i>e</i>, the base of the natural logarithms. 90 */ 91 public static final double E = 2.7182818284590452354; 92 93 /** 94 * The {@code double} value that is closer than any other to 95 * <i>pi</i>, the ratio of the circumference of a circle to its 96 * diameter. 97 */ 98 public static final double PI = 3.14159265358979323846; 99 100 /** 101 * Returns the trigonometric sine of an angle. Special cases: 102 * <ul><li>If the argument is NaN or an infinity, then the 103 * result is NaN. 104 * <li>If the argument is zero, then the result is a zero with the 105 * same sign as the argument.</ul> 106 * 107 * @param a an angle, in radians. 108 * @return the sine of the argument. 109 */ 110 public static native double sin(double a); 111 112 /** 113 * Returns the trigonometric cosine of an angle. Special cases: 114 * <ul><li>If the argument is NaN or an infinity, then the 115 * result is NaN.</ul> 116 * 117 * @param a an angle, in radians. 118 * @return the cosine of the argument. 119 */ 120 public static native double cos(double a); 121 122 /** 123 * Returns the trigonometric tangent of an angle. Special cases: 124 * <ul><li>If the argument is NaN or an infinity, then the result 125 * is NaN. 126 * <li>If the argument is zero, then the result is a zero with the 127 * same sign as the argument.</ul> 128 * 129 * @param a an angle, in radians. 130 * @return the tangent of the argument. 131 */ 132 public static native double tan(double a); 133 134 /** 135 * Returns the arc sine of a value; the returned angle is in the 136 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases: 137 * <ul><li>If the argument is NaN or its absolute value is greater 138 * than 1, then the result is NaN. 139 * <li>If the argument is zero, then the result is a zero with the 140 * same sign as the argument.</ul> 141 * 142 * @param a the value whose arc sine is to be returned. 143 * @return the arc sine of the argument. 144 */ 145 public static native double asin(double a); 146 147 /** 148 * Returns the arc cosine of a value; the returned angle is in the 149 * range 0.0 through <i>pi</i>. Special case: 150 * <ul><li>If the argument is NaN or its absolute value is greater 151 * than 1, then the result is NaN.</ul> 152 * 153 * @param a the value whose arc cosine is to be returned. 154 * @return the arc cosine of the argument. 155 */ 156 public static native double acos(double a); 157 158 /** 159 * Returns the arc tangent of a value; the returned angle is in the 160 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases: 161 * <ul><li>If the argument is NaN, then the result is NaN. 162 * <li>If the argument is zero, then the result is a zero with the 163 * same sign as the argument.</ul> 164 * 165 * @param a the value whose arc tangent is to be returned. 166 * @return the arc tangent of the argument. 167 */ 168 public static native double atan(double a); 169 170 /** 171 * Converts an angle measured in degrees to an approximately 172 * equivalent angle measured in radians. The conversion from 173 * degrees to radians is generally inexact. 174 * 175 * @param angdeg an angle, in degrees 176 * @return the measurement of the angle {@code angdeg} 177 * in radians. 178 */ 179 public static strictfp double toRadians(double angdeg) { 180 // Do not delegate to Math.toRadians(angdeg) because 181 // this method has the strictfp modifier. 182 return angdeg / 180.0 * PI; 183 } 184 185 /** 186 * Converts an angle measured in radians to an approximately 187 * equivalent angle measured in degrees. The conversion from 188 * radians to degrees is generally inexact; users should 189 * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly 190 * equal {@code 0.0}. 191 * 192 * @param angrad an angle, in radians 193 * @return the measurement of the angle {@code angrad} 194 * in degrees. 195 */ 196 public static strictfp double toDegrees(double angrad) { 197 // Do not delegate to Math.toDegrees(angrad) because 198 // this method has the strictfp modifier. 199 return angrad * 180.0 / PI; 200 } 201 202 /** 203 * Returns Euler's number <i>e</i> raised to the power of a 204 * {@code double} value. Special cases: 205 * <ul><li>If the argument is NaN, the result is NaN. 206 * <li>If the argument is positive infinity, then the result is 207 * positive infinity. 208 * <li>If the argument is negative infinity, then the result is 209 * positive zero.</ul> 210 * 211 * @param a the exponent to raise <i>e</i> to. 212 * @return the value <i>e</i><sup>{@code a}</sup>, 213 * where <i>e</i> is the base of the natural logarithms. 214 */ 215 public static native double exp(double a); 216 217 /** 218 * Returns the natural logarithm (base <i>e</i>) of a {@code double} 219 * value. Special cases: 220 * <ul><li>If the argument is NaN or less than zero, then the result 221 * is NaN. 222 * <li>If the argument is positive infinity, then the result is 223 * positive infinity. 224 * <li>If the argument is positive zero or negative zero, then the 225 * result is negative infinity.</ul> 226 * 227 * @param a a value 228 * @return the value ln {@code a}, the natural logarithm of 229 * {@code a}. 230 */ 231 public static native double log(double a); 232 233 234 /** 235 * Returns the base 10 logarithm of a {@code double} value. 236 * Special cases: 237 * 238 * <ul><li>If the argument is NaN or less than zero, then the result 239 * is NaN. 240 * <li>If the argument is positive infinity, then the result is 241 * positive infinity. 242 * <li>If the argument is positive zero or negative zero, then the 243 * result is negative infinity. 244 * <li> If the argument is equal to 10<sup><i>n</i></sup> for 245 * integer <i>n</i>, then the result is <i>n</i>. 246 * </ul> 247 * 248 * @param a a value 249 * @return the base 10 logarithm of {@code a}. 250 * @since 1.5 251 */ 252 public static native double log10(double a); 253 254 /** 255 * Returns the correctly rounded positive square root of a 256 * {@code double} value. 257 * Special cases: 258 * <ul><li>If the argument is NaN or less than zero, then the result 259 * is NaN. 260 * <li>If the argument is positive infinity, then the result is positive 261 * infinity. 262 * <li>If the argument is positive zero or negative zero, then the 263 * result is the same as the argument.</ul> 264 * Otherwise, the result is the {@code double} value closest to 265 * the true mathematical square root of the argument value. 266 * 267 * @param a a value. 268 * @return the positive square root of {@code a}. 269 */ 270 public static native double sqrt(double a); 271 272 /** 273 * Returns the cube root of a {@code double} value. For 274 * positive finite {@code x}, {@code cbrt(-x) == 275 * -cbrt(x)}; that is, the cube root of a negative value is 276 * the negative of the cube root of that value's magnitude. 277 * Special cases: 278 * 279 * <ul> 280 * 281 * <li>If the argument is NaN, then the result is NaN. 282 * 283 * <li>If the argument is infinite, then the result is an infinity 284 * with the same sign as the argument. 285 * 286 * <li>If the argument is zero, then the result is a zero with the 287 * same sign as the argument. 288 * 289 * </ul> 290 * 291 * @param a a value. 292 * @return the cube root of {@code a}. 293 * @since 1.5 294 */ 295 public static native double cbrt(double a); 296 297 /** 298 * Computes the remainder operation on two arguments as prescribed 299 * by the IEEE 754 standard. 300 * The remainder value is mathematically equal to 301 * <code>f1 - f2</code> × <i>n</i>, 302 * where <i>n</i> is the mathematical integer closest to the exact 303 * mathematical value of the quotient {@code f1/f2}, and if two 304 * mathematical integers are equally close to {@code f1/f2}, 305 * then <i>n</i> is the integer that is even. If the remainder is 306 * zero, its sign is the same as the sign of the first argument. 307 * Special cases: 308 * <ul><li>If either argument is NaN, or the first argument is infinite, 309 * or the second argument is positive zero or negative zero, then the 310 * result is NaN. 311 * <li>If the first argument is finite and the second argument is 312 * infinite, then the result is the same as the first argument.</ul> 313 * 314 * @param f1 the dividend. 315 * @param f2 the divisor. 316 * @return the remainder when {@code f1} is divided by 317 * {@code f2}. 318 */ 319 public static native double IEEEremainder(double f1, double f2); 320 321 /** 322 * Returns the smallest (closest to negative infinity) 323 * {@code double} value that is greater than or equal to the 324 * argument and is equal to a mathematical integer. Special cases: 325 * <ul><li>If the argument value is already equal to a 326 * mathematical integer, then the result is the same as the 327 * argument. <li>If the argument is NaN or an infinity or 328 * positive zero or negative zero, then the result is the same as 329 * the argument. <li>If the argument value is less than zero but 330 * greater than -1.0, then the result is negative zero.</ul> Note 331 * that the value of {@code StrictMath.ceil(x)} is exactly the 332 * value of {@code -StrictMath.floor(-x)}. 333 * 334 * @param a a value. 335 * @return the smallest (closest to negative infinity) 336 * floating-point value that is greater than or equal to 337 * the argument and is equal to a mathematical integer. 338 */ 339 public static double ceil(double a) { 340 return floorOrCeil(a, -0.0, 1.0, 1.0); 341 } 342 343 /** 344 * Returns the largest (closest to positive infinity) 345 * {@code double} value that is less than or equal to the 346 * argument and is equal to a mathematical integer. Special cases: 347 * <ul><li>If the argument value is already equal to a 348 * mathematical integer, then the result is the same as the 349 * argument. <li>If the argument is NaN or an infinity or 350 * positive zero or negative zero, then the result is the same as 351 * the argument.</ul> 352 * 353 * @param a a value. 354 * @return the largest (closest to positive infinity) 355 * floating-point value that less than or equal to the argument 356 * and is equal to a mathematical integer. 357 */ 358 public static double floor(double a) { 359 return floorOrCeil(a, -1.0, 0.0, -1.0); 360 } 361 362 /** 363 * Internal method to share logic between floor and ceil. 364 * 365 * @param a the value to be floored or ceiled 366 * @param negativeBoundary result for values in (-1, 0) 367 * @param positiveBoundary result for values in (0, 1) 368 * @param increment value to add when the argument is non-integral 369 */ 370 private static double floorOrCeil(double a, 371 double negativeBoundary, 372 double positiveBoundary, 373 double sign) { 374 int exponent = Math.getExponent(a); 375 376 if (exponent < 0) { 377 /* 378 * Absolute value of argument is less than 1. 379 * floorOrceil(-0.0) => -0.0 380 * floorOrceil(+0.0) => +0.0 381 */ 382 return ((a == 0.0) ? a : 383 ( (a < 0.0) ? negativeBoundary : positiveBoundary) ); 384 } else if (exponent >= 52) { 385 /* 386 * Infinity, NaN, or a value so large it must be integral. 387 */ 388 return a; 389 } 390 // Else the argument is either an integral value already XOR it 391 // has to be rounded to one. 392 assert exponent >= 0 && exponent <= 51; 393 394 long doppel = Double.doubleToRawLongBits(a); 395 long mask = DoubleConsts.SIGNIF_BIT_MASK >> exponent; 396 397 if ( (mask & doppel) == 0L ) 398 return a; // integral value 399 else { 400 double result = Double.longBitsToDouble(doppel & (~mask)); 401 if (sign*a > 0.0) 402 result = result + sign; 403 return result; 404 } 405 } 406 407 /** 408 * Returns the {@code double} value that is closest in value 409 * to the argument and is equal to a mathematical integer. If two 410 * {@code double} values that are mathematical integers are 411 * equally close to the value of the argument, the result is the 412 * integer value that is even. Special cases: 413 * <ul><li>If the argument value is already equal to a mathematical 414 * integer, then the result is the same as the argument. 415 * <li>If the argument is NaN or an infinity or positive zero or negative 416 * zero, then the result is the same as the argument.</ul> 417 * 418 * @param a a value. 419 * @return the closest floating-point value to {@code a} that is 420 * equal to a mathematical integer. 421 * @author Joseph D. Darcy 422 */ 423 public static double rint(double a) { 424 /* 425 * If the absolute value of a is not less than 2^52, it 426 * is either a finite integer (the double format does not have 427 * enough significand bits for a number that large to have any 428 * fractional portion), an infinity, or a NaN. In any of 429 * these cases, rint of the argument is the argument. 430 * 431 * Otherwise, the sum (twoToThe52 + a ) will properly round 432 * away any fractional portion of a since ulp(twoToThe52) == 433 * 1.0; subtracting out twoToThe52 from this sum will then be 434 * exact and leave the rounded integer portion of a. 435 * 436 * This method does *not* need to be declared strictfp to get 437 * fully reproducible results. Whether or not a method is 438 * declared strictfp can only make a difference in the 439 * returned result if some operation would overflow or 440 * underflow with strictfp semantics. The operation 441 * (twoToThe52 + a ) cannot overflow since large values of a 442 * are screened out; the add cannot underflow since twoToThe52 443 * is too large. The subtraction ((twoToThe52 + a ) - 444 * twoToThe52) will be exact as discussed above and thus 445 * cannot overflow or meaningfully underflow. Finally, the 446 * last multiply in the return statement is by plus or minus 447 * 1.0, which is exact too. 448 */ 449 double twoToThe52 = (double)(1L << 52); // 2^52 450 double sign = Math.copySign(1.0, a); // preserve sign info 451 a = Math.abs(a); 452 453 if (a < twoToThe52) { // E_min <= ilogb(a) <= 51 454 a = ((twoToThe52 + a ) - twoToThe52); 455 } 456 457 return sign * a; // restore original sign 458 } 459 460 /** 461 * Returns the angle <i>theta</i> from the conversion of rectangular 462 * coordinates ({@code x}, {@code y}) to polar 463 * coordinates (r, <i>theta</i>). 464 * This method computes the phase <i>theta</i> by computing an arc tangent 465 * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special 466 * cases: 467 * <ul><li>If either argument is NaN, then the result is NaN. 468 * <li>If the first argument is positive zero and the second argument 469 * is positive, or the first argument is positive and finite and the 470 * second argument is positive infinity, then the result is positive 471 * zero. 472 * <li>If the first argument is negative zero and the second argument 473 * is positive, or the first argument is negative and finite and the 474 * second argument is positive infinity, then the result is negative zero. 475 * <li>If the first argument is positive zero and the second argument 476 * is negative, or the first argument is positive and finite and the 477 * second argument is negative infinity, then the result is the 478 * {@code double} value closest to <i>pi</i>. 479 * <li>If the first argument is negative zero and the second argument 480 * is negative, or the first argument is negative and finite and the 481 * second argument is negative infinity, then the result is the 482 * {@code double} value closest to -<i>pi</i>. 483 * <li>If the first argument is positive and the second argument is 484 * positive zero or negative zero, or the first argument is positive 485 * infinity and the second argument is finite, then the result is the 486 * {@code double} value closest to <i>pi</i>/2. 487 * <li>If the first argument is negative and the second argument is 488 * positive zero or negative zero, or the first argument is negative 489 * infinity and the second argument is finite, then the result is the 490 * {@code double} value closest to -<i>pi</i>/2. 491 * <li>If both arguments are positive infinity, then the result is the 492 * {@code double} value closest to <i>pi</i>/4. 493 * <li>If the first argument is positive infinity and the second argument 494 * is negative infinity, then the result is the {@code double} 495 * value closest to 3*<i>pi</i>/4. 496 * <li>If the first argument is negative infinity and the second argument 497 * is positive infinity, then the result is the {@code double} value 498 * closest to -<i>pi</i>/4. 499 * <li>If both arguments are negative infinity, then the result is the 500 * {@code double} value closest to -3*<i>pi</i>/4.</ul> 501 * 502 * @param y the ordinate coordinate 503 * @param x the abscissa coordinate 504 * @return the <i>theta</i> component of the point 505 * (<i>r</i>, <i>theta</i>) 506 * in polar coordinates that corresponds to the point 507 * (<i>x</i>, <i>y</i>) in Cartesian coordinates. 508 */ 509 public static native double atan2(double y, double x); 510 511 512 /** 513 * Returns the value of the first argument raised to the power of the 514 * second argument. Special cases: 515 * 516 * <ul><li>If the second argument is positive or negative zero, then the 517 * result is 1.0. 518 * <li>If the second argument is 1.0, then the result is the same as the 519 * first argument. 520 * <li>If the second argument is NaN, then the result is NaN. 521 * <li>If the first argument is NaN and the second argument is nonzero, 522 * then the result is NaN. 523 * 524 * <li>If 525 * <ul> 526 * <li>the absolute value of the first argument is greater than 1 527 * and the second argument is positive infinity, or 528 * <li>the absolute value of the first argument is less than 1 and 529 * the second argument is negative infinity, 530 * </ul> 531 * then the result is positive infinity. 532 * 533 * <li>If 534 * <ul> 535 * <li>the absolute value of the first argument is greater than 1 and 536 * the second argument is negative infinity, or 537 * <li>the absolute value of the 538 * first argument is less than 1 and the second argument is positive 539 * infinity, 540 * </ul> 541 * then the result is positive zero. 542 * 543 * <li>If the absolute value of the first argument equals 1 and the 544 * second argument is infinite, then the result is NaN. 545 * 546 * <li>If 547 * <ul> 548 * <li>the first argument is positive zero and the second argument 549 * is greater than zero, or 550 * <li>the first argument is positive infinity and the second 551 * argument is less than zero, 552 * </ul> 553 * then the result is positive zero. 554 * 555 * <li>If 556 * <ul> 557 * <li>the first argument is positive zero and the second argument 558 * is less than zero, or 559 * <li>the first argument is positive infinity and the second 560 * argument is greater than zero, 561 * </ul> 562 * then the result is positive infinity. 563 * 564 * <li>If 565 * <ul> 566 * <li>the first argument is negative zero and the second argument 567 * is greater than zero but not a finite odd integer, or 568 * <li>the first argument is negative infinity and the second 569 * argument is less than zero but not a finite odd integer, 570 * </ul> 571 * then the result is positive zero. 572 * 573 * <li>If 574 * <ul> 575 * <li>the first argument is negative zero and the second argument 576 * is a positive finite odd integer, or 577 * <li>the first argument is negative infinity and the second 578 * argument is a negative finite odd integer, 579 * </ul> 580 * then the result is negative zero. 581 * 582 * <li>If 583 * <ul> 584 * <li>the first argument is negative zero and the second argument 585 * is less than zero but not a finite odd integer, or 586 * <li>the first argument is negative infinity and the second 587 * argument is greater than zero but not a finite odd integer, 588 * </ul> 589 * then the result is positive infinity. 590 * 591 * <li>If 592 * <ul> 593 * <li>the first argument is negative zero and the second argument 594 * is a negative finite odd integer, or 595 * <li>the first argument is negative infinity and the second 596 * argument is a positive finite odd integer, 597 * </ul> 598 * then the result is negative infinity. 599 * 600 * <li>If the first argument is finite and less than zero 601 * <ul> 602 * <li> if the second argument is a finite even integer, the 603 * result is equal to the result of raising the absolute value of 604 * the first argument to the power of the second argument 605 * 606 * <li>if the second argument is a finite odd integer, the result 607 * is equal to the negative of the result of raising the absolute 608 * value of the first argument to the power of the second 609 * argument 610 * 611 * <li>if the second argument is finite and not an integer, then 612 * the result is NaN. 613 * </ul> 614 * 615 * <li>If both arguments are integers, then the result is exactly equal 616 * to the mathematical result of raising the first argument to the power 617 * of the second argument if that result can in fact be represented 618 * exactly as a {@code double} value.</ul> 619 * 620 * <p>(In the foregoing descriptions, a floating-point value is 621 * considered to be an integer if and only if it is finite and a 622 * fixed point of the method {@link #ceil ceil} or, 623 * equivalently, a fixed point of the method {@link #floor 624 * floor}. A value is a fixed point of a one-argument 625 * method if and only if the result of applying the method to the 626 * value is equal to the value.) 627 * 628 * @param a base. 629 * @param b the exponent. 630 * @return the value {@code a}<sup>{@code b}</sup>. 631 */ 632 public static native double pow(double a, double b); 633 634 /** 635 * Returns the closest {@code int} to the argument, with ties 636 * rounding up. 637 * 638 * <p>Special cases: 639 * <ul><li>If the argument is NaN, the result is 0. 640 * <li>If the argument is negative infinity or any value less than or 641 * equal to the value of {@code Integer.MIN_VALUE}, the result is 642 * equal to the value of {@code Integer.MIN_VALUE}. 643 * <li>If the argument is positive infinity or any value greater than or 644 * equal to the value of {@code Integer.MAX_VALUE}, the result is 645 * equal to the value of {@code Integer.MAX_VALUE}.</ul> 646 * 647 * @param a a floating-point value to be rounded to an integer. 648 * @return the value of the argument rounded to the nearest 649 * {@code int} value. 650 * @see java.lang.Integer#MAX_VALUE 651 * @see java.lang.Integer#MIN_VALUE 652 */ 653 public static int round(float a) { 654 return Math.round(a); 655 } 656 657 /** 658 * Returns the closest {@code long} to the argument, with ties 659 * rounding up. 660 * 661 * <p>Special cases: 662 * <ul><li>If the argument is NaN, the result is 0. 663 * <li>If the argument is negative infinity or any value less than or 664 * equal to the value of {@code Long.MIN_VALUE}, the result is 665 * equal to the value of {@code Long.MIN_VALUE}. 666 * <li>If the argument is positive infinity or any value greater than or 667 * equal to the value of {@code Long.MAX_VALUE}, the result is 668 * equal to the value of {@code Long.MAX_VALUE}.</ul> 669 * 670 * @param a a floating-point value to be rounded to a 671 * {@code long}. 672 * @return the value of the argument rounded to the nearest 673 * {@code long} value. 674 * @see java.lang.Long#MAX_VALUE 675 * @see java.lang.Long#MIN_VALUE 676 */ 677 public static long round(double a) { 678 return Math.round(a); 679 } 680 681 private static Random randomNumberGenerator; 682 683 private static synchronized Random initRNG() { 684 Random rnd = randomNumberGenerator; 685 return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd; 686 } 687 688 /** 689 * Returns a {@code double} value with a positive sign, greater 690 * than or equal to {@code 0.0} and less than {@code 1.0}. 691 * Returned values are chosen pseudorandomly with (approximately) 692 * uniform distribution from that range. 693 * 694 * <p>When this method is first called, it creates a single new 695 * pseudorandom-number generator, exactly as if by the expression 696 * 697 * <blockquote>{@code new java.util.Random()}</blockquote> 698 * 699 * This new pseudorandom-number generator is used thereafter for 700 * all calls to this method and is used nowhere else. 701 * 702 * <p>This method is properly synchronized to allow correct use by 703 * more than one thread. However, if many threads need to generate 704 * pseudorandom numbers at a great rate, it may reduce contention 705 * for each thread to have its own pseudorandom-number generator. 706 * 707 * @return a pseudorandom {@code double} greater than or equal 708 * to {@code 0.0} and less than {@code 1.0}. 709 * @see Random#nextDouble() 710 */ 711 public static double random() { 712 Random rnd = randomNumberGenerator; 713 if (rnd == null) rnd = initRNG(); 714 return rnd.nextDouble(); 715 } 716 717 /** 718 * Returns the sum of its arguments, 719 * throwing an exception if the result overflows an {@code int}. 720 * 721 * @param x the first value 722 * @param y the second value 723 * @return the result 724 * @throws ArithmeticException if the result overflows an int 725 * @see Math#addExact(int,int) 726 * @since 1.8 727 */ 728 public static int addExact(int x, int y) { 729 return Math.addExact(x, y); 730 } 731 732 /** 733 * Returns the sum of its arguments, 734 * throwing an exception if the result overflows a {@code long}. 735 * 736 * @param x the first value 737 * @param y the second value 738 * @return the result 739 * @throws ArithmeticException if the result overflows a long 740 * @see Math#addExact(long,long) 741 * @since 1.8 742 */ 743 public static long addExact(long x, long y) { 744 return Math.addExact(x, y); 745 } 746 747 /** 748 * Returns the difference of the arguments, 749 * throwing an exception if the result overflows an {@code int}. 750 * 751 * @param x the first value 752 * @param y the second value to subtract from the first 753 * @return the result 754 * @throws ArithmeticException if the result overflows an int 755 * @see Math#subtractExact(int,int) 756 * @since 1.8 757 */ 758 public static int subtractExact(int x, int y) { 759 return Math.subtractExact(x, y); 760 } 761 762 /** 763 * Returns the difference of the arguments, 764 * throwing an exception if the result overflows a {@code long}. 765 * 766 * @param x the first value 767 * @param y the second value to subtract from the first 768 * @return the result 769 * @throws ArithmeticException if the result overflows a long 770 * @see Math#subtractExact(long,long) 771 * @since 1.8 772 */ 773 public static long subtractExact(long x, long y) { 774 return Math.subtractExact(x, y); 775 } 776 777 /** 778 * Returns the product of the arguments, 779 * throwing an exception if the result overflows an {@code int}. 780 * 781 * @param x the first value 782 * @param y the second value 783 * @return the result 784 * @throws ArithmeticException if the result overflows an int 785 * @see Math#multiplyExact(int,int) 786 * @since 1.8 787 */ 788 public static int multiplyExact(int x, int y) { 789 return Math.multiplyExact(x, y); 790 } 791 792 /** 793 * Returns the product of the arguments, 794 * throwing an exception if the result overflows a {@code long}. 795 * 796 * @param x the first value 797 * @param y the second value 798 * @return the result 799 * @throws ArithmeticException if the result overflows a long 800 * @see Math#multiplyExact(long,long) 801 * @since 1.8 802 */ 803 public static long multiplyExact(long x, long y) { 804 return Math.multiplyExact(x, y); 805 } 806 807 /** 808 * Returns the value of the {@code long} argument; 809 * throwing an exception if the value overflows an {@code int}. 810 * 811 * @param value the long value 812 * @return the argument as an int 813 * @throws ArithmeticException if the {@code argument} overflows an int 814 * @see Math#toIntExact(long) 815 * @since 1.8 816 */ 817 public static int toIntExact(long value) { 818 return Math.toIntExact(value); 819 } 820 821 /** 822 * Returns the largest (closest to positive infinity) 823 * {@code int} value that is less than or equal to the algebraic quotient. 824 * There is one special case, if the dividend is the 825 * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1}, 826 * then integer overflow occurs and 827 * the result is equal to the {@code Integer.MIN_VALUE}. 828 * <p> 829 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 830 * a comparison to the integer division {@code /} operator. 831 * 832 * @param x the dividend 833 * @param y the divisor 834 * @return the largest (closest to positive infinity) 835 * {@code int} value that is less than or equal to the algebraic quotient. 836 * @throws ArithmeticException if the divisor {@code y} is zero 837 * @see Math#floorDiv(int, int) 838 * @see Math#floor(double) 839 * @since 1.8 840 */ 841 public static int floorDiv(int x, int y) { 842 return Math.floorDiv(x, y); 843 } 844 845 /** 846 * Returns the largest (closest to positive infinity) 847 * {@code long} value that is less than or equal to the algebraic quotient. 848 * There is one special case, if the dividend is the 849 * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, 850 * then integer overflow occurs and 851 * the result is equal to the {@code Long.MIN_VALUE}. 852 * <p> 853 * See {@link Math#floorDiv(int, int) Math.floorDiv} for examples and 854 * a comparison to the integer division {@code /} operator. 855 * 856 * @param x the dividend 857 * @param y the divisor 858 * @return the largest (closest to positive infinity) 859 * {@code long} value that is less than or equal to the algebraic quotient. 860 * @throws ArithmeticException if the divisor {@code y} is zero 861 * @see Math#floorDiv(long, long) 862 * @see Math#floor(double) 863 * @since 1.8 864 */ 865 public static long floorDiv(long x, long y) { 866 return Math.floorDiv(x, y); 867 } 868 869 /** 870 * Returns the floor modulus of the {@code int} arguments. 871 * <p> 872 * The floor modulus is {@code x - (floorDiv(x, y) * y)}, 873 * has the same sign as the divisor {@code y}, and 874 * is in the range of {@code -abs(y) < r < +abs(y)}. 875 * <p> 876 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 877 * <ul> 878 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} 879 * </ul> 880 * <p> 881 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 882 * a comparison to the {@code %} operator. 883 * 884 * @param x the dividend 885 * @param y the divisor 886 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 887 * @throws ArithmeticException if the divisor {@code y} is zero 888 * @see Math#floorMod(int, int) 889 * @see StrictMath#floorDiv(int, int) 890 * @since 1.8 891 */ 892 public static int floorMod(int x, int y) { 893 return Math.floorMod(x , y); 894 } 895 /** 896 * Returns the floor modulus of the {@code long} arguments. 897 * <p> 898 * The floor modulus is {@code x - (floorDiv(x, y) * y)}, 899 * has the same sign as the divisor {@code y}, and 900 * is in the range of {@code -abs(y) < r < +abs(y)}. 901 * <p> 902 * The relationship between {@code floorDiv} and {@code floorMod} is such that: 903 * <ul> 904 * <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x} 905 * </ul> 906 * <p> 907 * See {@link Math#floorMod(int, int) Math.floorMod} for examples and 908 * a comparison to the {@code %} operator. 909 * 910 * @param x the dividend 911 * @param y the divisor 912 * @return the floor modulus {@code x - (floorDiv(x, y) * y)} 913 * @throws ArithmeticException if the divisor {@code y} is zero 914 * @see Math#floorMod(long, long) 915 * @see StrictMath#floorDiv(long, long) 916 * @since 1.8 917 */ 918 public static long floorMod(long x, long y) { 919 return Math.floorMod(x, y); 920 } 921 922 /** 923 * Returns the absolute value of an {@code int} value. 924 * If the argument is not negative, the argument is returned. 925 * If the argument is negative, the negation of the argument is returned. 926 * 927 * <p>Note that if the argument is equal to the value of 928 * {@link Integer#MIN_VALUE}, the most negative representable 929 * {@code int} value, the result is that same value, which is 930 * negative. 931 * 932 * @param a the argument whose absolute value is to be determined. 933 * @return the absolute value of the argument. 934 */ 935 public static int abs(int a) { 936 return Math.abs(a); 937 } 938 939 /** 940 * Returns the absolute value of a {@code long} value. 941 * If the argument is not negative, the argument is returned. 942 * If the argument is negative, the negation of the argument is returned. 943 * 944 * <p>Note that if the argument is equal to the value of 945 * {@link Long#MIN_VALUE}, the most negative representable 946 * {@code long} value, the result is that same value, which 947 * is negative. 948 * 949 * @param a the argument whose absolute value is to be determined. 950 * @return the absolute value of the argument. 951 */ 952 public static long abs(long a) { 953 return Math.abs(a); 954 } 955 956 /** 957 * Returns the absolute value of a {@code float} value. 958 * If the argument is not negative, the argument is returned. 959 * If the argument is negative, the negation of the argument is returned. 960 * Special cases: 961 * <ul><li>If the argument is positive zero or negative zero, the 962 * result is positive zero. 963 * <li>If the argument is infinite, the result is positive infinity. 964 * <li>If the argument is NaN, the result is NaN.</ul> 965 * In other words, the result is the same as the value of the expression: 966 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))} 967 * 968 * @param a the argument whose absolute value is to be determined 969 * @return the absolute value of the argument. 970 */ 971 public static float abs(float a) { 972 return Math.abs(a); 973 } 974 975 /** 976 * Returns the absolute value of a {@code double} value. 977 * If the argument is not negative, the argument is returned. 978 * If the argument is negative, the negation of the argument is returned. 979 * Special cases: 980 * <ul><li>If the argument is positive zero or negative zero, the result 981 * is positive zero. 982 * <li>If the argument is infinite, the result is positive infinity. 983 * <li>If the argument is NaN, the result is NaN.</ul> 984 * In other words, the result is the same as the value of the expression: 985 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)} 986 * 987 * @param a the argument whose absolute value is to be determined 988 * @return the absolute value of the argument. 989 */ 990 public static double abs(double a) { 991 return Math.abs(a); 992 } 993 994 /** 995 * Returns the greater of two {@code int} values. That is, the 996 * result is the argument closer to the value of 997 * {@link Integer#MAX_VALUE}. If the arguments have the same value, 998 * the result is that same value. 999 * 1000 * @param a an argument. 1001 * @param b another argument. 1002 * @return the larger of {@code a} and {@code b}. 1003 */ 1004 public static int max(int a, int b) { 1005 return Math.max(a, b); 1006 } 1007 1008 /** 1009 * Returns the greater of two {@code long} values. That is, the 1010 * result is the argument closer to the value of 1011 * {@link Long#MAX_VALUE}. If the arguments have the same value, 1012 * the result is that same value. 1013 * 1014 * @param a an argument. 1015 * @param b another argument. 1016 * @return the larger of {@code a} and {@code b}. 1017 */ 1018 public static long max(long a, long b) { 1019 return Math.max(a, b); 1020 } 1021 1022 /** 1023 * Returns the greater of two {@code float} values. That is, 1024 * the result is the argument closer to positive infinity. If the 1025 * arguments have the same value, the result is that same 1026 * value. If either value is NaN, then the result is NaN. Unlike 1027 * the numerical comparison operators, this method considers 1028 * negative zero to be strictly smaller than positive zero. If one 1029 * argument is positive zero and the other negative zero, the 1030 * result is positive zero. 1031 * 1032 * @param a an argument. 1033 * @param b another argument. 1034 * @return the larger of {@code a} and {@code b}. 1035 */ 1036 public static float max(float a, float b) { 1037 return Math.max(a, b); 1038 } 1039 1040 /** 1041 * Returns the greater of two {@code double} values. That 1042 * is, the result is the argument closer to positive infinity. If 1043 * the arguments have the same value, the result is that same 1044 * value. If either value is NaN, then the result is NaN. Unlike 1045 * the numerical comparison operators, this method considers 1046 * negative zero to be strictly smaller than positive zero. If one 1047 * argument is positive zero and the other negative zero, the 1048 * result is positive zero. 1049 * 1050 * @param a an argument. 1051 * @param b another argument. 1052 * @return the larger of {@code a} and {@code b}. 1053 */ 1054 public static double max(double a, double b) { 1055 return Math.max(a, b); 1056 } 1057 1058 /** 1059 * Returns the smaller of two {@code int} values. That is, 1060 * the result the argument closer to the value of 1061 * {@link Integer#MIN_VALUE}. If the arguments have the same 1062 * value, the result is that same value. 1063 * 1064 * @param a an argument. 1065 * @param b another argument. 1066 * @return the smaller of {@code a} and {@code b}. 1067 */ 1068 public static int min(int a, int b) { 1069 return Math.min(a, b); 1070 } 1071 1072 /** 1073 * Returns the smaller of two {@code long} values. That is, 1074 * the result is the argument closer to the value of 1075 * {@link Long#MIN_VALUE}. If the arguments have the same 1076 * value, the result is that same value. 1077 * 1078 * @param a an argument. 1079 * @param b another argument. 1080 * @return the smaller of {@code a} and {@code b}. 1081 */ 1082 public static long min(long a, long b) { 1083 return Math.min(a, b); 1084 } 1085 1086 /** 1087 * Returns the smaller of two {@code float} values. That is, 1088 * the result is the value closer to negative infinity. If the 1089 * arguments have the same value, the result is that same 1090 * value. If either value is NaN, then the result is NaN. Unlike 1091 * the numerical comparison operators, this method considers 1092 * negative zero to be strictly smaller than positive zero. If 1093 * one argument is positive zero and the other is negative zero, 1094 * the result is negative zero. 1095 * 1096 * @param a an argument. 1097 * @param b another argument. 1098 * @return the smaller of {@code a} and {@code b.} 1099 */ 1100 public static float min(float a, float b) { 1101 return Math.min(a, b); 1102 } 1103 1104 /** 1105 * Returns the smaller of two {@code double} values. That 1106 * is, the result is the value closer to negative infinity. If the 1107 * arguments have the same value, the result is that same 1108 * value. If either value is NaN, then the result is NaN. Unlike 1109 * the numerical comparison operators, this method considers 1110 * negative zero to be strictly smaller than positive zero. If one 1111 * argument is positive zero and the other is negative zero, the 1112 * result is negative zero. 1113 * 1114 * @param a an argument. 1115 * @param b another argument. 1116 * @return the smaller of {@code a} and {@code b}. 1117 */ 1118 public static double min(double a, double b) { 1119 return Math.min(a, b); 1120 } 1121 1122 /** 1123 * Returns the size of an ulp of the argument. An ulp, unit in 1124 * the last place, of a {@code double} value is the positive 1125 * distance between this floating-point value and the {@code 1126 * double} value next larger in magnitude. Note that for non-NaN 1127 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1128 * 1129 * <p>Special Cases: 1130 * <ul> 1131 * <li> If the argument is NaN, then the result is NaN. 1132 * <li> If the argument is positive or negative infinity, then the 1133 * result is positive infinity. 1134 * <li> If the argument is positive or negative zero, then the result is 1135 * {@code Double.MIN_VALUE}. 1136 * <li> If the argument is ±{@code Double.MAX_VALUE}, then 1137 * the result is equal to 2<sup>971</sup>. 1138 * </ul> 1139 * 1140 * @param d the floating-point value whose ulp is to be returned 1141 * @return the size of an ulp of the argument 1142 * @author Joseph D. Darcy 1143 * @since 1.5 1144 */ 1145 public static double ulp(double d) { 1146 return Math.ulp(d); 1147 } 1148 1149 /** 1150 * Returns the size of an ulp of the argument. An ulp, unit in 1151 * the last place, of a {@code float} value is the positive 1152 * distance between this floating-point value and the {@code 1153 * float} value next larger in magnitude. Note that for non-NaN 1154 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1155 * 1156 * <p>Special Cases: 1157 * <ul> 1158 * <li> If the argument is NaN, then the result is NaN. 1159 * <li> If the argument is positive or negative infinity, then the 1160 * result is positive infinity. 1161 * <li> If the argument is positive or negative zero, then the result is 1162 * {@code Float.MIN_VALUE}. 1163 * <li> If the argument is ±{@code Float.MAX_VALUE}, then 1164 * the result is equal to 2<sup>104</sup>. 1165 * </ul> 1166 * 1167 * @param f the floating-point value whose ulp is to be returned 1168 * @return the size of an ulp of the argument 1169 * @author Joseph D. Darcy 1170 * @since 1.5 1171 */ 1172 public static float ulp(float f) { 1173 return Math.ulp(f); 1174 } 1175 1176 /** 1177 * Returns the signum function of the argument; zero if the argument 1178 * is zero, 1.0 if the argument is greater than zero, -1.0 if the 1179 * argument is less than zero. 1180 * 1181 * <p>Special Cases: 1182 * <ul> 1183 * <li> If the argument is NaN, then the result is NaN. 1184 * <li> If the argument is positive zero or negative zero, then the 1185 * result is the same as the argument. 1186 * </ul> 1187 * 1188 * @param d the floating-point value whose signum is to be returned 1189 * @return the signum function of the argument 1190 * @author Joseph D. Darcy 1191 * @since 1.5 1192 */ 1193 public static double signum(double d) { 1194 return Math.signum(d); 1195 } 1196 1197 /** 1198 * Returns the signum function of the argument; zero if the argument 1199 * is zero, 1.0f if the argument is greater than zero, -1.0f if the 1200 * argument is less than zero. 1201 * 1202 * <p>Special Cases: 1203 * <ul> 1204 * <li> If the argument is NaN, then the result is NaN. 1205 * <li> If the argument is positive zero or negative zero, then the 1206 * result is the same as the argument. 1207 * </ul> 1208 * 1209 * @param f the floating-point value whose signum is to be returned 1210 * @return the signum function of the argument 1211 * @author Joseph D. Darcy 1212 * @since 1.5 1213 */ 1214 public static float signum(float f) { 1215 return Math.signum(f); 1216 } 1217 1218 /** 1219 * Returns the hyperbolic sine of a {@code double} value. 1220 * The hyperbolic sine of <i>x</i> is defined to be 1221 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2 1222 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1223 * 1224 * <p>Special cases: 1225 * <ul> 1226 * 1227 * <li>If the argument is NaN, then the result is NaN. 1228 * 1229 * <li>If the argument is infinite, then the result is an infinity 1230 * with the same sign as the argument. 1231 * 1232 * <li>If the argument is zero, then the result is a zero with the 1233 * same sign as the argument. 1234 * 1235 * </ul> 1236 * 1237 * @param x The number whose hyperbolic sine is to be returned. 1238 * @return The hyperbolic sine of {@code x}. 1239 * @since 1.5 1240 */ 1241 public static native double sinh(double x); 1242 1243 /** 1244 * Returns the hyperbolic cosine of a {@code double} value. 1245 * The hyperbolic cosine of <i>x</i> is defined to be 1246 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2 1247 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1248 * 1249 * <p>Special cases: 1250 * <ul> 1251 * 1252 * <li>If the argument is NaN, then the result is NaN. 1253 * 1254 * <li>If the argument is infinite, then the result is positive 1255 * infinity. 1256 * 1257 * <li>If the argument is zero, then the result is {@code 1.0}. 1258 * 1259 * </ul> 1260 * 1261 * @param x The number whose hyperbolic cosine is to be returned. 1262 * @return The hyperbolic cosine of {@code x}. 1263 * @since 1.5 1264 */ 1265 public static native double cosh(double x); 1266 1267 /** 1268 * Returns the hyperbolic tangent of a {@code double} value. 1269 * The hyperbolic tangent of <i>x</i> is defined to be 1270 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>), 1271 * in other words, {@linkplain Math#sinh 1272 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note 1273 * that the absolute value of the exact tanh is always less than 1274 * 1. 1275 * 1276 * <p>Special cases: 1277 * <ul> 1278 * 1279 * <li>If the argument is NaN, then the result is NaN. 1280 * 1281 * <li>If the argument is zero, then the result is a zero with the 1282 * same sign as the argument. 1283 * 1284 * <li>If the argument is positive infinity, then the result is 1285 * {@code +1.0}. 1286 * 1287 * <li>If the argument is negative infinity, then the result is 1288 * {@code -1.0}. 1289 * 1290 * </ul> 1291 * 1292 * @param x The number whose hyperbolic tangent is to be returned. 1293 * @return The hyperbolic tangent of {@code x}. 1294 * @since 1.5 1295 */ 1296 public static native double tanh(double x); 1297 1298 /** 1299 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1300 * without intermediate overflow or underflow. 1301 * 1302 * <p>Special cases: 1303 * <ul> 1304 * 1305 * <li> If either argument is infinite, then the result 1306 * is positive infinity. 1307 * 1308 * <li> If either argument is NaN and neither argument is infinite, 1309 * then the result is NaN. 1310 * 1311 * </ul> 1312 * 1313 * @param x a value 1314 * @param y a value 1315 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1316 * without intermediate overflow or underflow 1317 * @since 1.5 1318 */ 1319 public static native double hypot(double x, double y); 1320 1321 /** 1322 * Returns <i>e</i><sup>x</sup> -1. Note that for values of 1323 * <i>x</i> near 0, the exact sum of 1324 * {@code expm1(x)} + 1 is much closer to the true 1325 * result of <i>e</i><sup>x</sup> than {@code exp(x)}. 1326 * 1327 * <p>Special cases: 1328 * <ul> 1329 * <li>If the argument is NaN, the result is NaN. 1330 * 1331 * <li>If the argument is positive infinity, then the result is 1332 * positive infinity. 1333 * 1334 * <li>If the argument is negative infinity, then the result is 1335 * -1.0. 1336 * 1337 * <li>If the argument is zero, then the result is a zero with the 1338 * same sign as the argument. 1339 * 1340 * </ul> 1341 * 1342 * @param x the exponent to raise <i>e</i> to in the computation of 1343 * <i>e</i><sup>{@code x}</sup> -1. 1344 * @return the value <i>e</i><sup>{@code x}</sup> - 1. 1345 * @since 1.5 1346 */ 1347 public static native double expm1(double x); 1348 1349 /** 1350 * Returns the natural logarithm of the sum of the argument and 1. 1351 * Note that for small values {@code x}, the result of 1352 * {@code log1p(x)} is much closer to the true result of ln(1 1353 * + {@code x}) than the floating-point evaluation of 1354 * {@code log(1.0+x)}. 1355 * 1356 * <p>Special cases: 1357 * <ul> 1358 * 1359 * <li>If the argument is NaN or less than -1, then the result is 1360 * NaN. 1361 * 1362 * <li>If the argument is positive infinity, then the result is 1363 * positive infinity. 1364 * 1365 * <li>If the argument is negative one, then the result is 1366 * negative infinity. 1367 * 1368 * <li>If the argument is zero, then the result is a zero with the 1369 * same sign as the argument. 1370 * 1371 * </ul> 1372 * 1373 * @param x a value 1374 * @return the value ln({@code x} + 1), the natural 1375 * log of {@code x} + 1 1376 * @since 1.5 1377 */ 1378 public static native double log1p(double x); 1379 1380 /** 1381 * Returns the first floating-point argument with the sign of the 1382 * second floating-point argument. For this method, a NaN 1383 * {@code sign} argument is always treated as if it were 1384 * positive. 1385 * 1386 * @param magnitude the parameter providing the magnitude of the result 1387 * @param sign the parameter providing the sign of the result 1388 * @return a value with the magnitude of {@code magnitude} 1389 * and the sign of {@code sign}. 1390 * @since 1.6 1391 */ 1392 public static double copySign(double magnitude, double sign) { 1393 return Math.copySign(magnitude, (Double.isNaN(sign)?1.0d:sign)); 1394 } 1395 1396 /** 1397 * Returns the first floating-point argument with the sign of the 1398 * second floating-point argument. For this method, a NaN 1399 * {@code sign} argument is always treated as if it were 1400 * positive. 1401 * 1402 * @param magnitude the parameter providing the magnitude of the result 1403 * @param sign the parameter providing the sign of the result 1404 * @return a value with the magnitude of {@code magnitude} 1405 * and the sign of {@code sign}. 1406 * @since 1.6 1407 */ 1408 public static float copySign(float magnitude, float sign) { 1409 return Math.copySign(magnitude, (Float.isNaN(sign)?1.0f:sign)); 1410 } 1411 /** 1412 * Returns the unbiased exponent used in the representation of a 1413 * {@code float}. Special cases: 1414 * 1415 * <ul> 1416 * <li>If the argument is NaN or infinite, then the result is 1417 * {@link Float#MAX_EXPONENT} + 1. 1418 * <li>If the argument is zero or subnormal, then the result is 1419 * {@link Float#MIN_EXPONENT} -1. 1420 * </ul> 1421 * @param f a {@code float} value 1422 * @since 1.6 1423 */ 1424 public static int getExponent(float f) { 1425 return Math.getExponent(f); 1426 } 1427 1428 /** 1429 * Returns the unbiased exponent used in the representation of a 1430 * {@code double}. Special cases: 1431 * 1432 * <ul> 1433 * <li>If the argument is NaN or infinite, then the result is 1434 * {@link Double#MAX_EXPONENT} + 1. 1435 * <li>If the argument is zero or subnormal, then the result is 1436 * {@link Double#MIN_EXPONENT} -1. 1437 * </ul> 1438 * @param d a {@code double} value 1439 * @since 1.6 1440 */ 1441 public static int getExponent(double d) { 1442 return Math.getExponent(d); 1443 } 1444 1445 /** 1446 * Returns the floating-point number adjacent to the first 1447 * argument in the direction of the second argument. If both 1448 * arguments compare as equal the second argument is returned. 1449 * 1450 * <p>Special cases: 1451 * <ul> 1452 * <li> If either argument is a NaN, then NaN is returned. 1453 * 1454 * <li> If both arguments are signed zeros, {@code direction} 1455 * is returned unchanged (as implied by the requirement of 1456 * returning the second argument if the arguments compare as 1457 * equal). 1458 * 1459 * <li> If {@code start} is 1460 * ±{@link Double#MIN_VALUE} and {@code direction} 1461 * has a value such that the result should have a smaller 1462 * magnitude, then a zero with the same sign as {@code start} 1463 * is returned. 1464 * 1465 * <li> If {@code start} is infinite and 1466 * {@code direction} has a value such that the result should 1467 * have a smaller magnitude, {@link Double#MAX_VALUE} with the 1468 * same sign as {@code start} is returned. 1469 * 1470 * <li> If {@code start} is equal to ± 1471 * {@link Double#MAX_VALUE} and {@code direction} has a 1472 * value such that the result should have a larger magnitude, an 1473 * infinity with same sign as {@code start} is returned. 1474 * </ul> 1475 * 1476 * @param start starting floating-point value 1477 * @param direction value indicating which of 1478 * {@code start}'s neighbors or {@code start} should 1479 * be returned 1480 * @return The floating-point number adjacent to {@code start} in the 1481 * direction of {@code direction}. 1482 * @since 1.6 1483 */ 1484 public static double nextAfter(double start, double direction) { 1485 return Math.nextAfter(start, direction); 1486 } 1487 1488 /** 1489 * Returns the floating-point number adjacent to the first 1490 * argument in the direction of the second argument. If both 1491 * arguments compare as equal a value equivalent to the second argument 1492 * is returned. 1493 * 1494 * <p>Special cases: 1495 * <ul> 1496 * <li> If either argument is a NaN, then NaN is returned. 1497 * 1498 * <li> If both arguments are signed zeros, a value equivalent 1499 * to {@code direction} is returned. 1500 * 1501 * <li> If {@code start} is 1502 * ±{@link Float#MIN_VALUE} and {@code direction} 1503 * has a value such that the result should have a smaller 1504 * magnitude, then a zero with the same sign as {@code start} 1505 * is returned. 1506 * 1507 * <li> If {@code start} is infinite and 1508 * {@code direction} has a value such that the result should 1509 * have a smaller magnitude, {@link Float#MAX_VALUE} with the 1510 * same sign as {@code start} is returned. 1511 * 1512 * <li> If {@code start} is equal to ± 1513 * {@link Float#MAX_VALUE} and {@code direction} has a 1514 * value such that the result should have a larger magnitude, an 1515 * infinity with same sign as {@code start} is returned. 1516 * </ul> 1517 * 1518 * @param start starting floating-point value 1519 * @param direction value indicating which of 1520 * {@code start}'s neighbors or {@code start} should 1521 * be returned 1522 * @return The floating-point number adjacent to {@code start} in the 1523 * direction of {@code direction}. 1524 * @since 1.6 1525 */ 1526 public static float nextAfter(float start, double direction) { 1527 return Math.nextAfter(start, direction); 1528 } 1529 1530 /** 1531 * Returns the floating-point value adjacent to {@code d} in 1532 * the direction of positive infinity. This method is 1533 * semantically equivalent to {@code nextAfter(d, 1534 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp} 1535 * implementation may run faster than its equivalent 1536 * {@code nextAfter} call. 1537 * 1538 * <p>Special Cases: 1539 * <ul> 1540 * <li> If the argument is NaN, the result is NaN. 1541 * 1542 * <li> If the argument is positive infinity, the result is 1543 * positive infinity. 1544 * 1545 * <li> If the argument is zero, the result is 1546 * {@link Double#MIN_VALUE} 1547 * 1548 * </ul> 1549 * 1550 * @param d starting floating-point value 1551 * @return The adjacent floating-point value closer to positive 1552 * infinity. 1553 * @since 1.6 1554 */ 1555 public static double nextUp(double d) { 1556 return Math.nextUp(d); 1557 } 1558 1559 /** 1560 * Returns the floating-point value adjacent to {@code f} in 1561 * the direction of positive infinity. This method is 1562 * semantically equivalent to {@code nextAfter(f, 1563 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp} 1564 * implementation may run faster than its equivalent 1565 * {@code nextAfter} call. 1566 * 1567 * <p>Special Cases: 1568 * <ul> 1569 * <li> If the argument is NaN, the result is NaN. 1570 * 1571 * <li> If the argument is positive infinity, the result is 1572 * positive infinity. 1573 * 1574 * <li> If the argument is zero, the result is 1575 * {@link Float#MIN_VALUE} 1576 * 1577 * </ul> 1578 * 1579 * @param f starting floating-point value 1580 * @return The adjacent floating-point value closer to positive 1581 * infinity. 1582 * @since 1.6 1583 */ 1584 public static float nextUp(float f) { 1585 return Math.nextUp(f); 1586 } 1587 1588 /** 1589 * Returns the floating-point value adjacent to {@code d} in 1590 * the direction of negative infinity. This method is 1591 * semantically equivalent to {@code nextAfter(d, 1592 * Double.NEGATIVE_INFINITY)}; however, a 1593 * {@code nextDown} implementation may run faster than its 1594 * equivalent {@code nextAfter} call. 1595 * 1596 * <p>Special Cases: 1597 * <ul> 1598 * <li> If the argument is NaN, the result is NaN. 1599 * 1600 * <li> If the argument is negative infinity, the result is 1601 * negative infinity. 1602 * 1603 * <li> If the argument is zero, the result is 1604 * {@code -Double.MIN_VALUE} 1605 * 1606 * </ul> 1607 * 1608 * @param d starting floating-point value 1609 * @return The adjacent floating-point value closer to negative 1610 * infinity. 1611 * @since 1.8 1612 */ 1613 public static double nextDown(double d) { 1614 return Math.nextDown(d); 1615 } 1616 1617 /** 1618 * Returns the floating-point value adjacent to {@code f} in 1619 * the direction of negative infinity. This method is 1620 * semantically equivalent to {@code nextAfter(f, 1621 * Float.NEGATIVE_INFINITY)}; however, a 1622 * {@code nextDown} implementation may run faster than its 1623 * equivalent {@code nextAfter} call. 1624 * 1625 * <p>Special Cases: 1626 * <ul> 1627 * <li> If the argument is NaN, the result is NaN. 1628 * 1629 * <li> If the argument is negative infinity, the result is 1630 * negative infinity. 1631 * 1632 * <li> If the argument is zero, the result is 1633 * {@code -Float.MIN_VALUE} 1634 * 1635 * </ul> 1636 * 1637 * @param f starting floating-point value 1638 * @return The adjacent floating-point value closer to negative 1639 * infinity. 1640 * @since 1.8 1641 */ 1642 public static float nextDown(float f) { 1643 return Math.nextDown(f); 1644 } 1645 1646 /** 1647 * Returns {@code d} × 1648 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1649 * by a single correctly rounded floating-point multiply to a 1650 * member of the double value set. See the Java 1651 * Language Specification for a discussion of floating-point 1652 * value sets. If the exponent of the result is between {@link 1653 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the 1654 * answer is calculated exactly. If the exponent of the result 1655 * would be larger than {@code Double.MAX_EXPONENT}, an 1656 * infinity is returned. Note that if the result is subnormal, 1657 * precision may be lost; that is, when {@code scalb(x, n)} 1658 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1659 * <i>x</i>. When the result is non-NaN, the result has the same 1660 * sign as {@code d}. 1661 * 1662 * <p>Special cases: 1663 * <ul> 1664 * <li> If the first argument is NaN, NaN is returned. 1665 * <li> If the first argument is infinite, then an infinity of the 1666 * same sign is returned. 1667 * <li> If the first argument is zero, then a zero of the same 1668 * sign is returned. 1669 * </ul> 1670 * 1671 * @param d number to be scaled by a power of two. 1672 * @param scaleFactor power of 2 used to scale {@code d} 1673 * @return {@code d} × 2<sup>{@code scaleFactor}</sup> 1674 * @since 1.6 1675 */ 1676 public static double scalb(double d, int scaleFactor) { 1677 return Math.scalb(d, scaleFactor); 1678 } 1679 1680 /** 1681 * Returns {@code f} × 1682 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1683 * by a single correctly rounded floating-point multiply to a 1684 * member of the float value set. See the Java 1685 * Language Specification for a discussion of floating-point 1686 * value sets. If the exponent of the result is between {@link 1687 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the 1688 * answer is calculated exactly. If the exponent of the result 1689 * would be larger than {@code Float.MAX_EXPONENT}, an 1690 * infinity is returned. Note that if the result is subnormal, 1691 * precision may be lost; that is, when {@code scalb(x, n)} 1692 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1693 * <i>x</i>. When the result is non-NaN, the result has the same 1694 * sign as {@code f}. 1695 * 1696 * <p>Special cases: 1697 * <ul> 1698 * <li> If the first argument is NaN, NaN is returned. 1699 * <li> If the first argument is infinite, then an infinity of the 1700 * same sign is returned. 1701 * <li> If the first argument is zero, then a zero of the same 1702 * sign is returned. 1703 * </ul> 1704 * 1705 * @param f number to be scaled by a power of two. 1706 * @param scaleFactor power of 2 used to scale {@code f} 1707 * @return {@code f} × 2<sup>{@code scaleFactor}</sup> 1708 * @since 1.6 1709 */ 1710 public static float scalb(float f, int scaleFactor) { 1711 return Math.scalb(f, scaleFactor); 1712 } 1713 }