1 /* 2 * Copyright (c) 1999, 2011, 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 * Return 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 * Return 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 * Return 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 * Return 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 * Return 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(int) 815 * @since 1.8 816 */ 817 public static int toIntExact(long value) { 818 return Math.toIntExact(value); 819 } 820 821 /** 822 * Returns the absolute value of an {@code int} value. 823 * If the argument is not negative, the argument is returned. 824 * If the argument is negative, the negation of the argument is returned. 825 * 826 * <p>Note that if the argument is equal to the value of 827 * {@link Integer#MIN_VALUE}, the most negative representable 828 * {@code int} value, the result is that same value, which is 829 * negative. 830 * 831 * @param a the argument whose absolute value is to be determined. 832 * @return the absolute value of the argument. 833 */ 834 public static int abs(int a) { 835 return Math.abs(a); 836 } 837 838 /** 839 * Returns the absolute value of a {@code long} value. 840 * If the argument is not negative, the argument is returned. 841 * If the argument is negative, the negation of the argument is returned. 842 * 843 * <p>Note that if the argument is equal to the value of 844 * {@link Long#MIN_VALUE}, the most negative representable 845 * {@code long} value, the result is that same value, which 846 * is negative. 847 * 848 * @param a the argument whose absolute value is to be determined. 849 * @return the absolute value of the argument. 850 */ 851 public static long abs(long a) { 852 return Math.abs(a); 853 } 854 855 /** 856 * Returns the absolute value of a {@code float} value. 857 * If the argument is not negative, the argument is returned. 858 * If the argument is negative, the negation of the argument is returned. 859 * Special cases: 860 * <ul><li>If the argument is positive zero or negative zero, the 861 * result is positive zero. 862 * <li>If the argument is infinite, the result is positive infinity. 863 * <li>If the argument is NaN, the result is NaN.</ul> 864 * In other words, the result is the same as the value of the expression: 865 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))} 866 * 867 * @param a the argument whose absolute value is to be determined 868 * @return the absolute value of the argument. 869 */ 870 public static float abs(float a) { 871 return Math.abs(a); 872 } 873 874 /** 875 * Returns the absolute value of a {@code double} value. 876 * If the argument is not negative, the argument is returned. 877 * If the argument is negative, the negation of the argument is returned. 878 * Special cases: 879 * <ul><li>If the argument is positive zero or negative zero, the result 880 * is positive zero. 881 * <li>If the argument is infinite, the result is positive infinity. 882 * <li>If the argument is NaN, the result is NaN.</ul> 883 * In other words, the result is the same as the value of the expression: 884 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)} 885 * 886 * @param a the argument whose absolute value is to be determined 887 * @return the absolute value of the argument. 888 */ 889 public static double abs(double a) { 890 return Math.abs(a); 891 } 892 893 /** 894 * Returns the greater of two {@code int} values. That is, the 895 * result is the argument closer to the value of 896 * {@link Integer#MAX_VALUE}. If the arguments have the same value, 897 * the result is that same value. 898 * 899 * @param a an argument. 900 * @param b another argument. 901 * @return the larger of {@code a} and {@code b}. 902 */ 903 public static int max(int a, int b) { 904 return Math.max(a, b); 905 } 906 907 /** 908 * Returns the greater of two {@code long} values. That is, the 909 * result is the argument closer to the value of 910 * {@link Long#MAX_VALUE}. If the arguments have the same value, 911 * the result is that same value. 912 * 913 * @param a an argument. 914 * @param b another argument. 915 * @return the larger of {@code a} and {@code b}. 916 */ 917 public static long max(long a, long b) { 918 return Math.max(a, b); 919 } 920 921 /** 922 * Returns the greater of two {@code float} values. That is, 923 * the result is the argument closer to positive infinity. If the 924 * arguments have the same value, the result is that same 925 * value. If either value is NaN, then the result is NaN. Unlike 926 * the numerical comparison operators, this method considers 927 * negative zero to be strictly smaller than positive zero. If one 928 * argument is positive zero and the other negative zero, the 929 * result is positive zero. 930 * 931 * @param a an argument. 932 * @param b another argument. 933 * @return the larger of {@code a} and {@code b}. 934 */ 935 public static float max(float a, float b) { 936 return Math.max(a, b); 937 } 938 939 /** 940 * Returns the greater of two {@code double} values. That 941 * is, the result is the argument closer to positive infinity. If 942 * the arguments have the same value, the result is that same 943 * value. If either value is NaN, then the result is NaN. Unlike 944 * the numerical comparison operators, this method considers 945 * negative zero to be strictly smaller than positive zero. If one 946 * argument is positive zero and the other negative zero, the 947 * result is positive zero. 948 * 949 * @param a an argument. 950 * @param b another argument. 951 * @return the larger of {@code a} and {@code b}. 952 */ 953 public static double max(double a, double b) { 954 return Math.max(a, b); 955 } 956 957 /** 958 * Returns the smaller of two {@code int} values. That is, 959 * the result the argument closer to the value of 960 * {@link Integer#MIN_VALUE}. If the arguments have the same 961 * value, the result is that same value. 962 * 963 * @param a an argument. 964 * @param b another argument. 965 * @return the smaller of {@code a} and {@code b}. 966 */ 967 public static int min(int a, int b) { 968 return Math.min(a, b); 969 } 970 971 /** 972 * Returns the smaller of two {@code long} values. That is, 973 * the result is the argument closer to the value of 974 * {@link Long#MIN_VALUE}. If the arguments have the same 975 * value, the result is that same value. 976 * 977 * @param a an argument. 978 * @param b another argument. 979 * @return the smaller of {@code a} and {@code b}. 980 */ 981 public static long min(long a, long b) { 982 return Math.min(a, b); 983 } 984 985 /** 986 * Returns the smaller of two {@code float} values. That is, 987 * the result is the value closer to negative infinity. If the 988 * arguments have the same value, the result is that same 989 * value. If either value is NaN, then the result is NaN. Unlike 990 * the numerical comparison operators, this method considers 991 * negative zero to be strictly smaller than positive zero. If 992 * one argument is positive zero and the other is negative zero, 993 * the result is negative zero. 994 * 995 * @param a an argument. 996 * @param b another argument. 997 * @return the smaller of {@code a} and {@code b.} 998 */ 999 public static float min(float a, float b) { 1000 return Math.min(a, b); 1001 } 1002 1003 /** 1004 * Returns the smaller of two {@code double} values. That 1005 * is, the result is the value closer to negative infinity. If the 1006 * arguments have the same value, the result is that same 1007 * value. If either value is NaN, then the result is NaN. Unlike 1008 * the numerical comparison operators, this method considers 1009 * negative zero to be strictly smaller than positive zero. If one 1010 * argument is positive zero and the other is negative zero, the 1011 * result is negative zero. 1012 * 1013 * @param a an argument. 1014 * @param b another argument. 1015 * @return the smaller of {@code a} and {@code b}. 1016 */ 1017 public static double min(double a, double b) { 1018 return Math.min(a, b); 1019 } 1020 1021 /** 1022 * Returns the size of an ulp of the argument. An ulp, unit in 1023 * the last place, of a {@code double} value is the positive 1024 * distance between this floating-point value and the {@code 1025 * double} value next larger in magnitude. Note that for non-NaN 1026 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1027 * 1028 * <p>Special Cases: 1029 * <ul> 1030 * <li> If the argument is NaN, then the result is NaN. 1031 * <li> If the argument is positive or negative infinity, then the 1032 * result is positive infinity. 1033 * <li> If the argument is positive or negative zero, then the result is 1034 * {@code Double.MIN_VALUE}. 1035 * <li> If the argument is ±{@code Double.MAX_VALUE}, then 1036 * the result is equal to 2<sup>971</sup>. 1037 * </ul> 1038 * 1039 * @param d the floating-point value whose ulp is to be returned 1040 * @return the size of an ulp of the argument 1041 * @author Joseph D. Darcy 1042 * @since 1.5 1043 */ 1044 public static double ulp(double d) { 1045 return Math.ulp(d); 1046 } 1047 1048 /** 1049 * Returns the size of an ulp of the argument. An ulp, unit in 1050 * the last place, of a {@code float} value is the positive 1051 * distance between this floating-point value and the {@code 1052 * float} value next larger in magnitude. Note that for non-NaN 1053 * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 1054 * 1055 * <p>Special Cases: 1056 * <ul> 1057 * <li> If the argument is NaN, then the result is NaN. 1058 * <li> If the argument is positive or negative infinity, then the 1059 * result is positive infinity. 1060 * <li> If the argument is positive or negative zero, then the result is 1061 * {@code Float.MIN_VALUE}. 1062 * <li> If the argument is ±{@code Float.MAX_VALUE}, then 1063 * the result is equal to 2<sup>104</sup>. 1064 * </ul> 1065 * 1066 * @param f the floating-point value whose ulp is to be returned 1067 * @return the size of an ulp of the argument 1068 * @author Joseph D. Darcy 1069 * @since 1.5 1070 */ 1071 public static float ulp(float f) { 1072 return Math.ulp(f); 1073 } 1074 1075 /** 1076 * Returns the signum function of the argument; zero if the argument 1077 * is zero, 1.0 if the argument is greater than zero, -1.0 if the 1078 * argument is less than zero. 1079 * 1080 * <p>Special Cases: 1081 * <ul> 1082 * <li> If the argument is NaN, then the result is NaN. 1083 * <li> If the argument is positive zero or negative zero, then the 1084 * result is the same as the argument. 1085 * </ul> 1086 * 1087 * @param d the floating-point value whose signum is to be returned 1088 * @return the signum function of the argument 1089 * @author Joseph D. Darcy 1090 * @since 1.5 1091 */ 1092 public static double signum(double d) { 1093 return Math.signum(d); 1094 } 1095 1096 /** 1097 * Returns the signum function of the argument; zero if the argument 1098 * is zero, 1.0f if the argument is greater than zero, -1.0f if the 1099 * argument is less than zero. 1100 * 1101 * <p>Special Cases: 1102 * <ul> 1103 * <li> If the argument is NaN, then the result is NaN. 1104 * <li> If the argument is positive zero or negative zero, then the 1105 * result is the same as the argument. 1106 * </ul> 1107 * 1108 * @param f the floating-point value whose signum is to be returned 1109 * @return the signum function of the argument 1110 * @author Joseph D. Darcy 1111 * @since 1.5 1112 */ 1113 public static float signum(float f) { 1114 return Math.signum(f); 1115 } 1116 1117 /** 1118 * Returns the hyperbolic sine of a {@code double} value. 1119 * The hyperbolic sine of <i>x</i> is defined to be 1120 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2 1121 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1122 * 1123 * <p>Special cases: 1124 * <ul> 1125 * 1126 * <li>If the argument is NaN, then the result is NaN. 1127 * 1128 * <li>If the argument is infinite, then the result is an infinity 1129 * with the same sign as the argument. 1130 * 1131 * <li>If the argument is zero, then the result is a zero with the 1132 * same sign as the argument. 1133 * 1134 * </ul> 1135 * 1136 * @param x The number whose hyperbolic sine is to be returned. 1137 * @return The hyperbolic sine of {@code x}. 1138 * @since 1.5 1139 */ 1140 public static native double sinh(double x); 1141 1142 /** 1143 * Returns the hyperbolic cosine of a {@code double} value. 1144 * The hyperbolic cosine of <i>x</i> is defined to be 1145 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2 1146 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1147 * 1148 * <p>Special cases: 1149 * <ul> 1150 * 1151 * <li>If the argument is NaN, then the result is NaN. 1152 * 1153 * <li>If the argument is infinite, then the result is positive 1154 * infinity. 1155 * 1156 * <li>If the argument is zero, then the result is {@code 1.0}. 1157 * 1158 * </ul> 1159 * 1160 * @param x The number whose hyperbolic cosine is to be returned. 1161 * @return The hyperbolic cosine of {@code x}. 1162 * @since 1.5 1163 */ 1164 public static native double cosh(double x); 1165 1166 /** 1167 * Returns the hyperbolic tangent of a {@code double} value. 1168 * The hyperbolic tangent of <i>x</i> is defined to be 1169 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>), 1170 * in other words, {@linkplain Math#sinh 1171 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note 1172 * that the absolute value of the exact tanh is always less than 1173 * 1. 1174 * 1175 * <p>Special cases: 1176 * <ul> 1177 * 1178 * <li>If the argument is NaN, then the result is NaN. 1179 * 1180 * <li>If the argument is zero, then the result is a zero with the 1181 * same sign as the argument. 1182 * 1183 * <li>If the argument is positive infinity, then the result is 1184 * {@code +1.0}. 1185 * 1186 * <li>If the argument is negative infinity, then the result is 1187 * {@code -1.0}. 1188 * 1189 * </ul> 1190 * 1191 * @param x The number whose hyperbolic tangent is to be returned. 1192 * @return The hyperbolic tangent of {@code x}. 1193 * @since 1.5 1194 */ 1195 public static native double tanh(double x); 1196 1197 /** 1198 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1199 * without intermediate overflow or underflow. 1200 * 1201 * <p>Special cases: 1202 * <ul> 1203 * 1204 * <li> If either argument is infinite, then the result 1205 * is positive infinity. 1206 * 1207 * <li> If either argument is NaN and neither argument is infinite, 1208 * then the result is NaN. 1209 * 1210 * </ul> 1211 * 1212 * @param x a value 1213 * @param y a value 1214 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1215 * without intermediate overflow or underflow 1216 * @since 1.5 1217 */ 1218 public static native double hypot(double x, double y); 1219 1220 /** 1221 * Returns <i>e</i><sup>x</sup> -1. Note that for values of 1222 * <i>x</i> near 0, the exact sum of 1223 * {@code expm1(x)} + 1 is much closer to the true 1224 * result of <i>e</i><sup>x</sup> than {@code exp(x)}. 1225 * 1226 * <p>Special cases: 1227 * <ul> 1228 * <li>If the argument is NaN, the result is NaN. 1229 * 1230 * <li>If the argument is positive infinity, then the result is 1231 * positive infinity. 1232 * 1233 * <li>If the argument is negative infinity, then the result is 1234 * -1.0. 1235 * 1236 * <li>If the argument is zero, then the result is a zero with the 1237 * same sign as the argument. 1238 * 1239 * </ul> 1240 * 1241 * @param x the exponent to raise <i>e</i> to in the computation of 1242 * <i>e</i><sup>{@code x}</sup> -1. 1243 * @return the value <i>e</i><sup>{@code x}</sup> - 1. 1244 * @since 1.5 1245 */ 1246 public static native double expm1(double x); 1247 1248 /** 1249 * Returns the natural logarithm of the sum of the argument and 1. 1250 * Note that for small values {@code x}, the result of 1251 * {@code log1p(x)} is much closer to the true result of ln(1 1252 * + {@code x}) than the floating-point evaluation of 1253 * {@code log(1.0+x)}. 1254 * 1255 * <p>Special cases: 1256 * <ul> 1257 * 1258 * <li>If the argument is NaN or less than -1, then the result is 1259 * NaN. 1260 * 1261 * <li>If the argument is positive infinity, then the result is 1262 * positive infinity. 1263 * 1264 * <li>If the argument is negative one, then the result is 1265 * negative infinity. 1266 * 1267 * <li>If the argument is zero, then the result is a zero with the 1268 * same sign as the argument. 1269 * 1270 * </ul> 1271 * 1272 * @param x a value 1273 * @return the value ln({@code x} + 1), the natural 1274 * log of {@code x} + 1 1275 * @since 1.5 1276 */ 1277 public static native double log1p(double x); 1278 1279 /** 1280 * Returns the first floating-point argument with the sign of the 1281 * second floating-point argument. For this method, a NaN 1282 * {@code sign} argument is always treated as if it were 1283 * positive. 1284 * 1285 * @param magnitude the parameter providing the magnitude of the result 1286 * @param sign the parameter providing the sign of the result 1287 * @return a value with the magnitude of {@code magnitude} 1288 * and the sign of {@code sign}. 1289 * @since 1.6 1290 */ 1291 public static double copySign(double magnitude, double sign) { 1292 return Math.copySign(magnitude, (Double.isNaN(sign)?1.0d:sign)); 1293 } 1294 1295 /** 1296 * Returns the first floating-point argument with the sign of the 1297 * second floating-point argument. For this method, a NaN 1298 * {@code sign} argument is always treated as if it were 1299 * positive. 1300 * 1301 * @param magnitude the parameter providing the magnitude of the result 1302 * @param sign the parameter providing the sign of the result 1303 * @return a value with the magnitude of {@code magnitude} 1304 * and the sign of {@code sign}. 1305 * @since 1.6 1306 */ 1307 public static float copySign(float magnitude, float sign) { 1308 return Math.copySign(magnitude, (Float.isNaN(sign)?1.0f:sign)); 1309 } 1310 /** 1311 * Returns the unbiased exponent used in the representation of a 1312 * {@code float}. Special cases: 1313 * 1314 * <ul> 1315 * <li>If the argument is NaN or infinite, then the result is 1316 * {@link Float#MAX_EXPONENT} + 1. 1317 * <li>If the argument is zero or subnormal, then the result is 1318 * {@link Float#MIN_EXPONENT} -1. 1319 * </ul> 1320 * @param f a {@code float} value 1321 * @since 1.6 1322 */ 1323 public static int getExponent(float f) { 1324 return Math.getExponent(f); 1325 } 1326 1327 /** 1328 * Returns the unbiased exponent used in the representation of a 1329 * {@code double}. Special cases: 1330 * 1331 * <ul> 1332 * <li>If the argument is NaN or infinite, then the result is 1333 * {@link Double#MAX_EXPONENT} + 1. 1334 * <li>If the argument is zero or subnormal, then the result is 1335 * {@link Double#MIN_EXPONENT} -1. 1336 * </ul> 1337 * @param d a {@code double} value 1338 * @since 1.6 1339 */ 1340 public static int getExponent(double d) { 1341 return Math.getExponent(d); 1342 } 1343 1344 /** 1345 * Returns the floating-point number adjacent to the first 1346 * argument in the direction of the second argument. If both 1347 * arguments compare as equal the second argument is returned. 1348 * 1349 * <p>Special cases: 1350 * <ul> 1351 * <li> If either argument is a NaN, then NaN is returned. 1352 * 1353 * <li> If both arguments are signed zeros, {@code direction} 1354 * is returned unchanged (as implied by the requirement of 1355 * returning the second argument if the arguments compare as 1356 * equal). 1357 * 1358 * <li> If {@code start} is 1359 * ±{@link Double#MIN_VALUE} and {@code direction} 1360 * has a value such that the result should have a smaller 1361 * magnitude, then a zero with the same sign as {@code start} 1362 * is returned. 1363 * 1364 * <li> If {@code start} is infinite and 1365 * {@code direction} has a value such that the result should 1366 * have a smaller magnitude, {@link Double#MAX_VALUE} with the 1367 * same sign as {@code start} is returned. 1368 * 1369 * <li> If {@code start} is equal to ± 1370 * {@link Double#MAX_VALUE} and {@code direction} has a 1371 * value such that the result should have a larger magnitude, an 1372 * infinity with same sign as {@code start} is returned. 1373 * </ul> 1374 * 1375 * @param start starting floating-point value 1376 * @param direction value indicating which of 1377 * {@code start}'s neighbors or {@code start} should 1378 * be returned 1379 * @return The floating-point number adjacent to {@code start} in the 1380 * direction of {@code direction}. 1381 * @since 1.6 1382 */ 1383 public static double nextAfter(double start, double direction) { 1384 return Math.nextAfter(start, direction); 1385 } 1386 1387 /** 1388 * Returns the floating-point number adjacent to the first 1389 * argument in the direction of the second argument. If both 1390 * arguments compare as equal a value equivalent to the second argument 1391 * is returned. 1392 * 1393 * <p>Special cases: 1394 * <ul> 1395 * <li> If either argument is a NaN, then NaN is returned. 1396 * 1397 * <li> If both arguments are signed zeros, a value equivalent 1398 * to {@code direction} is returned. 1399 * 1400 * <li> If {@code start} is 1401 * ±{@link Float#MIN_VALUE} and {@code direction} 1402 * has a value such that the result should have a smaller 1403 * magnitude, then a zero with the same sign as {@code start} 1404 * is returned. 1405 * 1406 * <li> If {@code start} is infinite and 1407 * {@code direction} has a value such that the result should 1408 * have a smaller magnitude, {@link Float#MAX_VALUE} with the 1409 * same sign as {@code start} is returned. 1410 * 1411 * <li> If {@code start} is equal to ± 1412 * {@link Float#MAX_VALUE} and {@code direction} has a 1413 * value such that the result should have a larger magnitude, an 1414 * infinity with same sign as {@code start} is returned. 1415 * </ul> 1416 * 1417 * @param start starting floating-point value 1418 * @param direction value indicating which of 1419 * {@code start}'s neighbors or {@code start} should 1420 * be returned 1421 * @return The floating-point number adjacent to {@code start} in the 1422 * direction of {@code direction}. 1423 * @since 1.6 1424 */ 1425 public static float nextAfter(float start, double direction) { 1426 return Math.nextAfter(start, direction); 1427 } 1428 1429 /** 1430 * Returns the floating-point value adjacent to {@code d} in 1431 * the direction of positive infinity. This method is 1432 * semantically equivalent to {@code nextAfter(d, 1433 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp} 1434 * implementation may run faster than its equivalent 1435 * {@code nextAfter} call. 1436 * 1437 * <p>Special Cases: 1438 * <ul> 1439 * <li> If the argument is NaN, the result is NaN. 1440 * 1441 * <li> If the argument is positive infinity, the result is 1442 * positive infinity. 1443 * 1444 * <li> If the argument is zero, the result is 1445 * {@link Double#MIN_VALUE} 1446 * 1447 * </ul> 1448 * 1449 * @param d starting floating-point value 1450 * @return The adjacent floating-point value closer to positive 1451 * infinity. 1452 * @since 1.6 1453 */ 1454 public static double nextUp(double d) { 1455 return Math.nextUp(d); 1456 } 1457 1458 /** 1459 * Returns the floating-point value adjacent to {@code f} in 1460 * the direction of positive infinity. This method is 1461 * semantically equivalent to {@code nextAfter(f, 1462 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp} 1463 * implementation may run faster than its equivalent 1464 * {@code nextAfter} call. 1465 * 1466 * <p>Special Cases: 1467 * <ul> 1468 * <li> If the argument is NaN, the result is NaN. 1469 * 1470 * <li> If the argument is positive infinity, the result is 1471 * positive infinity. 1472 * 1473 * <li> If the argument is zero, the result is 1474 * {@link Float#MIN_VALUE} 1475 * 1476 * </ul> 1477 * 1478 * @param f starting floating-point value 1479 * @return The adjacent floating-point value closer to positive 1480 * infinity. 1481 * @since 1.6 1482 */ 1483 public static float nextUp(float f) { 1484 return Math.nextUp(f); 1485 } 1486 1487 /** 1488 * Returns the floating-point value adjacent to {@code d} in 1489 * the direction of negative infinity. This method is 1490 * semantically equivalent to {@code nextAfter(d, 1491 * Double.NEGATIVE_INFINITY)}; however, a 1492 * {@code nextDown} implementation may run faster than its 1493 * equivalent {@code nextAfter} call. 1494 * 1495 * <p>Special Cases: 1496 * <ul> 1497 * <li> If the argument is NaN, the result is NaN. 1498 * 1499 * <li> If the argument is negative infinity, the result is 1500 * negative infinity. 1501 * 1502 * <li> If the argument is zero, the result is 1503 * {@code -Double.MIN_VALUE} 1504 * 1505 * </ul> 1506 * 1507 * @param d starting floating-point value 1508 * @return The adjacent floating-point value closer to negative 1509 * infinity. 1510 * @since 1.8 1511 */ 1512 public static double nextDown(double d) { 1513 return Math.nextDown(d); 1514 } 1515 1516 /** 1517 * Returns the floating-point value adjacent to {@code f} in 1518 * the direction of negative infinity. This method is 1519 * semantically equivalent to {@code nextAfter(f, 1520 * Float.NEGATIVE_INFINITY)}; however, a 1521 * {@code nextDown} implementation may run faster than its 1522 * equivalent {@code nextAfter} call. 1523 * 1524 * <p>Special Cases: 1525 * <ul> 1526 * <li> If the argument is NaN, the result is NaN. 1527 * 1528 * <li> If the argument is negative infinity, the result is 1529 * negative infinity. 1530 * 1531 * <li> If the argument is zero, the result is 1532 * {@code -Float.MIN_VALUE} 1533 * 1534 * </ul> 1535 * 1536 * @param f starting floating-point value 1537 * @return The adjacent floating-point value closer to negative 1538 * infinity. 1539 * @since 1.8 1540 */ 1541 public static float nextDown(float f) { 1542 return Math.nextDown(f); 1543 } 1544 1545 /** 1546 * Return {@code d} × 1547 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1548 * by a single correctly rounded floating-point multiply to a 1549 * member of the double value set. See the Java 1550 * Language Specification for a discussion of floating-point 1551 * value sets. If the exponent of the result is between {@link 1552 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the 1553 * answer is calculated exactly. If the exponent of the result 1554 * would be larger than {@code Double.MAX_EXPONENT}, an 1555 * infinity is returned. Note that if the result is subnormal, 1556 * precision may be lost; that is, when {@code scalb(x, n)} 1557 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1558 * <i>x</i>. When the result is non-NaN, the result has the same 1559 * sign as {@code d}. 1560 * 1561 * <p>Special cases: 1562 * <ul> 1563 * <li> If the first argument is NaN, NaN is returned. 1564 * <li> If the first argument is infinite, then an infinity of the 1565 * same sign is returned. 1566 * <li> If the first argument is zero, then a zero of the same 1567 * sign is returned. 1568 * </ul> 1569 * 1570 * @param d number to be scaled by a power of two. 1571 * @param scaleFactor power of 2 used to scale {@code d} 1572 * @return {@code d} × 2<sup>{@code scaleFactor}</sup> 1573 * @since 1.6 1574 */ 1575 public static double scalb(double d, int scaleFactor) { 1576 return Math.scalb(d, scaleFactor); 1577 } 1578 1579 /** 1580 * Return {@code f} × 1581 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1582 * by a single correctly rounded floating-point multiply to a 1583 * member of the float value set. See the Java 1584 * Language Specification for a discussion of floating-point 1585 * value sets. If the exponent of the result is between {@link 1586 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the 1587 * answer is calculated exactly. If the exponent of the result 1588 * would be larger than {@code Float.MAX_EXPONENT}, an 1589 * infinity is returned. Note that if the result is subnormal, 1590 * precision may be lost; that is, when {@code scalb(x, n)} 1591 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1592 * <i>x</i>. When the result is non-NaN, the result has the same 1593 * sign as {@code f}. 1594 * 1595 * <p>Special cases: 1596 * <ul> 1597 * <li> If the first argument is NaN, NaN is returned. 1598 * <li> If the first argument is infinite, then an infinity of the 1599 * same sign is returned. 1600 * <li> If the first argument is zero, then a zero of the same 1601 * sign is returned. 1602 * </ul> 1603 * 1604 * @param f number to be scaled by a power of two. 1605 * @param scaleFactor power of 2 used to scale {@code f} 1606 * @return {@code f} × 2<sup>{@code scaleFactor}</sup> 1607 * @since 1.6 1608 */ 1609 public static float scalb(float f, int scaleFactor) { 1610 return Math.scalb(f, scaleFactor); 1611 } 1612 }