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