1 /* 2 * Copyright 1999-2006 Sun Microsystems, Inc. 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. Sun designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 22 * CA 95054 USA or visit www.sun.com if you need additional information or 23 * have any questions. 24 */ 25 26 package java.lang; 27 import java.util.Random; 28 import sun.misc.FpUtils; 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 * @author unascribed 61 * @author Joseph D. Darcy 62 * @since 1.3 63 */ 64 65 public final class StrictMath { 66 67 /** 68 * Don't let anyone instantiate this class. 69 */ 70 private StrictMath() {} 71 72 /** 73 * The {@code double} value that is closer than any other to 74 * <i>e</i>, the base of the natural logarithms. 75 */ 76 public static final double E = 2.7182818284590452354; 77 78 /** 79 * The {@code double} value that is closer than any other to 80 * <i>pi</i>, the ratio of the circumference of a circle to its 81 * diameter. 82 */ 83 public static final double PI = 3.14159265358979323846; 84 85 /** 86 * Returns the trigonometric sine of an angle. Special cases: 87 * <ul><li>If the argument is NaN or an infinity, then the 88 * result is NaN. 89 * <li>If the argument is zero, then the result is a zero with the 90 * same sign as the argument.</ul> 91 * 92 * @param a an angle, in radians. 93 * @return the sine of the argument. 94 */ 95 public static native double sin(double a); 96 97 /** 98 * Returns the trigonometric cosine of an angle. Special cases: 99 * <ul><li>If the argument is NaN or an infinity, then the 100 * result is NaN.</ul> 101 * 102 * @param a an angle, in radians. 103 * @return the cosine of the argument. 104 */ 105 public static native double cos(double a); 106 107 /** 108 * Returns the trigonometric tangent of an angle. Special cases: 109 * <ul><li>If the argument is NaN or an infinity, then the result 110 * is NaN. 111 * <li>If the argument is zero, then the result is a zero with the 112 * same sign as the argument.</ul> 113 * 114 * @param a an angle, in radians. 115 * @return the tangent of the argument. 116 */ 117 public static native double tan(double a); 118 119 /** 120 * Returns the arc sine of a value; the returned angle is in the 121 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases: 122 * <ul><li>If the argument is NaN or its absolute value is greater 123 * than 1, then the result is NaN. 124 * <li>If the argument is zero, then the result is a zero with the 125 * same sign as the argument.</ul> 126 * 127 * @param a the value whose arc sine is to be returned. 128 * @return the arc sine of the argument. 129 */ 130 public static native double asin(double a); 131 132 /** 133 * Returns the arc cosine of a value; the returned angle is in the 134 * range 0.0 through <i>pi</i>. Special case: 135 * <ul><li>If the argument is NaN or its absolute value is greater 136 * than 1, then the result is NaN.</ul> 137 * 138 * @param a the value whose arc cosine is to be returned. 139 * @return the arc cosine of the argument. 140 */ 141 public static native double acos(double a); 142 143 /** 144 * Returns the arc tangent of a value; the returned angle is in the 145 * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases: 146 * <ul><li>If the argument is NaN, then the result is NaN. 147 * <li>If the argument is zero, then the result is a zero with the 148 * same sign as the argument.</ul> 149 * 150 * @param a the value whose arc tangent is to be returned. 151 * @return the arc tangent of the argument. 152 */ 153 public static native double atan(double a); 154 155 /** 156 * Converts an angle measured in degrees to an approximately 157 * equivalent angle measured in radians. The conversion from 158 * degrees to radians is generally inexact. 159 * 160 * @param angdeg an angle, in degrees 161 * @return the measurement of the angle {@code angdeg} 162 * in radians. 163 */ 164 public static strictfp double toRadians(double angdeg) { 165 return angdeg / 180.0 * PI; 166 } 167 168 /** 169 * Converts an angle measured in radians to an approximately 170 * equivalent angle measured in degrees. The conversion from 171 * radians to degrees is generally inexact; users should 172 * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly 173 * equal {@code 0.0}. 174 * 175 * @param angrad an angle, in radians 176 * @return the measurement of the angle {@code angrad} 177 * in degrees. 178 */ 179 public static strictfp double toDegrees(double angrad) { 180 return angrad * 180.0 / PI; 181 } 182 183 /** 184 * Returns Euler's number <i>e</i> raised to the power of a 185 * {@code double} value. Special cases: 186 * <ul><li>If the argument is NaN, the result is NaN. 187 * <li>If the argument is positive infinity, then the result is 188 * positive infinity. 189 * <li>If the argument is negative infinity, then the result is 190 * positive zero.</ul> 191 * 192 * @param a the exponent to raise <i>e</i> to. 193 * @return the value <i>e</i><sup>{@code a}</sup>, 194 * where <i>e</i> is the base of the natural logarithms. 195 */ 196 public static native double exp(double a); 197 198 /** 199 * Returns the natural logarithm (base <i>e</i>) of a {@code double} 200 * value. Special cases: 201 * <ul><li>If the argument is NaN or less than zero, then the result 202 * is NaN. 203 * <li>If the argument is positive infinity, then the result is 204 * positive infinity. 205 * <li>If the argument is positive zero or negative zero, then the 206 * result is negative infinity.</ul> 207 * 208 * @param a a value 209 * @return the value ln {@code a}, the natural logarithm of 210 * {@code a}. 211 */ 212 public static native double log(double a); 213 214 215 /** 216 * Returns the base 10 logarithm of a {@code double} value. 217 * Special cases: 218 * 219 * <ul><li>If the argument is NaN or less than zero, then the result 220 * is NaN. 221 * <li>If the argument is positive infinity, then the result is 222 * positive infinity. 223 * <li>If the argument is positive zero or negative zero, then the 224 * result is negative infinity. 225 * <li> If the argument is equal to 10<sup><i>n</i></sup> for 226 * integer <i>n</i>, then the result is <i>n</i>. 227 * </ul> 228 * 229 * @param a a value 230 * @return the base 10 logarithm of {@code a}. 231 * @since 1.5 232 */ 233 public static native double log10(double a); 234 235 /** 236 * Returns the correctly rounded positive square root of a 237 * {@code double} value. 238 * Special cases: 239 * <ul><li>If the argument is NaN or less than zero, then the result 240 * is NaN. 241 * <li>If the argument is positive infinity, then the result is positive 242 * infinity. 243 * <li>If the argument is positive zero or negative zero, then the 244 * result is the same as the argument.</ul> 245 * Otherwise, the result is the {@code double} value closest to 246 * the true mathematical square root of the argument value. 247 * 248 * @param a a value. 249 * @return the positive square root of {@code a}. 250 */ 251 public static native double sqrt(double a); 252 253 /** 254 * Returns the cube root of a {@code double} value. For 255 * positive finite {@code x}, {@code cbrt(-x) == 256 * -cbrt(x)}; that is, the cube root of a negative value is 257 * the negative of the cube root of that value's magnitude. 258 * Special cases: 259 * 260 * <ul> 261 * 262 * <li>If the argument is NaN, then the result is NaN. 263 * 264 * <li>If the argument is infinite, then the result is an infinity 265 * with the same sign as the argument. 266 * 267 * <li>If the argument is zero, then the result is a zero with the 268 * same sign as the argument. 269 * 270 * </ul> 271 * 272 * @param a a value. 273 * @return the cube root of {@code a}. 274 * @since 1.5 275 */ 276 public static native double cbrt(double a); 277 278 /** 279 * Computes the remainder operation on two arguments as prescribed 280 * by the IEEE 754 standard. 281 * The remainder value is mathematically equal to 282 * <code>f1 - f2</code> × <i>n</i>, 283 * where <i>n</i> is the mathematical integer closest to the exact 284 * mathematical value of the quotient {@code f1/f2}, and if two 285 * mathematical integers are equally close to {@code f1/f2}, 286 * then <i>n</i> is the integer that is even. If the remainder is 287 * zero, its sign is the same as the sign of the first argument. 288 * Special cases: 289 * <ul><li>If either argument is NaN, or the first argument is infinite, 290 * or the second argument is positive zero or negative zero, then the 291 * result is NaN. 292 * <li>If the first argument is finite and the second argument is 293 * infinite, then the result is the same as the first argument.</ul> 294 * 295 * @param f1 the dividend. 296 * @param f2 the divisor. 297 * @return the remainder when {@code f1} is divided by 298 * {@code f2}. 299 */ 300 public static native double IEEEremainder(double f1, double f2); 301 302 /** 303 * Returns the smallest (closest to negative infinity) 304 * {@code double} value that is greater than or equal to the 305 * argument and is equal to a mathematical integer. Special cases: 306 * <ul><li>If the argument value is already equal to a 307 * mathematical integer, then the result is the same as the 308 * argument. <li>If the argument is NaN or an infinity or 309 * positive zero or negative zero, then the result is the same as 310 * the argument. <li>If the argument value is less than zero but 311 * greater than -1.0, then the result is negative zero.</ul> Note 312 * that the value of {@code StrictMath.ceil(x)} is exactly the 313 * value of {@code -StrictMath.floor(-x)}. 314 * 315 * @param a a value. 316 * @return the smallest (closest to negative infinity) 317 * floating-point value that is greater than or equal to 318 * the argument and is equal to a mathematical integer. 319 */ 320 public static double ceil(double a) { 321 return floorOrCeil(a, -0.0, 1.0, 1.0); 322 } 323 324 /** 325 * Returns the largest (closest to positive infinity) 326 * {@code double} value that is less than or equal to the 327 * argument and is equal to a mathematical integer. Special cases: 328 * <ul><li>If the argument value is already equal to a 329 * mathematical integer, then the result is the same as the 330 * argument. <li>If the argument is NaN or an infinity or 331 * positive zero or negative zero, then the result is the same as 332 * the argument.</ul> 333 * 334 * @param a a value. 335 * @return the largest (closest to positive infinity) 336 * floating-point value that less than or equal to the argument 337 * and is equal to a mathematical integer. 338 */ 339 public static double floor(double a) { 340 return floorOrCeil(a, -1.0, 0.0, -1.0); 341 } 342 343 /** 344 * Internal method to share logic between floor and ceil. 345 * 346 * @param a the value to be floored or ceiled 347 * @param negativeBoundary result for values in (-1, 0) 348 * @param positiveBoundary result for values in (0, 1) 349 * @param increment value to add when the argument is non-integral 350 */ 351 private static double floorOrCeil(double a, 352 double negativeBoundary, 353 double positiveBoundary, 354 double sign) { 355 int exponent = Math.getExponent(a); 356 357 if (exponent < 0) { 358 /* 359 * Absolute value of argument is less than 1. 360 * floorOrceil(-0.0) => -0.0 361 * floorOrceil(+0.0) => +0.0 362 */ 363 return ((a == 0.0) ? a : 364 ( (a < 0.0) ? negativeBoundary : positiveBoundary) ); 365 } else if (exponent >= 52) { 366 /* 367 * Infinity, NaN, or a value so large it must be integral. 368 */ 369 return a; 370 } 371 // Else the argument is either an integral value already XOR it 372 // has to be rounded to one. 373 assert exponent >= 0 && exponent <= 51; 374 375 long doppel = Double.doubleToRawLongBits(a); 376 long mask = DoubleConsts.SIGNIF_BIT_MASK >> exponent; 377 378 if ( (mask & doppel) == 0L ) 379 return a; // integral value 380 else { 381 double result = Double.longBitsToDouble(doppel & (~mask)); 382 if (sign*a > 0.0) 383 result = result + sign; 384 return result; 385 } 386 } 387 388 /** 389 * Returns the {@code double} value that is closest in value 390 * to the argument and is equal to a mathematical integer. If two 391 * {@code double} values that are mathematical integers are 392 * equally close to the value of the argument, the result is the 393 * integer value that is even. Special cases: 394 * <ul><li>If the argument value is already equal to a mathematical 395 * integer, then the result is the same as the argument. 396 * <li>If the argument is NaN or an infinity or positive zero or negative 397 * zero, then the result is the same as the argument.</ul> 398 * 399 * @param a a value. 400 * @return the closest floating-point value to {@code a} that is 401 * equal to a mathematical integer. 402 * @author Joseph D. Darcy 403 */ 404 public static double rint(double a) { 405 /* 406 * If the absolute value of a is not less than 2^52, it 407 * is either a finite integer (the double format does not have 408 * enough significand bits for a number that large to have any 409 * fractional portion), an infinity, or a NaN. In any of 410 * these cases, rint of the argument is the argument. 411 * 412 * Otherwise, the sum (twoToThe52 + a ) will properly round 413 * away any fractional portion of a since ulp(twoToThe52) == 414 * 1.0; subtracting out twoToThe52 from this sum will then be 415 * exact and leave the rounded integer portion of a. 416 * 417 * This method does *not* need to be declared strictfp to get 418 * fully reproducible results. Whether or not a method is 419 * declared strictfp can only make a difference in the 420 * returned result if some operation would overflow or 421 * underflow with strictfp semantics. The operation 422 * (twoToThe52 + a ) cannot overflow since large values of a 423 * are screened out; the add cannot underflow since twoToThe52 424 * is too large. The subtraction ((twoToThe52 + a ) - 425 * twoToThe52) will be exact as discussed above and thus 426 * cannot overflow or meaningfully underflow. Finally, the 427 * last multiply in the return statement is by plus or minus 428 * 1.0, which is exact too. 429 */ 430 double twoToThe52 = (double)(1L << 52); // 2^52 431 double sign = FpUtils.rawCopySign(1.0, a); // preserve sign info 432 a = Math.abs(a); 433 434 if (a < twoToThe52) { // E_min <= ilogb(a) <= 51 435 a = ((twoToThe52 + a ) - twoToThe52); 436 } 437 438 return sign * a; // restore original sign 439 } 440 441 /** 442 * Returns the angle <i>theta</i> from the conversion of rectangular 443 * coordinates ({@code x}, {@code y}) to polar 444 * coordinates (r, <i>theta</i>). 445 * This method computes the phase <i>theta</i> by computing an arc tangent 446 * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special 447 * cases: 448 * <ul><li>If either argument is NaN, then the result is NaN. 449 * <li>If the first argument is positive zero and the second argument 450 * is positive, or the first argument is positive and finite and the 451 * second argument is positive infinity, then the result is positive 452 * zero. 453 * <li>If the first argument is negative zero and the second argument 454 * is positive, or the first argument is negative and finite and the 455 * second argument is positive infinity, then the result is negative zero. 456 * <li>If the first argument is positive zero and the second argument 457 * is negative, or the first argument is positive and finite and the 458 * second argument is negative infinity, then the result is the 459 * {@code double} value closest to <i>pi</i>. 460 * <li>If the first argument is negative zero and the second argument 461 * is negative, or the first argument is negative and finite and the 462 * second argument is negative infinity, then the result is the 463 * {@code double} value closest to -<i>pi</i>. 464 * <li>If the first argument is positive and the second argument is 465 * positive zero or negative zero, or the first argument is positive 466 * infinity and the second argument is finite, then the result is the 467 * {@code double} value closest to <i>pi</i>/2. 468 * <li>If the first argument is negative and the second argument is 469 * positive zero or negative zero, or the first argument is negative 470 * infinity and the second argument is finite, then the result is the 471 * {@code double} value closest to -<i>pi</i>/2. 472 * <li>If both arguments are positive infinity, then the result is the 473 * {@code double} value closest to <i>pi</i>/4. 474 * <li>If the first argument is positive infinity and the second argument 475 * is negative infinity, then the result is the {@code double} 476 * value closest to 3*<i>pi</i>/4. 477 * <li>If the first argument is negative infinity and the second argument 478 * is positive infinity, then the result is the {@code double} value 479 * closest to -<i>pi</i>/4. 480 * <li>If both arguments are negative infinity, then the result is the 481 * {@code double} value closest to -3*<i>pi</i>/4.</ul> 482 * 483 * @param y the ordinate coordinate 484 * @param x the abscissa coordinate 485 * @return the <i>theta</i> component of the point 486 * (<i>r</i>, <i>theta</i>) 487 * in polar coordinates that corresponds to the point 488 * (<i>x</i>, <i>y</i>) in Cartesian coordinates. 489 */ 490 public static native double atan2(double y, double x); 491 492 493 /** 494 * Returns the value of the first argument raised to the power of the 495 * second argument. Special cases: 496 * 497 * <ul><li>If the second argument is positive or negative zero, then the 498 * result is 1.0. 499 * <li>If the second argument is 1.0, then the result is the same as the 500 * first argument. 501 * <li>If the second argument is NaN, then the result is NaN. 502 * <li>If the first argument is NaN and the second argument is nonzero, 503 * then the result is NaN. 504 * 505 * <li>If 506 * <ul> 507 * <li>the absolute value of the first argument is greater than 1 508 * and the second argument is positive infinity, or 509 * <li>the absolute value of the first argument is less than 1 and 510 * the second argument is negative infinity, 511 * </ul> 512 * then the result is positive infinity. 513 * 514 * <li>If 515 * <ul> 516 * <li>the absolute value of the first argument is greater than 1 and 517 * the second argument is negative infinity, or 518 * <li>the absolute value of the 519 * first argument is less than 1 and the second argument is positive 520 * infinity, 521 * </ul> 522 * then the result is positive zero. 523 * 524 * <li>If the absolute value of the first argument equals 1 and the 525 * second argument is infinite, then the result is NaN. 526 * 527 * <li>If 528 * <ul> 529 * <li>the first argument is positive zero and the second argument 530 * is greater than zero, or 531 * <li>the first argument is positive infinity and the second 532 * argument is less than zero, 533 * </ul> 534 * then the result is positive zero. 535 * 536 * <li>If 537 * <ul> 538 * <li>the first argument is positive zero and the second argument 539 * is less than zero, or 540 * <li>the first argument is positive infinity and the second 541 * argument is greater than zero, 542 * </ul> 543 * then the result is positive infinity. 544 * 545 * <li>If 546 * <ul> 547 * <li>the first argument is negative zero and the second argument 548 * is greater than zero but not a finite odd integer, or 549 * <li>the first argument is negative infinity and the second 550 * argument is less than zero but not a finite odd integer, 551 * </ul> 552 * then the result is positive zero. 553 * 554 * <li>If 555 * <ul> 556 * <li>the first argument is negative zero and the second argument 557 * is a positive finite odd integer, or 558 * <li>the first argument is negative infinity and the second 559 * argument is a negative finite odd integer, 560 * </ul> 561 * then the result is negative zero. 562 * 563 * <li>If 564 * <ul> 565 * <li>the first argument is negative zero and the second argument 566 * is less than zero but not a finite odd integer, or 567 * <li>the first argument is negative infinity and the second 568 * argument is greater than zero but not a finite odd integer, 569 * </ul> 570 * then the result is positive infinity. 571 * 572 * <li>If 573 * <ul> 574 * <li>the first argument is negative zero and the second argument 575 * is a negative finite odd integer, or 576 * <li>the first argument is negative infinity and the second 577 * argument is a positive finite odd integer, 578 * </ul> 579 * then the result is negative infinity. 580 * 581 * <li>If the first argument is finite and less than zero 582 * <ul> 583 * <li> if the second argument is a finite even integer, the 584 * result is equal to the result of raising the absolute value of 585 * the first argument to the power of the second argument 586 * 587 * <li>if the second argument is a finite odd integer, the result 588 * is equal to the negative of the result of raising the absolute 589 * value of the first argument to the power of the second 590 * argument 591 * 592 * <li>if the second argument is finite and not an integer, then 593 * the result is NaN. 594 * </ul> 595 * 596 * <li>If both arguments are integers, then the result is exactly equal 597 * to the mathematical result of raising the first argument to the power 598 * of the second argument if that result can in fact be represented 599 * exactly as a {@code double} value.</ul> 600 * 601 * <p>(In the foregoing descriptions, a floating-point value is 602 * considered to be an integer if and only if it is finite and a 603 * fixed point of the method {@link #ceil ceil} or, 604 * equivalently, a fixed point of the method {@link #floor 605 * floor}. A value is a fixed point of a one-argument 606 * method if and only if the result of applying the method to the 607 * value is equal to the value.) 608 * 609 * @param a base. 610 * @param b the exponent. 611 * @return the value {@code a}<sup>{@code b}</sup>. 612 */ 613 public static native double pow(double a, double b); 614 615 /** 616 * Returns the closest {@code int} to the argument. The 617 * result is rounded to an integer by adding 1/2, taking the 618 * floor of the result, and casting the result to type {@code int}. 619 * In other words, the result is equal to the value of the expression: 620 * <p>{@code (int)Math.floor(a + 0.5f)} 621 * 622 * <p>Special cases: 623 * <ul><li>If the argument is NaN, the result is 0. 624 * <li>If the argument is negative infinity or any value less than or 625 * equal to the value of {@code Integer.MIN_VALUE}, the result is 626 * equal to the value of {@code Integer.MIN_VALUE}. 627 * <li>If the argument is positive infinity or any value greater than or 628 * equal to the value of {@code Integer.MAX_VALUE}, the result is 629 * equal to the value of {@code Integer.MAX_VALUE}.</ul> 630 * 631 * @param a a floating-point value to be rounded to an integer. 632 * @return the value of the argument rounded to the nearest 633 * {@code int} value. 634 * @see java.lang.Integer#MAX_VALUE 635 * @see java.lang.Integer#MIN_VALUE 636 */ 637 public static int round(float a) { 638 return (int)floor(a + 0.5f); 639 } 640 641 /** 642 * Returns the closest {@code long} to the argument. The result 643 * is rounded to an integer by adding 1/2, taking the floor of the 644 * result, and casting the result to type {@code long}. In other 645 * words, the result is equal to the value of the expression: 646 * <p>{@code (long)Math.floor(a + 0.5d)} 647 * 648 * <p>Special cases: 649 * <ul><li>If the argument is NaN, the result is 0. 650 * <li>If the argument is negative infinity or any value less than or 651 * equal to the value of {@code Long.MIN_VALUE}, the result is 652 * equal to the value of {@code Long.MIN_VALUE}. 653 * <li>If the argument is positive infinity or any value greater than or 654 * equal to the value of {@code Long.MAX_VALUE}, the result is 655 * equal to the value of {@code Long.MAX_VALUE}.</ul> 656 * 657 * @param a a floating-point value to be rounded to a 658 * {@code long}. 659 * @return the value of the argument rounded to the nearest 660 * {@code long} value. 661 * @see java.lang.Long#MAX_VALUE 662 * @see java.lang.Long#MIN_VALUE 663 */ 664 public static long round(double a) { 665 return (long)floor(a + 0.5d); 666 } 667 668 private static Random randomNumberGenerator; 669 670 private static synchronized void initRNG() { 671 if (randomNumberGenerator == null) 672 randomNumberGenerator = new Random(); 673 } 674 675 /** 676 * Returns a {@code double} value with a positive sign, greater 677 * than or equal to {@code 0.0} and less than {@code 1.0}. 678 * Returned values are chosen pseudorandomly with (approximately) 679 * uniform distribution from that range. 680 * 681 * <p>When this method is first called, it creates a single new 682 * pseudorandom-number generator, exactly as if by the expression 683 * <blockquote>{@code new java.util.Random}</blockquote> This 684 * new pseudorandom-number generator is used thereafter for all 685 * calls to this method and is used nowhere else. 686 * 687 * <p>This method is properly synchronized to allow correct use by 688 * more than one thread. However, if many threads need to generate 689 * pseudorandom numbers at a great rate, it may reduce contention 690 * for each thread to have its own pseudorandom number generator. 691 * 692 * @return a pseudorandom {@code double} greater than or equal 693 * to {@code 0.0} and less than {@code 1.0}. 694 * @see java.util.Random#nextDouble() 695 */ 696 public static double random() { 697 if (randomNumberGenerator == null) initRNG(); 698 return randomNumberGenerator.nextDouble(); 699 } 700 701 /** 702 * Returns the absolute value of an {@code int} value.. 703 * If the argument is not negative, the argument is returned. 704 * If the argument is negative, the negation of the argument is returned. 705 * 706 * <p>Note that if the argument is equal to the value of 707 * {@link Integer#MIN_VALUE}, the most negative representable 708 * {@code int} value, the result is that same value, which is 709 * negative. 710 * 711 * @param a the argument whose absolute value is to be determined. 712 * @return the absolute value of the argument. 713 */ 714 public static int abs(int a) { 715 return (a < 0) ? -a : a; 716 } 717 718 /** 719 * Returns the absolute value of a {@code long} value. 720 * If the argument is not negative, the argument is returned. 721 * If the argument is negative, the negation of the argument is returned. 722 * 723 * <p>Note that if the argument is equal to the value of 724 * {@link Long#MIN_VALUE}, the most negative representable 725 * {@code long} value, the result is that same value, which 726 * is negative. 727 * 728 * @param a the argument whose absolute value is to be determined. 729 * @return the absolute value of the argument. 730 */ 731 public static long abs(long a) { 732 return (a < 0) ? -a : a; 733 } 734 735 /** 736 * Returns the absolute value of a {@code float} value. 737 * If the argument is not negative, the argument is returned. 738 * If the argument is negative, the negation of the argument is returned. 739 * Special cases: 740 * <ul><li>If the argument is positive zero or negative zero, the 741 * result is positive zero. 742 * <li>If the argument is infinite, the result is positive infinity. 743 * <li>If the argument is NaN, the result is NaN.</ul> 744 * In other words, the result is the same as the value of the expression: 745 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))} 746 * 747 * @param a the argument whose absolute value is to be determined 748 * @return the absolute value of the argument. 749 */ 750 public static float abs(float a) { 751 return (a <= 0.0F) ? 0.0F - a : a; 752 } 753 754 /** 755 * Returns the absolute value of a {@code double} value. 756 * If the argument is not negative, the argument is returned. 757 * If the argument is negative, the negation of the argument is returned. 758 * Special cases: 759 * <ul><li>If the argument is positive zero or negative zero, the result 760 * is positive zero. 761 * <li>If the argument is infinite, the result is positive infinity. 762 * <li>If the argument is NaN, the result is NaN.</ul> 763 * In other words, the result is the same as the value of the expression: 764 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)} 765 * 766 * @param a the argument whose absolute value is to be determined 767 * @return the absolute value of the argument. 768 */ 769 public static double abs(double a) { 770 return (a <= 0.0D) ? 0.0D - a : a; 771 } 772 773 /** 774 * Returns the greater of two {@code int} values. That is, the 775 * result is the argument closer to the value of 776 * {@link Integer#MAX_VALUE}. If the arguments have the same value, 777 * the result is that same value. 778 * 779 * @param a an argument. 780 * @param b another argument. 781 * @return the larger of {@code a} and {@code b}. 782 */ 783 public static int max(int a, int b) { 784 return (a >= b) ? a : b; 785 } 786 787 /** 788 * Returns the greater of two {@code long} values. That is, the 789 * result is the argument closer to the value of 790 * {@link Long#MAX_VALUE}. If the arguments have the same value, 791 * the result is that same value. 792 * 793 * @param a an argument. 794 * @param b another argument. 795 * @return the larger of {@code a} and {@code b}. 796 */ 797 public static long max(long a, long b) { 798 return (a >= b) ? a : b; 799 } 800 801 private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f); 802 private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d); 803 804 /** 805 * Returns the greater of two {@code float} values. That is, 806 * the result is the argument closer to positive infinity. If the 807 * arguments have the same value, the result is that same 808 * value. If either value is NaN, then the result is NaN. Unlike 809 * the numerical comparison operators, this method considers 810 * negative zero to be strictly smaller than positive zero. If one 811 * argument is positive zero and the other negative zero, the 812 * result is positive zero. 813 * 814 * @param a an argument. 815 * @param b another argument. 816 * @return the larger of {@code a} and {@code b}. 817 */ 818 public static float max(float a, float b) { 819 if (a != a) return a; // a is NaN 820 if ((a == 0.0f) && (b == 0.0f) 821 && (Float.floatToIntBits(a) == negativeZeroFloatBits)) { 822 return b; 823 } 824 return (a >= b) ? a : b; 825 } 826 827 /** 828 * Returns the greater of two {@code double} values. That 829 * is, the result is the argument closer to positive infinity. If 830 * the arguments have the same value, the result is that same 831 * value. If either value is NaN, then the result is NaN. Unlike 832 * the numerical comparison operators, this method considers 833 * negative zero to be strictly smaller than positive zero. If one 834 * argument is positive zero and the other negative zero, the 835 * result is positive zero. 836 * 837 * @param a an argument. 838 * @param b another argument. 839 * @return the larger of {@code a} and {@code b}. 840 */ 841 public static double max(double a, double b) { 842 if (a != a) return a; // a is NaN 843 if ((a == 0.0d) && (b == 0.0d) 844 && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) { 845 return b; 846 } 847 return (a >= b) ? a : b; 848 } 849 850 /** 851 * Returns the smaller of two {@code int} values. That is, 852 * the result the argument closer to the value of 853 * {@link Integer#MIN_VALUE}. If the arguments have the same 854 * value, the result is that same value. 855 * 856 * @param a an argument. 857 * @param b another argument. 858 * @return the smaller of {@code a} and {@code b}. 859 */ 860 public static int min(int a, int b) { 861 return (a <= b) ? a : b; 862 } 863 864 /** 865 * Returns the smaller of two {@code long} values. That is, 866 * the result is the argument closer to the value of 867 * {@link Long#MIN_VALUE}. If the arguments have the same 868 * value, the result is that same value. 869 * 870 * @param a an argument. 871 * @param b another argument. 872 * @return the smaller of {@code a} and {@code b}. 873 */ 874 public static long min(long a, long b) { 875 return (a <= b) ? a : b; 876 } 877 878 /** 879 * Returns the smaller of two {@code float} values. That is, 880 * the result is the value closer to negative infinity. If the 881 * arguments have the same value, the result is that same 882 * value. If either value is NaN, then the result is NaN. Unlike 883 * the numerical comparison operators, this method considers 884 * negative zero to be strictly smaller than positive zero. If 885 * one argument is positive zero and the other is negative zero, 886 * the result is negative zero. 887 * 888 * @param a an argument. 889 * @param b another argument. 890 * @return the smaller of {@code a} and {@code b.} 891 */ 892 public static float min(float a, float b) { 893 if (a != a) return a; // a is NaN 894 if ((a == 0.0f) && (b == 0.0f) 895 && (Float.floatToIntBits(b) == negativeZeroFloatBits)) { 896 return b; 897 } 898 return (a <= b) ? a : b; 899 } 900 901 /** 902 * Returns the smaller of two {@code double} values. That 903 * is, the result is the value closer to negative infinity. If the 904 * arguments have the same value, the result is that same 905 * value. If either value is NaN, then the result is NaN. Unlike 906 * the numerical comparison operators, this method considers 907 * negative zero to be strictly smaller than positive zero. If one 908 * argument is positive zero and the other is negative zero, the 909 * result is negative zero. 910 * 911 * @param a an argument. 912 * @param b another argument. 913 * @return the smaller of {@code a} and {@code b}. 914 */ 915 public static double min(double a, double b) { 916 if (a != a) return a; // a is NaN 917 if ((a == 0.0d) && (b == 0.0d) 918 && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) { 919 return b; 920 } 921 return (a <= b) ? a : b; 922 } 923 924 /** 925 * Returns the size of an ulp of the argument. An ulp of a 926 * {@code double} value is the positive distance between this 927 * floating-point value and the {@code double} value next 928 * larger in magnitude. Note that for non-NaN <i>x</i>, 929 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 930 * 931 * <p>Special Cases: 932 * <ul> 933 * <li> If the argument is NaN, then the result is NaN. 934 * <li> If the argument is positive or negative infinity, then the 935 * result is positive infinity. 936 * <li> If the argument is positive or negative zero, then the result is 937 * {@code Double.MIN_VALUE}. 938 * <li> If the argument is ±{@code Double.MAX_VALUE}, then 939 * the result is equal to 2<sup>971</sup>. 940 * </ul> 941 * 942 * @param d the floating-point value whose ulp is to be returned 943 * @return the size of an ulp of the argument 944 * @author Joseph D. Darcy 945 * @since 1.5 946 */ 947 public static double ulp(double d) { 948 return sun.misc.FpUtils.ulp(d); 949 } 950 951 /** 952 * Returns the size of an ulp of the argument. An ulp of a 953 * {@code float} value is the positive distance between this 954 * floating-point value and the {@code float} value next 955 * larger in magnitude. Note that for non-NaN <i>x</i>, 956 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 957 * 958 * <p>Special Cases: 959 * <ul> 960 * <li> If the argument is NaN, then the result is NaN. 961 * <li> If the argument is positive or negative infinity, then the 962 * result is positive infinity. 963 * <li> If the argument is positive or negative zero, then the result is 964 * {@code Float.MIN_VALUE}. 965 * <li> If the argument is ±{@code Float.MAX_VALUE}, then 966 * the result is equal to 2<sup>104</sup>. 967 * </ul> 968 * 969 * @param f the floating-point value whose ulp is to be returned 970 * @return the size of an ulp of the argument 971 * @author Joseph D. Darcy 972 * @since 1.5 973 */ 974 public static float ulp(float f) { 975 return sun.misc.FpUtils.ulp(f); 976 } 977 978 /** 979 * Returns the signum function of the argument; zero if the argument 980 * is zero, 1.0 if the argument is greater than zero, -1.0 if the 981 * argument is less than zero. 982 * 983 * <p>Special Cases: 984 * <ul> 985 * <li> If the argument is NaN, then the result is NaN. 986 * <li> If the argument is positive zero or negative zero, then the 987 * result is the same as the argument. 988 * </ul> 989 * 990 * @param d the floating-point value whose signum is to be returned 991 * @return the signum function of the argument 992 * @author Joseph D. Darcy 993 * @since 1.5 994 */ 995 public static double signum(double d) { 996 return sun.misc.FpUtils.signum(d); 997 } 998 999 /** 1000 * Returns the signum function of the argument; zero if the argument 1001 * is zero, 1.0f if the argument is greater than zero, -1.0f if the 1002 * argument is less than zero. 1003 * 1004 * <p>Special Cases: 1005 * <ul> 1006 * <li> If the argument is NaN, then the result is NaN. 1007 * <li> If the argument is positive zero or negative zero, then the 1008 * result is the same as the argument. 1009 * </ul> 1010 * 1011 * @param f the floating-point value whose signum is to be returned 1012 * @return the signum function of the argument 1013 * @author Joseph D. Darcy 1014 * @since 1.5 1015 */ 1016 public static float signum(float f) { 1017 return sun.misc.FpUtils.signum(f); 1018 } 1019 1020 /** 1021 * Returns the hyperbolic sine of a {@code double} value. 1022 * The hyperbolic sine of <i>x</i> is defined to be 1023 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2 1024 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1025 * 1026 * <p>Special cases: 1027 * <ul> 1028 * 1029 * <li>If the argument is NaN, then the result is NaN. 1030 * 1031 * <li>If the argument is infinite, then the result is an infinity 1032 * with the same sign as the argument. 1033 * 1034 * <li>If the argument is zero, then the result is a zero with the 1035 * same sign as the argument. 1036 * 1037 * </ul> 1038 * 1039 * @param x The number whose hyperbolic sine is to be returned. 1040 * @return The hyperbolic sine of {@code x}. 1041 * @since 1.5 1042 */ 1043 public static native double sinh(double x); 1044 1045 /** 1046 * Returns the hyperbolic cosine of a {@code double} value. 1047 * The hyperbolic cosine of <i>x</i> is defined to be 1048 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2 1049 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1050 * 1051 * <p>Special cases: 1052 * <ul> 1053 * 1054 * <li>If the argument is NaN, then the result is NaN. 1055 * 1056 * <li>If the argument is infinite, then the result is positive 1057 * infinity. 1058 * 1059 * <li>If the argument is zero, then the result is {@code 1.0}. 1060 * 1061 * </ul> 1062 * 1063 * @param x The number whose hyperbolic cosine is to be returned. 1064 * @return The hyperbolic cosine of {@code x}. 1065 * @since 1.5 1066 */ 1067 public static native double cosh(double x); 1068 1069 /** 1070 * Returns the hyperbolic tangent of a {@code double} value. 1071 * The hyperbolic tangent of <i>x</i> is defined to be 1072 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>), 1073 * in other words, {@linkplain Math#sinh 1074 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note 1075 * that the absolute value of the exact tanh is always less than 1076 * 1. 1077 * 1078 * <p>Special cases: 1079 * <ul> 1080 * 1081 * <li>If the argument is NaN, then the result is NaN. 1082 * 1083 * <li>If the argument is zero, then the result is a zero with the 1084 * same sign as the argument. 1085 * 1086 * <li>If the argument is positive infinity, then the result is 1087 * {@code +1.0}. 1088 * 1089 * <li>If the argument is negative infinity, then the result is 1090 * {@code -1.0}. 1091 * 1092 * </ul> 1093 * 1094 * @param x The number whose hyperbolic tangent is to be returned. 1095 * @return The hyperbolic tangent of {@code x}. 1096 * @since 1.5 1097 */ 1098 public static native double tanh(double x); 1099 1100 /** 1101 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1102 * without intermediate overflow or underflow. 1103 * 1104 * <p>Special cases: 1105 * <ul> 1106 * 1107 * <li> If either argument is infinite, then the result 1108 * is positive infinity. 1109 * 1110 * <li> If either argument is NaN and neither argument is infinite, 1111 * then the result is NaN. 1112 * 1113 * </ul> 1114 * 1115 * @param x a value 1116 * @param y a value 1117 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1118 * without intermediate overflow or underflow 1119 * @since 1.5 1120 */ 1121 public static native double hypot(double x, double y); 1122 1123 /** 1124 * Returns <i>e</i><sup>x</sup> -1. Note that for values of 1125 * <i>x</i> near 0, the exact sum of 1126 * {@code expm1(x)} + 1 is much closer to the true 1127 * result of <i>e</i><sup>x</sup> than {@code exp(x)}. 1128 * 1129 * <p>Special cases: 1130 * <ul> 1131 * <li>If the argument is NaN, the result is NaN. 1132 * 1133 * <li>If the argument is positive infinity, then the result is 1134 * positive infinity. 1135 * 1136 * <li>If the argument is negative infinity, then the result is 1137 * -1.0. 1138 * 1139 * <li>If the argument is zero, then the result is a zero with the 1140 * same sign as the argument. 1141 * 1142 * </ul> 1143 * 1144 * @param x the exponent to raise <i>e</i> to in the computation of 1145 * <i>e</i><sup>{@code x}</sup> -1. 1146 * @return the value <i>e</i><sup>{@code x}</sup> - 1. 1147 * @since 1.5 1148 */ 1149 public static native double expm1(double x); 1150 1151 /** 1152 * Returns the natural logarithm of the sum of the argument and 1. 1153 * Note that for small values {@code x}, the result of 1154 * {@code log1p(x)} is much closer to the true result of ln(1 1155 * + {@code x}) than the floating-point evaluation of 1156 * {@code log(1.0+x)}. 1157 * 1158 * <p>Special cases: 1159 * <ul> 1160 * 1161 * <li>If the argument is NaN or less than -1, then the result is 1162 * NaN. 1163 * 1164 * <li>If the argument is positive infinity, then the result is 1165 * positive infinity. 1166 * 1167 * <li>If the argument is negative one, then the result is 1168 * negative infinity. 1169 * 1170 * <li>If the argument is zero, then the result is a zero with the 1171 * same sign as the argument. 1172 * 1173 * </ul> 1174 * 1175 * @param x a value 1176 * @return the value ln({@code x} + 1), the natural 1177 * log of {@code x} + 1 1178 * @since 1.5 1179 */ 1180 public static native double log1p(double x); 1181 1182 /** 1183 * Returns the first floating-point argument with the sign of the 1184 * second floating-point argument. For this method, a NaN 1185 * {@code sign} argument is always treated as if it were 1186 * positive. 1187 * 1188 * @param magnitude the parameter providing the magnitude of the result 1189 * @param sign the parameter providing the sign of the result 1190 * @return a value with the magnitude of {@code magnitude} 1191 * and the sign of {@code sign}. 1192 * @since 1.6 1193 */ 1194 public static double copySign(double magnitude, double sign) { 1195 return sun.misc.FpUtils.copySign(magnitude, sign); 1196 } 1197 1198 /** 1199 * Returns the first floating-point argument with the sign of the 1200 * second floating-point argument. For this method, a NaN 1201 * {@code sign} argument is always treated as if it were 1202 * positive. 1203 * 1204 * @param magnitude the parameter providing the magnitude of the result 1205 * @param sign the parameter providing the sign of the result 1206 * @return a value with the magnitude of {@code magnitude} 1207 * and the sign of {@code sign}. 1208 * @since 1.6 1209 */ 1210 public static float copySign(float magnitude, float sign) { 1211 return sun.misc.FpUtils.copySign(magnitude, sign); 1212 } 1213 /** 1214 * Returns the unbiased exponent used in the representation of a 1215 * {@code float}. Special cases: 1216 * 1217 * <ul> 1218 * <li>If the argument is NaN or infinite, then the result is 1219 * {@link Float#MAX_EXPONENT} + 1. 1220 * <li>If the argument is zero or subnormal, then the result is 1221 * {@link Float#MIN_EXPONENT} -1. 1222 * </ul> 1223 * @param f a {@code float} value 1224 * @since 1.6 1225 */ 1226 public static int getExponent(float f) { 1227 return sun.misc.FpUtils.getExponent(f); 1228 } 1229 1230 /** 1231 * Returns the unbiased exponent used in the representation of a 1232 * {@code double}. Special cases: 1233 * 1234 * <ul> 1235 * <li>If the argument is NaN or infinite, then the result is 1236 * {@link Double#MAX_EXPONENT} + 1. 1237 * <li>If the argument is zero or subnormal, then the result is 1238 * {@link Double#MIN_EXPONENT} -1. 1239 * </ul> 1240 * @param d a {@code double} value 1241 * @since 1.6 1242 */ 1243 public static int getExponent(double d) { 1244 return sun.misc.FpUtils.getExponent(d); 1245 } 1246 1247 /** 1248 * Returns the floating-point number adjacent to the first 1249 * argument in the direction of the second argument. If both 1250 * arguments compare as equal the second argument is returned. 1251 * 1252 * <p>Special cases: 1253 * <ul> 1254 * <li> If either argument is a NaN, then NaN is returned. 1255 * 1256 * <li> If both arguments are signed zeros, {@code direction} 1257 * is returned unchanged (as implied by the requirement of 1258 * returning the second argument if the arguments compare as 1259 * equal). 1260 * 1261 * <li> If {@code start} is 1262 * ±{@link Double#MIN_VALUE} and {@code direction} 1263 * has a value such that the result should have a smaller 1264 * magnitude, then a zero with the same sign as {@code start} 1265 * is returned. 1266 * 1267 * <li> If {@code start} is infinite and 1268 * {@code direction} has a value such that the result should 1269 * have a smaller magnitude, {@link Double#MAX_VALUE} with the 1270 * same sign as {@code start} is returned. 1271 * 1272 * <li> If {@code start} is equal to ± 1273 * {@link Double#MAX_VALUE} and {@code direction} has a 1274 * value such that the result should have a larger magnitude, an 1275 * infinity with same sign as {@code start} is returned. 1276 * </ul> 1277 * 1278 * @param start starting floating-point value 1279 * @param direction value indicating which of 1280 * {@code start}'s neighbors or {@code start} should 1281 * be returned 1282 * @return The floating-point number adjacent to {@code start} in the 1283 * direction of {@code direction}. 1284 * @since 1.6 1285 */ 1286 public static double nextAfter(double start, double direction) { 1287 return sun.misc.FpUtils.nextAfter(start, direction); 1288 } 1289 1290 /** 1291 * Returns the floating-point number adjacent to the first 1292 * argument in the direction of the second argument. If both 1293 * arguments compare as equal a value equivalent to the second argument 1294 * is returned. 1295 * 1296 * <p>Special cases: 1297 * <ul> 1298 * <li> If either argument is a NaN, then NaN is returned. 1299 * 1300 * <li> If both arguments are signed zeros, a value equivalent 1301 * to {@code direction} is returned. 1302 * 1303 * <li> If {@code start} is 1304 * ±{@link Float#MIN_VALUE} and {@code direction} 1305 * has a value such that the result should have a smaller 1306 * magnitude, then a zero with the same sign as {@code start} 1307 * is returned. 1308 * 1309 * <li> If {@code start} is infinite and 1310 * {@code direction} has a value such that the result should 1311 * have a smaller magnitude, {@link Float#MAX_VALUE} with the 1312 * same sign as {@code start} is returned. 1313 * 1314 * <li> If {@code start} is equal to ± 1315 * {@link Float#MAX_VALUE} and {@code direction} has a 1316 * value such that the result should have a larger magnitude, an 1317 * infinity with same sign as {@code start} is returned. 1318 * </ul> 1319 * 1320 * @param start starting floating-point value 1321 * @param direction value indicating which of 1322 * {@code start}'s neighbors or {@code start} should 1323 * be returned 1324 * @return The floating-point number adjacent to {@code start} in the 1325 * direction of {@code direction}. 1326 * @since 1.6 1327 */ 1328 public static float nextAfter(float start, double direction) { 1329 return sun.misc.FpUtils.nextAfter(start, direction); 1330 } 1331 1332 /** 1333 * Returns the floating-point value adjacent to {@code d} in 1334 * the direction of positive infinity. This method is 1335 * semantically equivalent to {@code nextAfter(d, 1336 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp} 1337 * implementation may run faster than its equivalent 1338 * {@code nextAfter} call. 1339 * 1340 * <p>Special Cases: 1341 * <ul> 1342 * <li> If the argument is NaN, the result is NaN. 1343 * 1344 * <li> If the argument is positive infinity, the result is 1345 * positive infinity. 1346 * 1347 * <li> If the argument is zero, the result is 1348 * {@link Double#MIN_VALUE} 1349 * 1350 * </ul> 1351 * 1352 * @param d starting floating-point value 1353 * @return The adjacent floating-point value closer to positive 1354 * infinity. 1355 * @since 1.6 1356 */ 1357 public static double nextUp(double d) { 1358 return sun.misc.FpUtils.nextUp(d); 1359 } 1360 1361 /** 1362 * Returns the floating-point value adjacent to {@code f} in 1363 * the direction of positive infinity. This method is 1364 * semantically equivalent to {@code nextAfter(f, 1365 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp} 1366 * implementation may run faster than its equivalent 1367 * {@code nextAfter} call. 1368 * 1369 * <p>Special Cases: 1370 * <ul> 1371 * <li> If the argument is NaN, the result is NaN. 1372 * 1373 * <li> If the argument is positive infinity, the result is 1374 * positive infinity. 1375 * 1376 * <li> If the argument is zero, the result is 1377 * {@link Float#MIN_VALUE} 1378 * 1379 * </ul> 1380 * 1381 * @param f starting floating-point value 1382 * @return The adjacent floating-point value closer to positive 1383 * infinity. 1384 * @since 1.6 1385 */ 1386 public static float nextUp(float f) { 1387 return sun.misc.FpUtils.nextUp(f); 1388 } 1389 1390 1391 /** 1392 * Return {@code d} × 1393 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1394 * by a single correctly rounded floating-point multiply to a 1395 * member of the double value set. See the Java 1396 * Language Specification for a discussion of floating-point 1397 * value sets. If the exponent of the result is between {@link 1398 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the 1399 * answer is calculated exactly. If the exponent of the result 1400 * would be larger than {@code Double.MAX_EXPONENT}, an 1401 * infinity is returned. Note that if the result is subnormal, 1402 * precision may be lost; that is, when {@code scalb(x, n)} 1403 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1404 * <i>x</i>. When the result is non-NaN, the result has the same 1405 * sign as {@code d}. 1406 * 1407 * <p>Special cases: 1408 * <ul> 1409 * <li> If the first argument is NaN, NaN is returned. 1410 * <li> If the first argument is infinite, then an infinity of the 1411 * same sign is returned. 1412 * <li> If the first argument is zero, then a zero of the same 1413 * sign is returned. 1414 * </ul> 1415 * 1416 * @param d number to be scaled by a power of two. 1417 * @param scaleFactor power of 2 used to scale {@code d} 1418 * @return {@code d} × 2<sup>{@code scaleFactor}</sup> 1419 * @since 1.6 1420 */ 1421 public static double scalb(double d, int scaleFactor) { 1422 return sun.misc.FpUtils.scalb(d, scaleFactor); 1423 } 1424 1425 /** 1426 * Return {@code f} × 1427 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1428 * by a single correctly rounded floating-point multiply to a 1429 * member of the float value set. See the Java 1430 * Language Specification for a discussion of floating-point 1431 * value sets. If the exponent of the result is between {@link 1432 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the 1433 * answer is calculated exactly. If the exponent of the result 1434 * would be larger than {@code Float.MAX_EXPONENT}, an 1435 * infinity is returned. Note that if the result is subnormal, 1436 * precision may be lost; that is, when {@code scalb(x, n)} 1437 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1438 * <i>x</i>. When the result is non-NaN, the result has the same 1439 * sign as {@code f}. 1440 * 1441 * <p>Special cases: 1442 * <ul> 1443 * <li> If the first argument is NaN, NaN is returned. 1444 * <li> If the first argument is infinite, then an infinity of the 1445 * same sign is returned. 1446 * <li> If the first argument is zero, then a zero of the same 1447 * sign is returned. 1448 * </ul> 1449 * 1450 * @param f number to be scaled by a power of two. 1451 * @param scaleFactor power of 2 used to scale {@code f} 1452 * @return {@code f} × 2<sup>{@code scaleFactor}</sup> 1453 * @since 1.6 1454 */ 1455 public static float scalb(float f, int scaleFactor) { 1456 return sun.misc.FpUtils.scalb(f, scaleFactor); 1457 } 1458 }