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