1 /* 2 * Copyright (c) 1999, 2010, 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.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 Random initRNG() { 671 Random rnd = randomNumberGenerator; 672 return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd; 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 * 684 * <blockquote>{@code new java.util.Random()}</blockquote> 685 * 686 * This new pseudorandom-number generator is used thereafter for 687 * all calls to this method and is used nowhere else. 688 * 689 * <p>This method is properly synchronized to allow correct use by 690 * more than one thread. However, if many threads need to generate 691 * pseudorandom numbers at a great rate, it may reduce contention 692 * for each thread to have its own pseudorandom number generator. 693 * 694 * @return a pseudorandom {@code double} greater than or equal 695 * to {@code 0.0} and less than {@code 1.0}. 696 * @see Random#nextDouble() 697 */ 698 public static double random() { 699 Random rnd = randomNumberGenerator; 700 if (rnd == null) rnd = initRNG(); 701 return rnd.nextDouble(); 702 } 703 704 /** 705 * Returns the absolute value of an {@code int} value.. 706 * If the argument is not negative, the argument is returned. 707 * If the argument is negative, the negation of the argument is returned. 708 * 709 * <p>Note that if the argument is equal to the value of 710 * {@link Integer#MIN_VALUE}, the most negative representable 711 * {@code int} value, the result is that same value, which is 712 * negative. 713 * 714 * @param a the argument whose absolute value is to be determined. 715 * @return the absolute value of the argument. 716 */ 717 public static int abs(int a) { 718 return (a < 0) ? -a : a; 719 } 720 721 /** 722 * Returns the absolute value of a {@code long} value. 723 * If the argument is not negative, the argument is returned. 724 * If the argument is negative, the negation of the argument is returned. 725 * 726 * <p>Note that if the argument is equal to the value of 727 * {@link Long#MIN_VALUE}, the most negative representable 728 * {@code long} value, the result is that same value, which 729 * is negative. 730 * 731 * @param a the argument whose absolute value is to be determined. 732 * @return the absolute value of the argument. 733 */ 734 public static long abs(long a) { 735 return (a < 0) ? -a : a; 736 } 737 738 /** 739 * Returns the absolute value of a {@code float} value. 740 * If the argument is not negative, the argument is returned. 741 * If the argument is negative, the negation of the argument is returned. 742 * Special cases: 743 * <ul><li>If the argument is positive zero or negative zero, the 744 * result is positive zero. 745 * <li>If the argument is infinite, the result is positive infinity. 746 * <li>If the argument is NaN, the result is NaN.</ul> 747 * In other words, the result is the same as the value of the expression: 748 * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))} 749 * 750 * @param a the argument whose absolute value is to be determined 751 * @return the absolute value of the argument. 752 */ 753 public static float abs(float a) { 754 return (a <= 0.0F) ? 0.0F - a : a; 755 } 756 757 /** 758 * Returns the absolute value of a {@code double} value. 759 * If the argument is not negative, the argument is returned. 760 * If the argument is negative, the negation of the argument is returned. 761 * Special cases: 762 * <ul><li>If the argument is positive zero or negative zero, the result 763 * is positive zero. 764 * <li>If the argument is infinite, the result is positive infinity. 765 * <li>If the argument is NaN, the result is NaN.</ul> 766 * In other words, the result is the same as the value of the expression: 767 * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)} 768 * 769 * @param a the argument whose absolute value is to be determined 770 * @return the absolute value of the argument. 771 */ 772 public static double abs(double a) { 773 return (a <= 0.0D) ? 0.0D - a : a; 774 } 775 776 /** 777 * Returns the greater of two {@code int} values. That is, the 778 * result is the argument closer to the value of 779 * {@link Integer#MAX_VALUE}. If the arguments have the same value, 780 * the result is that same value. 781 * 782 * @param a an argument. 783 * @param b another argument. 784 * @return the larger of {@code a} and {@code b}. 785 */ 786 public static int max(int a, int b) { 787 return (a >= b) ? a : b; 788 } 789 790 /** 791 * Returns the greater of two {@code long} values. That is, the 792 * result is the argument closer to the value of 793 * {@link Long#MAX_VALUE}. If the arguments have the same value, 794 * the result is that same value. 795 * 796 * @param a an argument. 797 * @param b another argument. 798 * @return the larger of {@code a} and {@code b}. 799 */ 800 public static long max(long a, long b) { 801 return (a >= b) ? a : b; 802 } 803 804 // Use raw bit-wise conversions on guaranteed non-NaN arguments. 805 private static long negativeZeroFloatBits = Float.floatToRawIntBits(-0.0f); 806 private static long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d); 807 808 /** 809 * Returns the greater of two {@code float} values. That is, 810 * the result is the argument closer to positive infinity. If the 811 * arguments have the same value, the result is that same 812 * value. If either value is NaN, then the result is NaN. Unlike 813 * the numerical comparison operators, this method considers 814 * negative zero to be strictly smaller than positive zero. If one 815 * argument is positive zero and the other negative zero, the 816 * result is positive zero. 817 * 818 * @param a an argument. 819 * @param b another argument. 820 * @return the larger of {@code a} and {@code b}. 821 */ 822 public static float max(float a, float b) { 823 if (a != a) 824 return a; // a is NaN 825 if ((a == 0.0f) && 826 (b == 0.0f) && 827 (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) { 828 // Raw conversion ok since NaN can't map to -0.0. 829 return b; 830 } 831 return (a >= b) ? a : b; 832 } 833 834 /** 835 * Returns the greater of two {@code double} values. That 836 * is, the result is the argument closer to positive infinity. If 837 * the arguments have the same value, the result is that same 838 * value. If either value is NaN, then the result is NaN. Unlike 839 * the numerical comparison operators, this method considers 840 * negative zero to be strictly smaller than positive zero. If one 841 * argument is positive zero and the other negative zero, the 842 * result is positive zero. 843 * 844 * @param a an argument. 845 * @param b another argument. 846 * @return the larger of {@code a} and {@code b}. 847 */ 848 public static double max(double a, double b) { 849 if (a != a) 850 return a; // a is NaN 851 if ((a == 0.0d) && 852 (b == 0.0d) && 853 (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) { 854 // Raw conversion ok since NaN can't map to -0.0. 855 return b; 856 } 857 return (a >= b) ? a : b; 858 } 859 860 /** 861 * Returns the smaller of two {@code int} values. That is, 862 * the result the argument closer to the value of 863 * {@link Integer#MIN_VALUE}. If the arguments have the same 864 * value, the result is that same value. 865 * 866 * @param a an argument. 867 * @param b another argument. 868 * @return the smaller of {@code a} and {@code b}. 869 */ 870 public static int min(int a, int b) { 871 return (a <= b) ? a : b; 872 } 873 874 /** 875 * Returns the smaller of two {@code long} values. That is, 876 * the result is the argument closer to the value of 877 * {@link Long#MIN_VALUE}. If the arguments have the same 878 * value, the result is that same value. 879 * 880 * @param a an argument. 881 * @param b another argument. 882 * @return the smaller of {@code a} and {@code b}. 883 */ 884 public static long min(long a, long b) { 885 return (a <= b) ? a : b; 886 } 887 888 /** 889 * Returns the smaller of two {@code float} values. That is, 890 * the result is the value closer to negative infinity. If the 891 * arguments have the same value, the result is that same 892 * value. If either value is NaN, then the result is NaN. Unlike 893 * the numerical comparison operators, this method considers 894 * negative zero to be strictly smaller than positive zero. If 895 * one argument is positive zero and the other is negative zero, 896 * the result is negative zero. 897 * 898 * @param a an argument. 899 * @param b another argument. 900 * @return the smaller of {@code a} and {@code b.} 901 */ 902 public static float min(float a, float b) { 903 if (a != a) 904 return a; // a is NaN 905 if ((a == 0.0f) && 906 (b == 0.0f) && 907 (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) { 908 // Raw conversion ok since NaN can't map to -0.0. 909 return b; 910 } 911 return (a <= b) ? a : b; 912 } 913 914 /** 915 * Returns the smaller of two {@code double} values. That 916 * is, the result is the value closer to negative infinity. If the 917 * arguments have the same value, the result is that same 918 * value. If either value is NaN, then the result is NaN. Unlike 919 * the numerical comparison operators, this method considers 920 * negative zero to be strictly smaller than positive zero. If one 921 * argument is positive zero and the other is negative zero, the 922 * result is negative zero. 923 * 924 * @param a an argument. 925 * @param b another argument. 926 * @return the smaller of {@code a} and {@code b}. 927 */ 928 public static double min(double a, double b) { 929 if (a != a) 930 return a; // a is NaN 931 if ((a == 0.0d) && 932 (b == 0.0d) && 933 (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) { 934 // Raw conversion ok since NaN can't map to -0.0. 935 return b; 936 } 937 return (a <= b) ? a : b; 938 } 939 940 /** 941 * Returns the size of an ulp of the argument. An ulp of a 942 * {@code double} value is the positive distance between this 943 * floating-point value and the {@code double} value next 944 * larger in magnitude. Note that for non-NaN <i>x</i>, 945 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 946 * 947 * <p>Special Cases: 948 * <ul> 949 * <li> If the argument is NaN, then the result is NaN. 950 * <li> If the argument is positive or negative infinity, then the 951 * result is positive infinity. 952 * <li> If the argument is positive or negative zero, then the result is 953 * {@code Double.MIN_VALUE}. 954 * <li> If the argument is ±{@code Double.MAX_VALUE}, then 955 * the result is equal to 2<sup>971</sup>. 956 * </ul> 957 * 958 * @param d the floating-point value whose ulp is to be returned 959 * @return the size of an ulp of the argument 960 * @author Joseph D. Darcy 961 * @since 1.5 962 */ 963 public static double ulp(double d) { 964 return sun.misc.FpUtils.ulp(d); 965 } 966 967 /** 968 * Returns the size of an ulp of the argument. An ulp of a 969 * {@code float} value is the positive distance between this 970 * floating-point value and the {@code float} value next 971 * larger in magnitude. Note that for non-NaN <i>x</i>, 972 * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>. 973 * 974 * <p>Special Cases: 975 * <ul> 976 * <li> If the argument is NaN, then the result is NaN. 977 * <li> If the argument is positive or negative infinity, then the 978 * result is positive infinity. 979 * <li> If the argument is positive or negative zero, then the result is 980 * {@code Float.MIN_VALUE}. 981 * <li> If the argument is ±{@code Float.MAX_VALUE}, then 982 * the result is equal to 2<sup>104</sup>. 983 * </ul> 984 * 985 * @param f the floating-point value whose ulp is to be returned 986 * @return the size of an ulp of the argument 987 * @author Joseph D. Darcy 988 * @since 1.5 989 */ 990 public static float ulp(float f) { 991 return sun.misc.FpUtils.ulp(f); 992 } 993 994 /** 995 * Returns the signum function of the argument; zero if the argument 996 * is zero, 1.0 if the argument is greater than zero, -1.0 if the 997 * argument is less than zero. 998 * 999 * <p>Special Cases: 1000 * <ul> 1001 * <li> If the argument is NaN, then the result is NaN. 1002 * <li> If the argument is positive zero or negative zero, then the 1003 * result is the same as the argument. 1004 * </ul> 1005 * 1006 * @param d the floating-point value whose signum is to be returned 1007 * @return the signum function of the argument 1008 * @author Joseph D. Darcy 1009 * @since 1.5 1010 */ 1011 public static double signum(double d) { 1012 return sun.misc.FpUtils.signum(d); 1013 } 1014 1015 /** 1016 * Returns the signum function of the argument; zero if the argument 1017 * is zero, 1.0f if the argument is greater than zero, -1.0f if the 1018 * argument is less than zero. 1019 * 1020 * <p>Special Cases: 1021 * <ul> 1022 * <li> If the argument is NaN, then the result is NaN. 1023 * <li> If the argument is positive zero or negative zero, then the 1024 * result is the same as the argument. 1025 * </ul> 1026 * 1027 * @param f the floating-point value whose signum is to be returned 1028 * @return the signum function of the argument 1029 * @author Joseph D. Darcy 1030 * @since 1.5 1031 */ 1032 public static float signum(float f) { 1033 return sun.misc.FpUtils.signum(f); 1034 } 1035 1036 /** 1037 * Returns the hyperbolic sine of a {@code double} value. 1038 * The hyperbolic sine of <i>x</i> is defined to be 1039 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2 1040 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1041 * 1042 * <p>Special cases: 1043 * <ul> 1044 * 1045 * <li>If the argument is NaN, then the result is NaN. 1046 * 1047 * <li>If the argument is infinite, then the result is an infinity 1048 * with the same sign as the argument. 1049 * 1050 * <li>If the argument is zero, then the result is a zero with the 1051 * same sign as the argument. 1052 * 1053 * </ul> 1054 * 1055 * @param x The number whose hyperbolic sine is to be returned. 1056 * @return The hyperbolic sine of {@code x}. 1057 * @since 1.5 1058 */ 1059 public static native double sinh(double x); 1060 1061 /** 1062 * Returns the hyperbolic cosine of a {@code double} value. 1063 * The hyperbolic cosine of <i>x</i> is defined to be 1064 * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2 1065 * where <i>e</i> is {@linkplain Math#E Euler's number}. 1066 * 1067 * <p>Special cases: 1068 * <ul> 1069 * 1070 * <li>If the argument is NaN, then the result is NaN. 1071 * 1072 * <li>If the argument is infinite, then the result is positive 1073 * infinity. 1074 * 1075 * <li>If the argument is zero, then the result is {@code 1.0}. 1076 * 1077 * </ul> 1078 * 1079 * @param x The number whose hyperbolic cosine is to be returned. 1080 * @return The hyperbolic cosine of {@code x}. 1081 * @since 1.5 1082 */ 1083 public static native double cosh(double x); 1084 1085 /** 1086 * Returns the hyperbolic tangent of a {@code double} value. 1087 * The hyperbolic tangent of <i>x</i> is defined to be 1088 * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>), 1089 * in other words, {@linkplain Math#sinh 1090 * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note 1091 * that the absolute value of the exact tanh is always less than 1092 * 1. 1093 * 1094 * <p>Special cases: 1095 * <ul> 1096 * 1097 * <li>If the argument is NaN, then the result is NaN. 1098 * 1099 * <li>If the argument is zero, then the result is a zero with the 1100 * same sign as the argument. 1101 * 1102 * <li>If the argument is positive infinity, then the result is 1103 * {@code +1.0}. 1104 * 1105 * <li>If the argument is negative infinity, then the result is 1106 * {@code -1.0}. 1107 * 1108 * </ul> 1109 * 1110 * @param x The number whose hyperbolic tangent is to be returned. 1111 * @return The hyperbolic tangent of {@code x}. 1112 * @since 1.5 1113 */ 1114 public static native double tanh(double x); 1115 1116 /** 1117 * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1118 * without intermediate overflow or underflow. 1119 * 1120 * <p>Special cases: 1121 * <ul> 1122 * 1123 * <li> If either argument is infinite, then the result 1124 * is positive infinity. 1125 * 1126 * <li> If either argument is NaN and neither argument is infinite, 1127 * then the result is NaN. 1128 * 1129 * </ul> 1130 * 1131 * @param x a value 1132 * @param y a value 1133 * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>) 1134 * without intermediate overflow or underflow 1135 * @since 1.5 1136 */ 1137 public static native double hypot(double x, double y); 1138 1139 /** 1140 * Returns <i>e</i><sup>x</sup> -1. Note that for values of 1141 * <i>x</i> near 0, the exact sum of 1142 * {@code expm1(x)} + 1 is much closer to the true 1143 * result of <i>e</i><sup>x</sup> than {@code exp(x)}. 1144 * 1145 * <p>Special cases: 1146 * <ul> 1147 * <li>If the argument is NaN, the result is NaN. 1148 * 1149 * <li>If the argument is positive infinity, then the result is 1150 * positive infinity. 1151 * 1152 * <li>If the argument is negative infinity, then the result is 1153 * -1.0. 1154 * 1155 * <li>If the argument is zero, then the result is a zero with the 1156 * same sign as the argument. 1157 * 1158 * </ul> 1159 * 1160 * @param x the exponent to raise <i>e</i> to in the computation of 1161 * <i>e</i><sup>{@code x}</sup> -1. 1162 * @return the value <i>e</i><sup>{@code x}</sup> - 1. 1163 * @since 1.5 1164 */ 1165 public static native double expm1(double x); 1166 1167 /** 1168 * Returns the natural logarithm of the sum of the argument and 1. 1169 * Note that for small values {@code x}, the result of 1170 * {@code log1p(x)} is much closer to the true result of ln(1 1171 * + {@code x}) than the floating-point evaluation of 1172 * {@code log(1.0+x)}. 1173 * 1174 * <p>Special cases: 1175 * <ul> 1176 * 1177 * <li>If the argument is NaN or less than -1, then the result is 1178 * NaN. 1179 * 1180 * <li>If the argument is positive infinity, then the result is 1181 * positive infinity. 1182 * 1183 * <li>If the argument is negative one, then the result is 1184 * negative infinity. 1185 * 1186 * <li>If the argument is zero, then the result is a zero with the 1187 * same sign as the argument. 1188 * 1189 * </ul> 1190 * 1191 * @param x a value 1192 * @return the value ln({@code x} + 1), the natural 1193 * log of {@code x} + 1 1194 * @since 1.5 1195 */ 1196 public static native double log1p(double x); 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 double copySign(double magnitude, double sign) { 1211 return sun.misc.FpUtils.copySign(magnitude, sign); 1212 } 1213 1214 /** 1215 * Returns the first floating-point argument with the sign of the 1216 * second floating-point argument. For this method, a NaN 1217 * {@code sign} argument is always treated as if it were 1218 * positive. 1219 * 1220 * @param magnitude the parameter providing the magnitude of the result 1221 * @param sign the parameter providing the sign of the result 1222 * @return a value with the magnitude of {@code magnitude} 1223 * and the sign of {@code sign}. 1224 * @since 1.6 1225 */ 1226 public static float copySign(float magnitude, float sign) { 1227 return sun.misc.FpUtils.copySign(magnitude, sign); 1228 } 1229 /** 1230 * Returns the unbiased exponent used in the representation of a 1231 * {@code float}. Special cases: 1232 * 1233 * <ul> 1234 * <li>If the argument is NaN or infinite, then the result is 1235 * {@link Float#MAX_EXPONENT} + 1. 1236 * <li>If the argument is zero or subnormal, then the result is 1237 * {@link Float#MIN_EXPONENT} -1. 1238 * </ul> 1239 * @param f a {@code float} value 1240 * @since 1.6 1241 */ 1242 public static int getExponent(float f) { 1243 return sun.misc.FpUtils.getExponent(f); 1244 } 1245 1246 /** 1247 * Returns the unbiased exponent used in the representation of a 1248 * {@code double}. Special cases: 1249 * 1250 * <ul> 1251 * <li>If the argument is NaN or infinite, then the result is 1252 * {@link Double#MAX_EXPONENT} + 1. 1253 * <li>If the argument is zero or subnormal, then the result is 1254 * {@link Double#MIN_EXPONENT} -1. 1255 * </ul> 1256 * @param d a {@code double} value 1257 * @since 1.6 1258 */ 1259 public static int getExponent(double d) { 1260 return sun.misc.FpUtils.getExponent(d); 1261 } 1262 1263 /** 1264 * Returns the floating-point number adjacent to the first 1265 * argument in the direction of the second argument. If both 1266 * arguments compare as equal the second argument is returned. 1267 * 1268 * <p>Special cases: 1269 * <ul> 1270 * <li> If either argument is a NaN, then NaN is returned. 1271 * 1272 * <li> If both arguments are signed zeros, {@code direction} 1273 * is returned unchanged (as implied by the requirement of 1274 * returning the second argument if the arguments compare as 1275 * equal). 1276 * 1277 * <li> If {@code start} is 1278 * ±{@link Double#MIN_VALUE} and {@code direction} 1279 * has a value such that the result should have a smaller 1280 * magnitude, then a zero with the same sign as {@code start} 1281 * is returned. 1282 * 1283 * <li> If {@code start} is infinite and 1284 * {@code direction} has a value such that the result should 1285 * have a smaller magnitude, {@link Double#MAX_VALUE} with the 1286 * same sign as {@code start} is returned. 1287 * 1288 * <li> If {@code start} is equal to ± 1289 * {@link Double#MAX_VALUE} and {@code direction} has a 1290 * value such that the result should have a larger magnitude, an 1291 * infinity with same sign as {@code start} is returned. 1292 * </ul> 1293 * 1294 * @param start starting floating-point value 1295 * @param direction value indicating which of 1296 * {@code start}'s neighbors or {@code start} should 1297 * be returned 1298 * @return The floating-point number adjacent to {@code start} in the 1299 * direction of {@code direction}. 1300 * @since 1.6 1301 */ 1302 public static double nextAfter(double start, double direction) { 1303 return sun.misc.FpUtils.nextAfter(start, direction); 1304 } 1305 1306 /** 1307 * Returns the floating-point number adjacent to the first 1308 * argument in the direction of the second argument. If both 1309 * arguments compare as equal a value equivalent to the second argument 1310 * is returned. 1311 * 1312 * <p>Special cases: 1313 * <ul> 1314 * <li> If either argument is a NaN, then NaN is returned. 1315 * 1316 * <li> If both arguments are signed zeros, a value equivalent 1317 * to {@code direction} is returned. 1318 * 1319 * <li> If {@code start} is 1320 * ±{@link Float#MIN_VALUE} and {@code direction} 1321 * has a value such that the result should have a smaller 1322 * magnitude, then a zero with the same sign as {@code start} 1323 * is returned. 1324 * 1325 * <li> If {@code start} is infinite and 1326 * {@code direction} has a value such that the result should 1327 * have a smaller magnitude, {@link Float#MAX_VALUE} with the 1328 * same sign as {@code start} is returned. 1329 * 1330 * <li> If {@code start} is equal to ± 1331 * {@link Float#MAX_VALUE} and {@code direction} has a 1332 * value such that the result should have a larger magnitude, an 1333 * infinity with same sign as {@code start} is returned. 1334 * </ul> 1335 * 1336 * @param start starting floating-point value 1337 * @param direction value indicating which of 1338 * {@code start}'s neighbors or {@code start} should 1339 * be returned 1340 * @return The floating-point number adjacent to {@code start} in the 1341 * direction of {@code direction}. 1342 * @since 1.6 1343 */ 1344 public static float nextAfter(float start, double direction) { 1345 return sun.misc.FpUtils.nextAfter(start, direction); 1346 } 1347 1348 /** 1349 * Returns the floating-point value adjacent to {@code d} in 1350 * the direction of positive infinity. This method is 1351 * semantically equivalent to {@code nextAfter(d, 1352 * Double.POSITIVE_INFINITY)}; however, a {@code nextUp} 1353 * implementation may run faster than its equivalent 1354 * {@code nextAfter} call. 1355 * 1356 * <p>Special Cases: 1357 * <ul> 1358 * <li> If the argument is NaN, the result is NaN. 1359 * 1360 * <li> If the argument is positive infinity, the result is 1361 * positive infinity. 1362 * 1363 * <li> If the argument is zero, the result is 1364 * {@link Double#MIN_VALUE} 1365 * 1366 * </ul> 1367 * 1368 * @param d starting floating-point value 1369 * @return The adjacent floating-point value closer to positive 1370 * infinity. 1371 * @since 1.6 1372 */ 1373 public static double nextUp(double d) { 1374 return sun.misc.FpUtils.nextUp(d); 1375 } 1376 1377 /** 1378 * Returns the floating-point value adjacent to {@code f} in 1379 * the direction of positive infinity. This method is 1380 * semantically equivalent to {@code nextAfter(f, 1381 * Float.POSITIVE_INFINITY)}; however, a {@code nextUp} 1382 * implementation may run faster than its equivalent 1383 * {@code nextAfter} call. 1384 * 1385 * <p>Special Cases: 1386 * <ul> 1387 * <li> If the argument is NaN, the result is NaN. 1388 * 1389 * <li> If the argument is positive infinity, the result is 1390 * positive infinity. 1391 * 1392 * <li> If the argument is zero, the result is 1393 * {@link Float#MIN_VALUE} 1394 * 1395 * </ul> 1396 * 1397 * @param f starting floating-point value 1398 * @return The adjacent floating-point value closer to positive 1399 * infinity. 1400 * @since 1.6 1401 */ 1402 public static float nextUp(float f) { 1403 return sun.misc.FpUtils.nextUp(f); 1404 } 1405 1406 1407 /** 1408 * Return {@code d} × 1409 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1410 * by a single correctly rounded floating-point multiply to a 1411 * member of the double value set. See the Java 1412 * Language Specification for a discussion of floating-point 1413 * value sets. If the exponent of the result is between {@link 1414 * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the 1415 * answer is calculated exactly. If the exponent of the result 1416 * would be larger than {@code Double.MAX_EXPONENT}, an 1417 * infinity is returned. Note that if the result is subnormal, 1418 * precision may be lost; that is, when {@code scalb(x, n)} 1419 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1420 * <i>x</i>. When the result is non-NaN, the result has the same 1421 * sign as {@code d}. 1422 * 1423 * <p>Special cases: 1424 * <ul> 1425 * <li> If the first argument is NaN, NaN is returned. 1426 * <li> If the first argument is infinite, then an infinity of the 1427 * same sign is returned. 1428 * <li> If the first argument is zero, then a zero of the same 1429 * sign is returned. 1430 * </ul> 1431 * 1432 * @param d number to be scaled by a power of two. 1433 * @param scaleFactor power of 2 used to scale {@code d} 1434 * @return {@code d} × 2<sup>{@code scaleFactor}</sup> 1435 * @since 1.6 1436 */ 1437 public static double scalb(double d, int scaleFactor) { 1438 return sun.misc.FpUtils.scalb(d, scaleFactor); 1439 } 1440 1441 /** 1442 * Return {@code f} × 1443 * 2<sup>{@code scaleFactor}</sup> rounded as if performed 1444 * by a single correctly rounded floating-point multiply to a 1445 * member of the float value set. See the Java 1446 * Language Specification for a discussion of floating-point 1447 * value sets. If the exponent of the result is between {@link 1448 * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the 1449 * answer is calculated exactly. If the exponent of the result 1450 * would be larger than {@code Float.MAX_EXPONENT}, an 1451 * infinity is returned. Note that if the result is subnormal, 1452 * precision may be lost; that is, when {@code scalb(x, n)} 1453 * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal 1454 * <i>x</i>. When the result is non-NaN, the result has the same 1455 * sign as {@code f}. 1456 * 1457 * <p>Special cases: 1458 * <ul> 1459 * <li> If the first argument is NaN, NaN is returned. 1460 * <li> If the first argument is infinite, then an infinity of the 1461 * same sign is returned. 1462 * <li> If the first argument is zero, then a zero of the same 1463 * sign is returned. 1464 * </ul> 1465 * 1466 * @param f number to be scaled by a power of two. 1467 * @param scaleFactor power of 2 used to scale {@code f} 1468 * @return {@code f} × 2<sup>{@code scaleFactor}</sup> 1469 * @since 1.6 1470 */ 1471 public static float scalb(float f, int scaleFactor) { 1472 return sun.misc.FpUtils.scalb(f, scaleFactor); 1473 } 1474 }