1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/src/main/java/java/lang/Math.java Sat Sep 29 10:56:23 2012 +0200
1.3 @@ -0,0 +1,1526 @@
1.4 +/*
1.5 + * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +import java.util.Random;
1.31 +
1.32 +
1.33 +/**
1.34 + * The class {@code Math} contains methods for performing basic
1.35 + * numeric operations such as the elementary exponential, logarithm,
1.36 + * square root, and trigonometric functions.
1.37 + *
1.38 + * <p>Unlike some of the numeric methods of class
1.39 + * {@code StrictMath}, all implementations of the equivalent
1.40 + * functions of class {@code Math} are not defined to return the
1.41 + * bit-for-bit same results. This relaxation permits
1.42 + * better-performing implementations where strict reproducibility is
1.43 + * not required.
1.44 + *
1.45 + * <p>By default many of the {@code Math} methods simply call
1.46 + * the equivalent method in {@code StrictMath} for their
1.47 + * implementation. Code generators are encouraged to use
1.48 + * platform-specific native libraries or microprocessor instructions,
1.49 + * where available, to provide higher-performance implementations of
1.50 + * {@code Math} methods. Such higher-performance
1.51 + * implementations still must conform to the specification for
1.52 + * {@code Math}.
1.53 + *
1.54 + * <p>The quality of implementation specifications concern two
1.55 + * properties, accuracy of the returned result and monotonicity of the
1.56 + * method. Accuracy of the floating-point {@code Math} methods
1.57 + * is measured in terms of <i>ulps</i>, units in the last place. For
1.58 + * a given floating-point format, an ulp of a specific real number
1.59 + * value is the distance between the two floating-point values
1.60 + * bracketing that numerical value. When discussing the accuracy of a
1.61 + * method as a whole rather than at a specific argument, the number of
1.62 + * ulps cited is for the worst-case error at any argument. If a
1.63 + * method always has an error less than 0.5 ulps, the method always
1.64 + * returns the floating-point number nearest the exact result; such a
1.65 + * method is <i>correctly rounded</i>. A correctly rounded method is
1.66 + * generally the best a floating-point approximation can be; however,
1.67 + * it is impractical for many floating-point methods to be correctly
1.68 + * rounded. Instead, for the {@code Math} class, a larger error
1.69 + * bound of 1 or 2 ulps is allowed for certain methods. Informally,
1.70 + * with a 1 ulp error bound, when the exact result is a representable
1.71 + * number, the exact result should be returned as the computed result;
1.72 + * otherwise, either of the two floating-point values which bracket
1.73 + * the exact result may be returned. For exact results large in
1.74 + * magnitude, one of the endpoints of the bracket may be infinite.
1.75 + * Besides accuracy at individual arguments, maintaining proper
1.76 + * relations between the method at different arguments is also
1.77 + * important. Therefore, most methods with more than 0.5 ulp errors
1.78 + * are required to be <i>semi-monotonic</i>: whenever the mathematical
1.79 + * function is non-decreasing, so is the floating-point approximation,
1.80 + * likewise, whenever the mathematical function is non-increasing, so
1.81 + * is the floating-point approximation. Not all approximations that
1.82 + * have 1 ulp accuracy will automatically meet the monotonicity
1.83 + * requirements.
1.84 + *
1.85 + * @author unascribed
1.86 + * @author Joseph D. Darcy
1.87 + * @since JDK1.0
1.88 + */
1.89 +
1.90 +public final class Math {
1.91 +
1.92 + /**
1.93 + * Don't let anyone instantiate this class.
1.94 + */
1.95 + private Math() {}
1.96 +
1.97 + /**
1.98 + * The {@code double} value that is closer than any other to
1.99 + * <i>e</i>, the base of the natural logarithms.
1.100 + */
1.101 + public static final double E = 2.7182818284590452354;
1.102 +
1.103 + /**
1.104 + * The {@code double} value that is closer than any other to
1.105 + * <i>pi</i>, the ratio of the circumference of a circle to its
1.106 + * diameter.
1.107 + */
1.108 + public static final double PI = 3.14159265358979323846;
1.109 +
1.110 + /**
1.111 + * Returns the trigonometric sine of an angle. Special cases:
1.112 + * <ul><li>If the argument is NaN or an infinity, then the
1.113 + * result is NaN.
1.114 + * <li>If the argument is zero, then the result is a zero with the
1.115 + * same sign as the argument.</ul>
1.116 + *
1.117 + * <p>The computed result must be within 1 ulp of the exact result.
1.118 + * Results must be semi-monotonic.
1.119 + *
1.120 + * @param a an angle, in radians.
1.121 + * @return the sine of the argument.
1.122 + */
1.123 + public static double sin(double a) {
1.124 + return StrictMath.sin(a); // default impl. delegates to StrictMath
1.125 + }
1.126 +
1.127 + /**
1.128 + * Returns the trigonometric cosine of an angle. Special cases:
1.129 + * <ul><li>If the argument is NaN or an infinity, then the
1.130 + * result is NaN.</ul>
1.131 + *
1.132 + * <p>The computed result must be within 1 ulp of the exact result.
1.133 + * Results must be semi-monotonic.
1.134 + *
1.135 + * @param a an angle, in radians.
1.136 + * @return the cosine of the argument.
1.137 + */
1.138 + public static double cos(double a) {
1.139 + return StrictMath.cos(a); // default impl. delegates to StrictMath
1.140 + }
1.141 +
1.142 + /**
1.143 + * Returns the trigonometric tangent of an angle. Special cases:
1.144 + * <ul><li>If the argument is NaN or an infinity, then the result
1.145 + * is NaN.
1.146 + * <li>If the argument is zero, then the result is a zero with the
1.147 + * same sign as the argument.</ul>
1.148 + *
1.149 + * <p>The computed result must be within 1 ulp of the exact result.
1.150 + * Results must be semi-monotonic.
1.151 + *
1.152 + * @param a an angle, in radians.
1.153 + * @return the tangent of the argument.
1.154 + */
1.155 + public static double tan(double a) {
1.156 + return StrictMath.tan(a); // default impl. delegates to StrictMath
1.157 + }
1.158 +
1.159 + /**
1.160 + * Returns the arc sine of a value; the returned angle is in the
1.161 + * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
1.162 + * <ul><li>If the argument is NaN or its absolute value is greater
1.163 + * than 1, then the result is NaN.
1.164 + * <li>If the argument is zero, then the result is a zero with the
1.165 + * same sign as the argument.</ul>
1.166 + *
1.167 + * <p>The computed result must be within 1 ulp of the exact result.
1.168 + * Results must be semi-monotonic.
1.169 + *
1.170 + * @param a the value whose arc sine is to be returned.
1.171 + * @return the arc sine of the argument.
1.172 + */
1.173 + public static double asin(double a) {
1.174 + return StrictMath.asin(a); // default impl. delegates to StrictMath
1.175 + }
1.176 +
1.177 + /**
1.178 + * Returns the arc cosine of a value; the returned angle is in the
1.179 + * range 0.0 through <i>pi</i>. Special case:
1.180 + * <ul><li>If the argument is NaN or its absolute value is greater
1.181 + * than 1, then the result is NaN.</ul>
1.182 + *
1.183 + * <p>The computed result must be within 1 ulp of the exact result.
1.184 + * Results must be semi-monotonic.
1.185 + *
1.186 + * @param a the value whose arc cosine is to be returned.
1.187 + * @return the arc cosine of the argument.
1.188 + */
1.189 + public static double acos(double a) {
1.190 + return StrictMath.acos(a); // default impl. delegates to StrictMath
1.191 + }
1.192 +
1.193 + /**
1.194 + * Returns the arc tangent of a value; the returned angle is in the
1.195 + * range -<i>pi</i>/2 through <i>pi</i>/2. Special cases:
1.196 + * <ul><li>If the argument is NaN, then the result is NaN.
1.197 + * <li>If the argument is zero, then the result is a zero with the
1.198 + * same sign as the argument.</ul>
1.199 + *
1.200 + * <p>The computed result must be within 1 ulp of the exact result.
1.201 + * Results must be semi-monotonic.
1.202 + *
1.203 + * @param a the value whose arc tangent is to be returned.
1.204 + * @return the arc tangent of the argument.
1.205 + */
1.206 + public static double atan(double a) {
1.207 + return StrictMath.atan(a); // default impl. delegates to StrictMath
1.208 + }
1.209 +
1.210 + /**
1.211 + * Converts an angle measured in degrees to an approximately
1.212 + * equivalent angle measured in radians. The conversion from
1.213 + * degrees to radians is generally inexact.
1.214 + *
1.215 + * @param angdeg an angle, in degrees
1.216 + * @return the measurement of the angle {@code angdeg}
1.217 + * in radians.
1.218 + * @since 1.2
1.219 + */
1.220 + public static double toRadians(double angdeg) {
1.221 + return angdeg / 180.0 * PI;
1.222 + }
1.223 +
1.224 + /**
1.225 + * Converts an angle measured in radians to an approximately
1.226 + * equivalent angle measured in degrees. The conversion from
1.227 + * radians to degrees is generally inexact; users should
1.228 + * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
1.229 + * equal {@code 0.0}.
1.230 + *
1.231 + * @param angrad an angle, in radians
1.232 + * @return the measurement of the angle {@code angrad}
1.233 + * in degrees.
1.234 + * @since 1.2
1.235 + */
1.236 + public static double toDegrees(double angrad) {
1.237 + return angrad * 180.0 / PI;
1.238 + }
1.239 +
1.240 + /**
1.241 + * Returns Euler's number <i>e</i> raised to the power of a
1.242 + * {@code double} value. Special cases:
1.243 + * <ul><li>If the argument is NaN, the result is NaN.
1.244 + * <li>If the argument is positive infinity, then the result is
1.245 + * positive infinity.
1.246 + * <li>If the argument is negative infinity, then the result is
1.247 + * positive zero.</ul>
1.248 + *
1.249 + * <p>The computed result must be within 1 ulp of the exact result.
1.250 + * Results must be semi-monotonic.
1.251 + *
1.252 + * @param a the exponent to raise <i>e</i> to.
1.253 + * @return the value <i>e</i><sup>{@code a}</sup>,
1.254 + * where <i>e</i> is the base of the natural logarithms.
1.255 + */
1.256 + public static double exp(double a) {
1.257 + return StrictMath.exp(a); // default impl. delegates to StrictMath
1.258 + }
1.259 +
1.260 + /**
1.261 + * Returns the natural logarithm (base <i>e</i>) of a {@code double}
1.262 + * value. Special cases:
1.263 + * <ul><li>If the argument is NaN or less than zero, then the result
1.264 + * is NaN.
1.265 + * <li>If the argument is positive infinity, then the result is
1.266 + * positive infinity.
1.267 + * <li>If the argument is positive zero or negative zero, then the
1.268 + * result is negative infinity.</ul>
1.269 + *
1.270 + * <p>The computed result must be within 1 ulp of the exact result.
1.271 + * Results must be semi-monotonic.
1.272 + *
1.273 + * @param a a value
1.274 + * @return the value ln {@code a}, the natural logarithm of
1.275 + * {@code a}.
1.276 + */
1.277 + public static double log(double a) {
1.278 + return StrictMath.log(a); // default impl. delegates to StrictMath
1.279 + }
1.280 +
1.281 + /**
1.282 + * Returns the base 10 logarithm of a {@code double} value.
1.283 + * Special cases:
1.284 + *
1.285 + * <ul><li>If the argument is NaN or less than zero, then the result
1.286 + * is NaN.
1.287 + * <li>If the argument is positive infinity, then the result is
1.288 + * positive infinity.
1.289 + * <li>If the argument is positive zero or negative zero, then the
1.290 + * result is negative infinity.
1.291 + * <li> If the argument is equal to 10<sup><i>n</i></sup> for
1.292 + * integer <i>n</i>, then the result is <i>n</i>.
1.293 + * </ul>
1.294 + *
1.295 + * <p>The computed result must be within 1 ulp of the exact result.
1.296 + * Results must be semi-monotonic.
1.297 + *
1.298 + * @param a a value
1.299 + * @return the base 10 logarithm of {@code a}.
1.300 + * @since 1.5
1.301 + */
1.302 + public static double log10(double a) {
1.303 + return StrictMath.log10(a); // default impl. delegates to StrictMath
1.304 + }
1.305 +
1.306 + /**
1.307 + * Returns the correctly rounded positive square root of a
1.308 + * {@code double} value.
1.309 + * Special cases:
1.310 + * <ul><li>If the argument is NaN or less than zero, then the result
1.311 + * is NaN.
1.312 + * <li>If the argument is positive infinity, then the result is positive
1.313 + * infinity.
1.314 + * <li>If the argument is positive zero or negative zero, then the
1.315 + * result is the same as the argument.</ul>
1.316 + * Otherwise, the result is the {@code double} value closest to
1.317 + * the true mathematical square root of the argument value.
1.318 + *
1.319 + * @param a a value.
1.320 + * @return the positive square root of {@code a}.
1.321 + * If the argument is NaN or less than zero, the result is NaN.
1.322 + */
1.323 + public static double sqrt(double a) {
1.324 + return StrictMath.sqrt(a); // default impl. delegates to StrictMath
1.325 + // Note that hardware sqrt instructions
1.326 + // frequently can be directly used by JITs
1.327 + // and should be much faster than doing
1.328 + // Math.sqrt in software.
1.329 + }
1.330 +
1.331 +
1.332 + /**
1.333 + * Returns the cube root of a {@code double} value. For
1.334 + * positive finite {@code x}, {@code cbrt(-x) ==
1.335 + * -cbrt(x)}; that is, the cube root of a negative value is
1.336 + * the negative of the cube root of that value's magnitude.
1.337 + *
1.338 + * Special cases:
1.339 + *
1.340 + * <ul>
1.341 + *
1.342 + * <li>If the argument is NaN, then the result is NaN.
1.343 + *
1.344 + * <li>If the argument is infinite, then the result is an infinity
1.345 + * with the same sign as the argument.
1.346 + *
1.347 + * <li>If the argument is zero, then the result is a zero with the
1.348 + * same sign as the argument.
1.349 + *
1.350 + * </ul>
1.351 + *
1.352 + * <p>The computed result must be within 1 ulp of the exact result.
1.353 + *
1.354 + * @param a a value.
1.355 + * @return the cube root of {@code a}.
1.356 + * @since 1.5
1.357 + */
1.358 + public static double cbrt(double a) {
1.359 + return StrictMath.cbrt(a);
1.360 + }
1.361 +
1.362 + /**
1.363 + * Computes the remainder operation on two arguments as prescribed
1.364 + * by the IEEE 754 standard.
1.365 + * The remainder value is mathematically equal to
1.366 + * <code>f1 - f2</code> × <i>n</i>,
1.367 + * where <i>n</i> is the mathematical integer closest to the exact
1.368 + * mathematical value of the quotient {@code f1/f2}, and if two
1.369 + * mathematical integers are equally close to {@code f1/f2},
1.370 + * then <i>n</i> is the integer that is even. If the remainder is
1.371 + * zero, its sign is the same as the sign of the first argument.
1.372 + * Special cases:
1.373 + * <ul><li>If either argument is NaN, or the first argument is infinite,
1.374 + * or the second argument is positive zero or negative zero, then the
1.375 + * result is NaN.
1.376 + * <li>If the first argument is finite and the second argument is
1.377 + * infinite, then the result is the same as the first argument.</ul>
1.378 + *
1.379 + * @param f1 the dividend.
1.380 + * @param f2 the divisor.
1.381 + * @return the remainder when {@code f1} is divided by
1.382 + * {@code f2}.
1.383 + */
1.384 + public static double IEEEremainder(double f1, double f2) {
1.385 + return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
1.386 + }
1.387 +
1.388 + /**
1.389 + * Returns the smallest (closest to negative infinity)
1.390 + * {@code double} value that is greater than or equal to the
1.391 + * argument and is equal to a mathematical integer. Special cases:
1.392 + * <ul><li>If the argument value is already equal to a
1.393 + * mathematical integer, then the result is the same as the
1.394 + * argument. <li>If the argument is NaN or an infinity or
1.395 + * positive zero or negative zero, then the result is the same as
1.396 + * the argument. <li>If the argument value is less than zero but
1.397 + * greater than -1.0, then the result is negative zero.</ul> Note
1.398 + * that the value of {@code Math.ceil(x)} is exactly the
1.399 + * value of {@code -Math.floor(-x)}.
1.400 + *
1.401 + *
1.402 + * @param a a value.
1.403 + * @return the smallest (closest to negative infinity)
1.404 + * floating-point value that is greater than or equal to
1.405 + * the argument and is equal to a mathematical integer.
1.406 + */
1.407 + public static double ceil(double a) {
1.408 + return StrictMath.ceil(a); // default impl. delegates to StrictMath
1.409 + }
1.410 +
1.411 + /**
1.412 + * Returns the largest (closest to positive infinity)
1.413 + * {@code double} value that is less than or equal to the
1.414 + * argument and is equal to a mathematical integer. Special cases:
1.415 + * <ul><li>If the argument value is already equal to a
1.416 + * mathematical integer, then the result is the same as the
1.417 + * argument. <li>If the argument is NaN or an infinity or
1.418 + * positive zero or negative zero, then the result is the same as
1.419 + * the argument.</ul>
1.420 + *
1.421 + * @param a a value.
1.422 + * @return the largest (closest to positive infinity)
1.423 + * floating-point value that less than or equal to the argument
1.424 + * and is equal to a mathematical integer.
1.425 + */
1.426 + public static double floor(double a) {
1.427 + return StrictMath.floor(a); // default impl. delegates to StrictMath
1.428 + }
1.429 +
1.430 + /**
1.431 + * Returns the {@code double} value that is closest in value
1.432 + * to the argument and is equal to a mathematical integer. If two
1.433 + * {@code double} values that are mathematical integers are
1.434 + * equally close, the result is the integer value that is
1.435 + * even. Special cases:
1.436 + * <ul><li>If the argument value is already equal to a mathematical
1.437 + * integer, then the result is the same as the argument.
1.438 + * <li>If the argument is NaN or an infinity or positive zero or negative
1.439 + * zero, then the result is the same as the argument.</ul>
1.440 + *
1.441 + * @param a a {@code double} value.
1.442 + * @return the closest floating-point value to {@code a} that is
1.443 + * equal to a mathematical integer.
1.444 + */
1.445 + public static double rint(double a) {
1.446 + return StrictMath.rint(a); // default impl. delegates to StrictMath
1.447 + }
1.448 +
1.449 + /**
1.450 + * Returns the angle <i>theta</i> from the conversion of rectangular
1.451 + * coordinates ({@code x}, {@code y}) to polar
1.452 + * coordinates (r, <i>theta</i>).
1.453 + * This method computes the phase <i>theta</i> by computing an arc tangent
1.454 + * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
1.455 + * cases:
1.456 + * <ul><li>If either argument is NaN, then the result is NaN.
1.457 + * <li>If the first argument is positive zero and the second argument
1.458 + * is positive, or the first argument is positive and finite and the
1.459 + * second argument is positive infinity, then the result is positive
1.460 + * zero.
1.461 + * <li>If the first argument is negative zero and the second argument
1.462 + * is positive, or the first argument is negative and finite and the
1.463 + * second argument is positive infinity, then the result is negative zero.
1.464 + * <li>If the first argument is positive zero and the second argument
1.465 + * is negative, or the first argument is positive and finite and the
1.466 + * second argument is negative infinity, then the result is the
1.467 + * {@code double} value closest to <i>pi</i>.
1.468 + * <li>If the first argument is negative zero and the second argument
1.469 + * is negative, or the first argument is negative and finite and the
1.470 + * second argument is negative infinity, then the result is the
1.471 + * {@code double} value closest to -<i>pi</i>.
1.472 + * <li>If the first argument is positive and the second argument is
1.473 + * positive zero or negative zero, or the first argument is positive
1.474 + * infinity and the second argument is finite, then the result is the
1.475 + * {@code double} value closest to <i>pi</i>/2.
1.476 + * <li>If the first argument is negative and the second argument is
1.477 + * positive zero or negative zero, or the first argument is negative
1.478 + * infinity and the second argument is finite, then the result is the
1.479 + * {@code double} value closest to -<i>pi</i>/2.
1.480 + * <li>If both arguments are positive infinity, then the result is the
1.481 + * {@code double} value closest to <i>pi</i>/4.
1.482 + * <li>If the first argument is positive infinity and the second argument
1.483 + * is negative infinity, then the result is the {@code double}
1.484 + * value closest to 3*<i>pi</i>/4.
1.485 + * <li>If the first argument is negative infinity and the second argument
1.486 + * is positive infinity, then the result is the {@code double} value
1.487 + * closest to -<i>pi</i>/4.
1.488 + * <li>If both arguments are negative infinity, then the result is the
1.489 + * {@code double} value closest to -3*<i>pi</i>/4.</ul>
1.490 + *
1.491 + * <p>The computed result must be within 2 ulps of the exact result.
1.492 + * Results must be semi-monotonic.
1.493 + *
1.494 + * @param y the ordinate coordinate
1.495 + * @param x the abscissa coordinate
1.496 + * @return the <i>theta</i> component of the point
1.497 + * (<i>r</i>, <i>theta</i>)
1.498 + * in polar coordinates that corresponds to the point
1.499 + * (<i>x</i>, <i>y</i>) in Cartesian coordinates.
1.500 + */
1.501 + public static double atan2(double y, double x) {
1.502 + return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
1.503 + }
1.504 +
1.505 + /**
1.506 + * Returns the value of the first argument raised to the power of the
1.507 + * second argument. Special cases:
1.508 + *
1.509 + * <ul><li>If the second argument is positive or negative zero, then the
1.510 + * result is 1.0.
1.511 + * <li>If the second argument is 1.0, then the result is the same as the
1.512 + * first argument.
1.513 + * <li>If the second argument is NaN, then the result is NaN.
1.514 + * <li>If the first argument is NaN and the second argument is nonzero,
1.515 + * then the result is NaN.
1.516 + *
1.517 + * <li>If
1.518 + * <ul>
1.519 + * <li>the absolute value of the first argument is greater than 1
1.520 + * and the second argument is positive infinity, or
1.521 + * <li>the absolute value of the first argument is less than 1 and
1.522 + * the second argument is negative infinity,
1.523 + * </ul>
1.524 + * then the result is positive infinity.
1.525 + *
1.526 + * <li>If
1.527 + * <ul>
1.528 + * <li>the absolute value of the first argument is greater than 1 and
1.529 + * the second argument is negative infinity, or
1.530 + * <li>the absolute value of the
1.531 + * first argument is less than 1 and the second argument is positive
1.532 + * infinity,
1.533 + * </ul>
1.534 + * then the result is positive zero.
1.535 + *
1.536 + * <li>If the absolute value of the first argument equals 1 and the
1.537 + * second argument is infinite, then the result is NaN.
1.538 + *
1.539 + * <li>If
1.540 + * <ul>
1.541 + * <li>the first argument is positive zero and the second argument
1.542 + * is greater than zero, or
1.543 + * <li>the first argument is positive infinity and the second
1.544 + * argument is less than zero,
1.545 + * </ul>
1.546 + * then the result is positive zero.
1.547 + *
1.548 + * <li>If
1.549 + * <ul>
1.550 + * <li>the first argument is positive zero and the second argument
1.551 + * is less than zero, or
1.552 + * <li>the first argument is positive infinity and the second
1.553 + * argument is greater than zero,
1.554 + * </ul>
1.555 + * then the result is positive infinity.
1.556 + *
1.557 + * <li>If
1.558 + * <ul>
1.559 + * <li>the first argument is negative zero and the second argument
1.560 + * is greater than zero but not a finite odd integer, or
1.561 + * <li>the first argument is negative infinity and the second
1.562 + * argument is less than zero but not a finite odd integer,
1.563 + * </ul>
1.564 + * then the result is positive zero.
1.565 + *
1.566 + * <li>If
1.567 + * <ul>
1.568 + * <li>the first argument is negative zero and the second argument
1.569 + * is a positive finite odd integer, or
1.570 + * <li>the first argument is negative infinity and the second
1.571 + * argument is a negative finite odd integer,
1.572 + * </ul>
1.573 + * then the result is negative zero.
1.574 + *
1.575 + * <li>If
1.576 + * <ul>
1.577 + * <li>the first argument is negative zero and the second argument
1.578 + * is less than zero but not a finite odd integer, or
1.579 + * <li>the first argument is negative infinity and the second
1.580 + * argument is greater than zero but not a finite odd integer,
1.581 + * </ul>
1.582 + * then the result is positive infinity.
1.583 + *
1.584 + * <li>If
1.585 + * <ul>
1.586 + * <li>the first argument is negative zero and the second argument
1.587 + * is a negative finite odd integer, or
1.588 + * <li>the first argument is negative infinity and the second
1.589 + * argument is a positive finite odd integer,
1.590 + * </ul>
1.591 + * then the result is negative infinity.
1.592 + *
1.593 + * <li>If the first argument is finite and less than zero
1.594 + * <ul>
1.595 + * <li> if the second argument is a finite even integer, the
1.596 + * result is equal to the result of raising the absolute value of
1.597 + * the first argument to the power of the second argument
1.598 + *
1.599 + * <li>if the second argument is a finite odd integer, the result
1.600 + * is equal to the negative of the result of raising the absolute
1.601 + * value of the first argument to the power of the second
1.602 + * argument
1.603 + *
1.604 + * <li>if the second argument is finite and not an integer, then
1.605 + * the result is NaN.
1.606 + * </ul>
1.607 + *
1.608 + * <li>If both arguments are integers, then the result is exactly equal
1.609 + * to the mathematical result of raising the first argument to the power
1.610 + * of the second argument if that result can in fact be represented
1.611 + * exactly as a {@code double} value.</ul>
1.612 + *
1.613 + * <p>(In the foregoing descriptions, a floating-point value is
1.614 + * considered to be an integer if and only if it is finite and a
1.615 + * fixed point of the method {@link #ceil ceil} or,
1.616 + * equivalently, a fixed point of the method {@link #floor
1.617 + * floor}. A value is a fixed point of a one-argument
1.618 + * method if and only if the result of applying the method to the
1.619 + * value is equal to the value.)
1.620 + *
1.621 + * <p>The computed result must be within 1 ulp of the exact result.
1.622 + * Results must be semi-monotonic.
1.623 + *
1.624 + * @param a the base.
1.625 + * @param b the exponent.
1.626 + * @return the value {@code a}<sup>{@code b}</sup>.
1.627 + */
1.628 + public static double pow(double a, double b) {
1.629 + return StrictMath.pow(a, b); // default impl. delegates to StrictMath
1.630 + }
1.631 +
1.632 + /**
1.633 + * Returns the closest {@code int} to the argument, with ties
1.634 + * rounding up.
1.635 + *
1.636 + * <p>
1.637 + * Special cases:
1.638 + * <ul><li>If the argument is NaN, the result is 0.
1.639 + * <li>If the argument is negative infinity or any value less than or
1.640 + * equal to the value of {@code Integer.MIN_VALUE}, the result is
1.641 + * equal to the value of {@code Integer.MIN_VALUE}.
1.642 + * <li>If the argument is positive infinity or any value greater than or
1.643 + * equal to the value of {@code Integer.MAX_VALUE}, the result is
1.644 + * equal to the value of {@code Integer.MAX_VALUE}.</ul>
1.645 + *
1.646 + * @param a a floating-point value to be rounded to an integer.
1.647 + * @return the value of the argument rounded to the nearest
1.648 + * {@code int} value.
1.649 + * @see java.lang.Integer#MAX_VALUE
1.650 + * @see java.lang.Integer#MIN_VALUE
1.651 + */
1.652 + public static int round(float a) {
1.653 + if (a != 0x1.fffffep-2f) // greatest float value less than 0.5
1.654 + return (int)floor(a + 0.5f);
1.655 + else
1.656 + return 0;
1.657 + }
1.658 +
1.659 + /**
1.660 + * Returns the closest {@code long} to the argument, with ties
1.661 + * rounding up.
1.662 + *
1.663 + * <p>Special cases:
1.664 + * <ul><li>If the argument is NaN, the result is 0.
1.665 + * <li>If the argument is negative infinity or any value less than or
1.666 + * equal to the value of {@code Long.MIN_VALUE}, the result is
1.667 + * equal to the value of {@code Long.MIN_VALUE}.
1.668 + * <li>If the argument is positive infinity or any value greater than or
1.669 + * equal to the value of {@code Long.MAX_VALUE}, the result is
1.670 + * equal to the value of {@code Long.MAX_VALUE}.</ul>
1.671 + *
1.672 + * @param a a floating-point value to be rounded to a
1.673 + * {@code long}.
1.674 + * @return the value of the argument rounded to the nearest
1.675 + * {@code long} value.
1.676 + * @see java.lang.Long#MAX_VALUE
1.677 + * @see java.lang.Long#MIN_VALUE
1.678 + */
1.679 + public static long round(double a) {
1.680 + if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
1.681 + return (long)floor(a + 0.5d);
1.682 + else
1.683 + return 0;
1.684 + }
1.685 +
1.686 + private static Random randomNumberGenerator;
1.687 +
1.688 + private static synchronized Random initRNG() {
1.689 + Random rnd = randomNumberGenerator;
1.690 + return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
1.691 + }
1.692 +
1.693 + /**
1.694 + * Returns a {@code double} value with a positive sign, greater
1.695 + * than or equal to {@code 0.0} and less than {@code 1.0}.
1.696 + * Returned values are chosen pseudorandomly with (approximately)
1.697 + * uniform distribution from that range.
1.698 + *
1.699 + * <p>When this method is first called, it creates a single new
1.700 + * pseudorandom-number generator, exactly as if by the expression
1.701 + *
1.702 + * <blockquote>{@code new java.util.Random()}</blockquote>
1.703 + *
1.704 + * This new pseudorandom-number generator is used thereafter for
1.705 + * all calls to this method and is used nowhere else.
1.706 + *
1.707 + * <p>This method is properly synchronized to allow correct use by
1.708 + * more than one thread. However, if many threads need to generate
1.709 + * pseudorandom numbers at a great rate, it may reduce contention
1.710 + * for each thread to have its own pseudorandom-number generator.
1.711 + *
1.712 + * @return a pseudorandom {@code double} greater than or equal
1.713 + * to {@code 0.0} and less than {@code 1.0}.
1.714 + * @see Random#nextDouble()
1.715 + */
1.716 + public static double random() {
1.717 + Random rnd = randomNumberGenerator;
1.718 + if (rnd == null) rnd = initRNG();
1.719 + return rnd.nextDouble();
1.720 + }
1.721 +
1.722 + /**
1.723 + * Returns the absolute value of an {@code int} value.
1.724 + * If the argument is not negative, the argument is returned.
1.725 + * If the argument is negative, the negation of the argument is returned.
1.726 + *
1.727 + * <p>Note that if the argument is equal to the value of
1.728 + * {@link Integer#MIN_VALUE}, the most negative representable
1.729 + * {@code int} value, the result is that same value, which is
1.730 + * negative.
1.731 + *
1.732 + * @param a the argument whose absolute value is to be determined
1.733 + * @return the absolute value of the argument.
1.734 + */
1.735 + public static int abs(int a) {
1.736 + return (a < 0) ? -a : a;
1.737 + }
1.738 +
1.739 + /**
1.740 + * Returns the absolute value of a {@code long} value.
1.741 + * If the argument is not negative, the argument is returned.
1.742 + * If the argument is negative, the negation of the argument is returned.
1.743 + *
1.744 + * <p>Note that if the argument is equal to the value of
1.745 + * {@link Long#MIN_VALUE}, the most negative representable
1.746 + * {@code long} value, the result is that same value, which
1.747 + * is negative.
1.748 + *
1.749 + * @param a the argument whose absolute value is to be determined
1.750 + * @return the absolute value of the argument.
1.751 + */
1.752 + public static long abs(long a) {
1.753 + return (a < 0) ? -a : a;
1.754 + }
1.755 +
1.756 + /**
1.757 + * Returns the absolute value of a {@code float} value.
1.758 + * If the argument is not negative, the argument is returned.
1.759 + * If the argument is negative, the negation of the argument is returned.
1.760 + * Special cases:
1.761 + * <ul><li>If the argument is positive zero or negative zero, the
1.762 + * result is positive zero.
1.763 + * <li>If the argument is infinite, the result is positive infinity.
1.764 + * <li>If the argument is NaN, the result is NaN.</ul>
1.765 + * In other words, the result is the same as the value of the expression:
1.766 + * <p>{@code Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))}
1.767 + *
1.768 + * @param a the argument whose absolute value is to be determined
1.769 + * @return the absolute value of the argument.
1.770 + */
1.771 + public static float abs(float a) {
1.772 + return (a <= 0.0F) ? 0.0F - a : a;
1.773 + }
1.774 +
1.775 + /**
1.776 + * Returns the absolute value of a {@code double} value.
1.777 + * If the argument is not negative, the argument is returned.
1.778 + * If the argument is negative, the negation of the argument is returned.
1.779 + * Special cases:
1.780 + * <ul><li>If the argument is positive zero or negative zero, the result
1.781 + * is positive zero.
1.782 + * <li>If the argument is infinite, the result is positive infinity.
1.783 + * <li>If the argument is NaN, the result is NaN.</ul>
1.784 + * In other words, the result is the same as the value of the expression:
1.785 + * <p>{@code Double.longBitsToDouble((Double.doubleToLongBits(a)<<1)>>>1)}
1.786 + *
1.787 + * @param a the argument whose absolute value is to be determined
1.788 + * @return the absolute value of the argument.
1.789 + */
1.790 + public static double abs(double a) {
1.791 + return (a <= 0.0D) ? 0.0D - a : a;
1.792 + }
1.793 +
1.794 + /**
1.795 + * Returns the greater of two {@code int} values. That is, the
1.796 + * result is the argument closer to the value of
1.797 + * {@link Integer#MAX_VALUE}. If the arguments have the same value,
1.798 + * the result is that same value.
1.799 + *
1.800 + * @param a an argument.
1.801 + * @param b another argument.
1.802 + * @return the larger of {@code a} and {@code b}.
1.803 + */
1.804 + public static int max(int a, int b) {
1.805 + return (a >= b) ? a : b;
1.806 + }
1.807 +
1.808 + /**
1.809 + * Returns the greater of two {@code long} values. That is, the
1.810 + * result is the argument closer to the value of
1.811 + * {@link Long#MAX_VALUE}. If the arguments have the same value,
1.812 + * the result is that same value.
1.813 + *
1.814 + * @param a an argument.
1.815 + * @param b another argument.
1.816 + * @return the larger of {@code a} and {@code b}.
1.817 + */
1.818 + public static long max(long a, long b) {
1.819 + return (a >= b) ? a : b;
1.820 + }
1.821 +
1.822 + private static long negativeZeroFloatBits = Float.floatToIntBits(-0.0f);
1.823 + private static long negativeZeroDoubleBits = Double.doubleToLongBits(-0.0d);
1.824 +
1.825 + /**
1.826 + * Returns the greater of two {@code float} values. That is,
1.827 + * the result is the argument closer to positive infinity. If the
1.828 + * arguments have the same value, the result is that same
1.829 + * value. If either value is NaN, then the result is NaN. Unlike
1.830 + * the numerical comparison operators, this method considers
1.831 + * negative zero to be strictly smaller than positive zero. If one
1.832 + * argument is positive zero and the other negative zero, the
1.833 + * result is positive zero.
1.834 + *
1.835 + * @param a an argument.
1.836 + * @param b another argument.
1.837 + * @return the larger of {@code a} and {@code b}.
1.838 + */
1.839 + public static float max(float a, float b) {
1.840 + if (a != a) return a; // a is NaN
1.841 + if ((a == 0.0f) && (b == 0.0f)
1.842 + && (Float.floatToIntBits(a) == negativeZeroFloatBits)) {
1.843 + return b;
1.844 + }
1.845 + return (a >= b) ? a : b;
1.846 + }
1.847 +
1.848 + /**
1.849 + * Returns the greater of two {@code double} values. That
1.850 + * is, the result is the argument closer to positive infinity. If
1.851 + * the arguments have the same value, the result is that same
1.852 + * value. If either value is NaN, then the result is NaN. Unlike
1.853 + * the numerical comparison operators, this method considers
1.854 + * negative zero to be strictly smaller than positive zero. If one
1.855 + * argument is positive zero and the other negative zero, the
1.856 + * result is positive zero.
1.857 + *
1.858 + * @param a an argument.
1.859 + * @param b another argument.
1.860 + * @return the larger of {@code a} and {@code b}.
1.861 + */
1.862 + public static double max(double a, double b) {
1.863 + if (a != a) return a; // a is NaN
1.864 + if ((a == 0.0d) && (b == 0.0d)
1.865 + && (Double.doubleToLongBits(a) == negativeZeroDoubleBits)) {
1.866 + return b;
1.867 + }
1.868 + return (a >= b) ? a : b;
1.869 + }
1.870 +
1.871 + /**
1.872 + * Returns the smaller of two {@code int} values. That is,
1.873 + * the result the argument closer to the value of
1.874 + * {@link Integer#MIN_VALUE}. If the arguments have the same
1.875 + * value, the result is that same value.
1.876 + *
1.877 + * @param a an argument.
1.878 + * @param b another argument.
1.879 + * @return the smaller of {@code a} and {@code b}.
1.880 + */
1.881 + public static int min(int a, int b) {
1.882 + return (a <= b) ? a : b;
1.883 + }
1.884 +
1.885 + /**
1.886 + * Returns the smaller of two {@code long} values. That is,
1.887 + * the result is the argument closer to the value of
1.888 + * {@link Long#MIN_VALUE}. If the arguments have the same
1.889 + * value, the result is that same value.
1.890 + *
1.891 + * @param a an argument.
1.892 + * @param b another argument.
1.893 + * @return the smaller of {@code a} and {@code b}.
1.894 + */
1.895 + public static long min(long a, long b) {
1.896 + return (a <= b) ? a : b;
1.897 + }
1.898 +
1.899 + /**
1.900 + * Returns the smaller of two {@code float} values. That is,
1.901 + * the result is the value closer to negative infinity. If the
1.902 + * arguments have the same value, the result is that same
1.903 + * value. If either value is NaN, then the result is NaN. Unlike
1.904 + * the numerical comparison operators, this method considers
1.905 + * negative zero to be strictly smaller than positive zero. If
1.906 + * one argument is positive zero and the other is negative zero,
1.907 + * the result is negative zero.
1.908 + *
1.909 + * @param a an argument.
1.910 + * @param b another argument.
1.911 + * @return the smaller of {@code a} and {@code b}.
1.912 + */
1.913 + public static float min(float a, float b) {
1.914 + if (a != a) return a; // a is NaN
1.915 + if ((a == 0.0f) && (b == 0.0f)
1.916 + && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
1.917 + return b;
1.918 + }
1.919 + return (a <= b) ? a : b;
1.920 + }
1.921 +
1.922 + /**
1.923 + * Returns the smaller of two {@code double} values. That
1.924 + * is, the result is the value closer to negative infinity. If the
1.925 + * arguments have the same value, the result is that same
1.926 + * value. If either value is NaN, then the result is NaN. Unlike
1.927 + * the numerical comparison operators, this method considers
1.928 + * negative zero to be strictly smaller than positive zero. If one
1.929 + * argument is positive zero and the other is negative zero, the
1.930 + * result is negative zero.
1.931 + *
1.932 + * @param a an argument.
1.933 + * @param b another argument.
1.934 + * @return the smaller of {@code a} and {@code b}.
1.935 + */
1.936 + public static double min(double a, double b) {
1.937 + if (a != a) return a; // a is NaN
1.938 + if ((a == 0.0d) && (b == 0.0d)
1.939 + && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
1.940 + return b;
1.941 + }
1.942 + return (a <= b) ? a : b;
1.943 + }
1.944 +
1.945 + /**
1.946 + * Returns the size of an ulp of the argument. An ulp of a
1.947 + * {@code double} value is the positive distance between this
1.948 + * floating-point value and the {@code double} value next
1.949 + * larger in magnitude. Note that for non-NaN <i>x</i>,
1.950 + * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
1.951 + *
1.952 + * <p>Special Cases:
1.953 + * <ul>
1.954 + * <li> If the argument is NaN, then the result is NaN.
1.955 + * <li> If the argument is positive or negative infinity, then the
1.956 + * result is positive infinity.
1.957 + * <li> If the argument is positive or negative zero, then the result is
1.958 + * {@code Double.MIN_VALUE}.
1.959 + * <li> If the argument is ±{@code Double.MAX_VALUE}, then
1.960 + * the result is equal to 2<sup>971</sup>.
1.961 + * </ul>
1.962 + *
1.963 + * @param d the floating-point value whose ulp is to be returned
1.964 + * @return the size of an ulp of the argument
1.965 + * @author Joseph D. Darcy
1.966 + * @since 1.5
1.967 + */
1.968 + public static double ulp(double d) {
1.969 + return sun.misc.FpUtils.ulp(d);
1.970 + }
1.971 +
1.972 + /**
1.973 + * Returns the size of an ulp of the argument. An ulp of a
1.974 + * {@code float} value is the positive distance between this
1.975 + * floating-point value and the {@code float} value next
1.976 + * larger in magnitude. Note that for non-NaN <i>x</i>,
1.977 + * <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
1.978 + *
1.979 + * <p>Special Cases:
1.980 + * <ul>
1.981 + * <li> If the argument is NaN, then the result is NaN.
1.982 + * <li> If the argument is positive or negative infinity, then the
1.983 + * result is positive infinity.
1.984 + * <li> If the argument is positive or negative zero, then the result is
1.985 + * {@code Float.MIN_VALUE}.
1.986 + * <li> If the argument is ±{@code Float.MAX_VALUE}, then
1.987 + * the result is equal to 2<sup>104</sup>.
1.988 + * </ul>
1.989 + *
1.990 + * @param f the floating-point value whose ulp is to be returned
1.991 + * @return the size of an ulp of the argument
1.992 + * @author Joseph D. Darcy
1.993 + * @since 1.5
1.994 + */
1.995 + public static float ulp(float f) {
1.996 + return sun.misc.FpUtils.ulp(f);
1.997 + }
1.998 +
1.999 + /**
1.1000 + * Returns the signum function of the argument; zero if the argument
1.1001 + * is zero, 1.0 if the argument is greater than zero, -1.0 if the
1.1002 + * argument is less than zero.
1.1003 + *
1.1004 + * <p>Special Cases:
1.1005 + * <ul>
1.1006 + * <li> If the argument is NaN, then the result is NaN.
1.1007 + * <li> If the argument is positive zero or negative zero, then the
1.1008 + * result is the same as the argument.
1.1009 + * </ul>
1.1010 + *
1.1011 + * @param d the floating-point value whose signum is to be returned
1.1012 + * @return the signum function of the argument
1.1013 + * @author Joseph D. Darcy
1.1014 + * @since 1.5
1.1015 + */
1.1016 + public static double signum(double d) {
1.1017 + return sun.misc.FpUtils.signum(d);
1.1018 + }
1.1019 +
1.1020 + /**
1.1021 + * Returns the signum function of the argument; zero if the argument
1.1022 + * is zero, 1.0f if the argument is greater than zero, -1.0f if the
1.1023 + * argument is less than zero.
1.1024 + *
1.1025 + * <p>Special Cases:
1.1026 + * <ul>
1.1027 + * <li> If the argument is NaN, then the result is NaN.
1.1028 + * <li> If the argument is positive zero or negative zero, then the
1.1029 + * result is the same as the argument.
1.1030 + * </ul>
1.1031 + *
1.1032 + * @param f the floating-point value whose signum is to be returned
1.1033 + * @return the signum function of the argument
1.1034 + * @author Joseph D. Darcy
1.1035 + * @since 1.5
1.1036 + */
1.1037 + public static float signum(float f) {
1.1038 + return sun.misc.FpUtils.signum(f);
1.1039 + }
1.1040 +
1.1041 + /**
1.1042 + * Returns the hyperbolic sine of a {@code double} value.
1.1043 + * The hyperbolic sine of <i>x</i> is defined to be
1.1044 + * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/2
1.1045 + * where <i>e</i> is {@linkplain Math#E Euler's number}.
1.1046 + *
1.1047 + * <p>Special cases:
1.1048 + * <ul>
1.1049 + *
1.1050 + * <li>If the argument is NaN, then the result is NaN.
1.1051 + *
1.1052 + * <li>If the argument is infinite, then the result is an infinity
1.1053 + * with the same sign as the argument.
1.1054 + *
1.1055 + * <li>If the argument is zero, then the result is a zero with the
1.1056 + * same sign as the argument.
1.1057 + *
1.1058 + * </ul>
1.1059 + *
1.1060 + * <p>The computed result must be within 2.5 ulps of the exact result.
1.1061 + *
1.1062 + * @param x The number whose hyperbolic sine is to be returned.
1.1063 + * @return The hyperbolic sine of {@code x}.
1.1064 + * @since 1.5
1.1065 + */
1.1066 + public static double sinh(double x) {
1.1067 + return StrictMath.sinh(x);
1.1068 + }
1.1069 +
1.1070 + /**
1.1071 + * Returns the hyperbolic cosine of a {@code double} value.
1.1072 + * The hyperbolic cosine of <i>x</i> is defined to be
1.1073 + * (<i>e<sup>x</sup> + e<sup>-x</sup></i>)/2
1.1074 + * where <i>e</i> is {@linkplain Math#E Euler's number}.
1.1075 + *
1.1076 + * <p>Special cases:
1.1077 + * <ul>
1.1078 + *
1.1079 + * <li>If the argument is NaN, then the result is NaN.
1.1080 + *
1.1081 + * <li>If the argument is infinite, then the result is positive
1.1082 + * infinity.
1.1083 + *
1.1084 + * <li>If the argument is zero, then the result is {@code 1.0}.
1.1085 + *
1.1086 + * </ul>
1.1087 + *
1.1088 + * <p>The computed result must be within 2.5 ulps of the exact result.
1.1089 + *
1.1090 + * @param x The number whose hyperbolic cosine is to be returned.
1.1091 + * @return The hyperbolic cosine of {@code x}.
1.1092 + * @since 1.5
1.1093 + */
1.1094 + public static double cosh(double x) {
1.1095 + return StrictMath.cosh(x);
1.1096 + }
1.1097 +
1.1098 + /**
1.1099 + * Returns the hyperbolic tangent of a {@code double} value.
1.1100 + * The hyperbolic tangent of <i>x</i> is defined to be
1.1101 + * (<i>e<sup>x</sup> - e<sup>-x</sup></i>)/(<i>e<sup>x</sup> + e<sup>-x</sup></i>),
1.1102 + * in other words, {@linkplain Math#sinh
1.1103 + * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}. Note
1.1104 + * that the absolute value of the exact tanh is always less than
1.1105 + * 1.
1.1106 + *
1.1107 + * <p>Special cases:
1.1108 + * <ul>
1.1109 + *
1.1110 + * <li>If the argument is NaN, then the result is NaN.
1.1111 + *
1.1112 + * <li>If the argument is zero, then the result is a zero with the
1.1113 + * same sign as the argument.
1.1114 + *
1.1115 + * <li>If the argument is positive infinity, then the result is
1.1116 + * {@code +1.0}.
1.1117 + *
1.1118 + * <li>If the argument is negative infinity, then the result is
1.1119 + * {@code -1.0}.
1.1120 + *
1.1121 + * </ul>
1.1122 + *
1.1123 + * <p>The computed result must be within 2.5 ulps of the exact result.
1.1124 + * The result of {@code tanh} for any finite input must have
1.1125 + * an absolute value less than or equal to 1. Note that once the
1.1126 + * exact result of tanh is within 1/2 of an ulp of the limit value
1.1127 + * of ±1, correctly signed ±{@code 1.0} should
1.1128 + * be returned.
1.1129 + *
1.1130 + * @param x The number whose hyperbolic tangent is to be returned.
1.1131 + * @return The hyperbolic tangent of {@code x}.
1.1132 + * @since 1.5
1.1133 + */
1.1134 + public static double tanh(double x) {
1.1135 + return StrictMath.tanh(x);
1.1136 + }
1.1137 +
1.1138 + /**
1.1139 + * Returns sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1.1140 + * without intermediate overflow or underflow.
1.1141 + *
1.1142 + * <p>Special cases:
1.1143 + * <ul>
1.1144 + *
1.1145 + * <li> If either argument is infinite, then the result
1.1146 + * is positive infinity.
1.1147 + *
1.1148 + * <li> If either argument is NaN and neither argument is infinite,
1.1149 + * then the result is NaN.
1.1150 + *
1.1151 + * </ul>
1.1152 + *
1.1153 + * <p>The computed result must be within 1 ulp of the exact
1.1154 + * result. If one parameter is held constant, the results must be
1.1155 + * semi-monotonic in the other parameter.
1.1156 + *
1.1157 + * @param x a value
1.1158 + * @param y a value
1.1159 + * @return sqrt(<i>x</i><sup>2</sup> +<i>y</i><sup>2</sup>)
1.1160 + * without intermediate overflow or underflow
1.1161 + * @since 1.5
1.1162 + */
1.1163 + public static double hypot(double x, double y) {
1.1164 + return StrictMath.hypot(x, y);
1.1165 + }
1.1166 +
1.1167 + /**
1.1168 + * Returns <i>e</i><sup>x</sup> -1. Note that for values of
1.1169 + * <i>x</i> near 0, the exact sum of
1.1170 + * {@code expm1(x)} + 1 is much closer to the true
1.1171 + * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
1.1172 + *
1.1173 + * <p>Special cases:
1.1174 + * <ul>
1.1175 + * <li>If the argument is NaN, the result is NaN.
1.1176 + *
1.1177 + * <li>If the argument is positive infinity, then the result is
1.1178 + * positive infinity.
1.1179 + *
1.1180 + * <li>If the argument is negative infinity, then the result is
1.1181 + * -1.0.
1.1182 + *
1.1183 + * <li>If the argument is zero, then the result is a zero with the
1.1184 + * same sign as the argument.
1.1185 + *
1.1186 + * </ul>
1.1187 + *
1.1188 + * <p>The computed result must be within 1 ulp of the exact result.
1.1189 + * Results must be semi-monotonic. The result of
1.1190 + * {@code expm1} for any finite input must be greater than or
1.1191 + * equal to {@code -1.0}. Note that once the exact result of
1.1192 + * <i>e</i><sup>{@code x}</sup> - 1 is within 1/2
1.1193 + * ulp of the limit value -1, {@code -1.0} should be
1.1194 + * returned.
1.1195 + *
1.1196 + * @param x the exponent to raise <i>e</i> to in the computation of
1.1197 + * <i>e</i><sup>{@code x}</sup> -1.
1.1198 + * @return the value <i>e</i><sup>{@code x}</sup> - 1.
1.1199 + * @since 1.5
1.1200 + */
1.1201 + public static double expm1(double x) {
1.1202 + return StrictMath.expm1(x);
1.1203 + }
1.1204 +
1.1205 + /**
1.1206 + * Returns the natural logarithm of the sum of the argument and 1.
1.1207 + * Note that for small values {@code x}, the result of
1.1208 + * {@code log1p(x)} is much closer to the true result of ln(1
1.1209 + * + {@code x}) than the floating-point evaluation of
1.1210 + * {@code log(1.0+x)}.
1.1211 + *
1.1212 + * <p>Special cases:
1.1213 + *
1.1214 + * <ul>
1.1215 + *
1.1216 + * <li>If the argument is NaN or less than -1, then the result is
1.1217 + * NaN.
1.1218 + *
1.1219 + * <li>If the argument is positive infinity, then the result is
1.1220 + * positive infinity.
1.1221 + *
1.1222 + * <li>If the argument is negative one, then the result is
1.1223 + * negative infinity.
1.1224 + *
1.1225 + * <li>If the argument is zero, then the result is a zero with the
1.1226 + * same sign as the argument.
1.1227 + *
1.1228 + * </ul>
1.1229 + *
1.1230 + * <p>The computed result must be within 1 ulp of the exact result.
1.1231 + * Results must be semi-monotonic.
1.1232 + *
1.1233 + * @param x a value
1.1234 + * @return the value ln({@code x} + 1), the natural
1.1235 + * log of {@code x} + 1
1.1236 + * @since 1.5
1.1237 + */
1.1238 + public static double log1p(double x) {
1.1239 + return StrictMath.log1p(x);
1.1240 + }
1.1241 +
1.1242 + /**
1.1243 + * Returns the first floating-point argument with the sign of the
1.1244 + * second floating-point argument. Note that unlike the {@link
1.1245 + * StrictMath#copySign(double, double) StrictMath.copySign}
1.1246 + * method, this method does not require NaN {@code sign}
1.1247 + * arguments to be treated as positive values; implementations are
1.1248 + * permitted to treat some NaN arguments as positive and other NaN
1.1249 + * arguments as negative to allow greater performance.
1.1250 + *
1.1251 + * @param magnitude the parameter providing the magnitude of the result
1.1252 + * @param sign the parameter providing the sign of the result
1.1253 + * @return a value with the magnitude of {@code magnitude}
1.1254 + * and the sign of {@code sign}.
1.1255 + * @since 1.6
1.1256 + */
1.1257 + public static double copySign(double magnitude, double sign) {
1.1258 + return sun.misc.FpUtils.rawCopySign(magnitude, sign);
1.1259 + }
1.1260 +
1.1261 + /**
1.1262 + * Returns the first floating-point argument with the sign of the
1.1263 + * second floating-point argument. Note that unlike the {@link
1.1264 + * StrictMath#copySign(float, float) StrictMath.copySign}
1.1265 + * method, this method does not require NaN {@code sign}
1.1266 + * arguments to be treated as positive values; implementations are
1.1267 + * permitted to treat some NaN arguments as positive and other NaN
1.1268 + * arguments as negative to allow greater performance.
1.1269 + *
1.1270 + * @param magnitude the parameter providing the magnitude of the result
1.1271 + * @param sign the parameter providing the sign of the result
1.1272 + * @return a value with the magnitude of {@code magnitude}
1.1273 + * and the sign of {@code sign}.
1.1274 + * @since 1.6
1.1275 + */
1.1276 + public static float copySign(float magnitude, float sign) {
1.1277 + return sun.misc.FpUtils.rawCopySign(magnitude, sign);
1.1278 + }
1.1279 +
1.1280 + /**
1.1281 + * Returns the unbiased exponent used in the representation of a
1.1282 + * {@code float}. Special cases:
1.1283 + *
1.1284 + * <ul>
1.1285 + * <li>If the argument is NaN or infinite, then the result is
1.1286 + * {@link Float#MAX_EXPONENT} + 1.
1.1287 + * <li>If the argument is zero or subnormal, then the result is
1.1288 + * {@link Float#MIN_EXPONENT} -1.
1.1289 + * </ul>
1.1290 + * @param f a {@code float} value
1.1291 + * @return the unbiased exponent of the argument
1.1292 + * @since 1.6
1.1293 + */
1.1294 + public static int getExponent(float f) {
1.1295 + return sun.misc.FpUtils.getExponent(f);
1.1296 + }
1.1297 +
1.1298 + /**
1.1299 + * Returns the unbiased exponent used in the representation of a
1.1300 + * {@code double}. Special cases:
1.1301 + *
1.1302 + * <ul>
1.1303 + * <li>If the argument is NaN or infinite, then the result is
1.1304 + * {@link Double#MAX_EXPONENT} + 1.
1.1305 + * <li>If the argument is zero or subnormal, then the result is
1.1306 + * {@link Double#MIN_EXPONENT} -1.
1.1307 + * </ul>
1.1308 + * @param d a {@code double} value
1.1309 + * @return the unbiased exponent of the argument
1.1310 + * @since 1.6
1.1311 + */
1.1312 + public static int getExponent(double d) {
1.1313 + return sun.misc.FpUtils.getExponent(d);
1.1314 + }
1.1315 +
1.1316 + /**
1.1317 + * Returns the floating-point number adjacent to the first
1.1318 + * argument in the direction of the second argument. If both
1.1319 + * arguments compare as equal the second argument is returned.
1.1320 + *
1.1321 + * <p>
1.1322 + * Special cases:
1.1323 + * <ul>
1.1324 + * <li> If either argument is a NaN, then NaN is returned.
1.1325 + *
1.1326 + * <li> If both arguments are signed zeros, {@code direction}
1.1327 + * is returned unchanged (as implied by the requirement of
1.1328 + * returning the second argument if the arguments compare as
1.1329 + * equal).
1.1330 + *
1.1331 + * <li> If {@code start} is
1.1332 + * ±{@link Double#MIN_VALUE} and {@code direction}
1.1333 + * has a value such that the result should have a smaller
1.1334 + * magnitude, then a zero with the same sign as {@code start}
1.1335 + * is returned.
1.1336 + *
1.1337 + * <li> If {@code start} is infinite and
1.1338 + * {@code direction} has a value such that the result should
1.1339 + * have a smaller magnitude, {@link Double#MAX_VALUE} with the
1.1340 + * same sign as {@code start} is returned.
1.1341 + *
1.1342 + * <li> If {@code start} is equal to ±
1.1343 + * {@link Double#MAX_VALUE} and {@code direction} has a
1.1344 + * value such that the result should have a larger magnitude, an
1.1345 + * infinity with same sign as {@code start} is returned.
1.1346 + * </ul>
1.1347 + *
1.1348 + * @param start starting floating-point value
1.1349 + * @param direction value indicating which of
1.1350 + * {@code start}'s neighbors or {@code start} should
1.1351 + * be returned
1.1352 + * @return The floating-point number adjacent to {@code start} in the
1.1353 + * direction of {@code direction}.
1.1354 + * @since 1.6
1.1355 + */
1.1356 + public static double nextAfter(double start, double direction) {
1.1357 + return sun.misc.FpUtils.nextAfter(start, direction);
1.1358 + }
1.1359 +
1.1360 + /**
1.1361 + * Returns the floating-point number adjacent to the first
1.1362 + * argument in the direction of the second argument. If both
1.1363 + * arguments compare as equal a value equivalent to the second argument
1.1364 + * is returned.
1.1365 + *
1.1366 + * <p>
1.1367 + * Special cases:
1.1368 + * <ul>
1.1369 + * <li> If either argument is a NaN, then NaN is returned.
1.1370 + *
1.1371 + * <li> If both arguments are signed zeros, a value equivalent
1.1372 + * to {@code direction} is returned.
1.1373 + *
1.1374 + * <li> If {@code start} is
1.1375 + * ±{@link Float#MIN_VALUE} and {@code direction}
1.1376 + * has a value such that the result should have a smaller
1.1377 + * magnitude, then a zero with the same sign as {@code start}
1.1378 + * is returned.
1.1379 + *
1.1380 + * <li> If {@code start} is infinite and
1.1381 + * {@code direction} has a value such that the result should
1.1382 + * have a smaller magnitude, {@link Float#MAX_VALUE} with the
1.1383 + * same sign as {@code start} is returned.
1.1384 + *
1.1385 + * <li> If {@code start} is equal to ±
1.1386 + * {@link Float#MAX_VALUE} and {@code direction} has a
1.1387 + * value such that the result should have a larger magnitude, an
1.1388 + * infinity with same sign as {@code start} is returned.
1.1389 + * </ul>
1.1390 + *
1.1391 + * @param start starting floating-point value
1.1392 + * @param direction value indicating which of
1.1393 + * {@code start}'s neighbors or {@code start} should
1.1394 + * be returned
1.1395 + * @return The floating-point number adjacent to {@code start} in the
1.1396 + * direction of {@code direction}.
1.1397 + * @since 1.6
1.1398 + */
1.1399 + public static float nextAfter(float start, double direction) {
1.1400 + return sun.misc.FpUtils.nextAfter(start, direction);
1.1401 + }
1.1402 +
1.1403 + /**
1.1404 + * Returns the floating-point value adjacent to {@code d} in
1.1405 + * the direction of positive infinity. This method is
1.1406 + * semantically equivalent to {@code nextAfter(d,
1.1407 + * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
1.1408 + * implementation may run faster than its equivalent
1.1409 + * {@code nextAfter} call.
1.1410 + *
1.1411 + * <p>Special Cases:
1.1412 + * <ul>
1.1413 + * <li> If the argument is NaN, the result is NaN.
1.1414 + *
1.1415 + * <li> If the argument is positive infinity, the result is
1.1416 + * positive infinity.
1.1417 + *
1.1418 + * <li> If the argument is zero, the result is
1.1419 + * {@link Double#MIN_VALUE}
1.1420 + *
1.1421 + * </ul>
1.1422 + *
1.1423 + * @param d starting floating-point value
1.1424 + * @return The adjacent floating-point value closer to positive
1.1425 + * infinity.
1.1426 + * @since 1.6
1.1427 + */
1.1428 + public static double nextUp(double d) {
1.1429 + return sun.misc.FpUtils.nextUp(d);
1.1430 + }
1.1431 +
1.1432 + /**
1.1433 + * Returns the floating-point value adjacent to {@code f} in
1.1434 + * the direction of positive infinity. This method is
1.1435 + * semantically equivalent to {@code nextAfter(f,
1.1436 + * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
1.1437 + * implementation may run faster than its equivalent
1.1438 + * {@code nextAfter} call.
1.1439 + *
1.1440 + * <p>Special Cases:
1.1441 + * <ul>
1.1442 + * <li> If the argument is NaN, the result is NaN.
1.1443 + *
1.1444 + * <li> If the argument is positive infinity, the result is
1.1445 + * positive infinity.
1.1446 + *
1.1447 + * <li> If the argument is zero, the result is
1.1448 + * {@link Float#MIN_VALUE}
1.1449 + *
1.1450 + * </ul>
1.1451 + *
1.1452 + * @param f starting floating-point value
1.1453 + * @return The adjacent floating-point value closer to positive
1.1454 + * infinity.
1.1455 + * @since 1.6
1.1456 + */
1.1457 + public static float nextUp(float f) {
1.1458 + return sun.misc.FpUtils.nextUp(f);
1.1459 + }
1.1460 +
1.1461 +
1.1462 + /**
1.1463 + * Return {@code d} ×
1.1464 + * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1.1465 + * by a single correctly rounded floating-point multiply to a
1.1466 + * member of the double value set. See the Java
1.1467 + * Language Specification for a discussion of floating-point
1.1468 + * value sets. If the exponent of the result is between {@link
1.1469 + * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
1.1470 + * answer is calculated exactly. If the exponent of the result
1.1471 + * would be larger than {@code Double.MAX_EXPONENT}, an
1.1472 + * infinity is returned. Note that if the result is subnormal,
1.1473 + * precision may be lost; that is, when {@code scalb(x, n)}
1.1474 + * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1.1475 + * <i>x</i>. When the result is non-NaN, the result has the same
1.1476 + * sign as {@code d}.
1.1477 + *
1.1478 + * <p>Special cases:
1.1479 + * <ul>
1.1480 + * <li> If the first argument is NaN, NaN is returned.
1.1481 + * <li> If the first argument is infinite, then an infinity of the
1.1482 + * same sign is returned.
1.1483 + * <li> If the first argument is zero, then a zero of the same
1.1484 + * sign is returned.
1.1485 + * </ul>
1.1486 + *
1.1487 + * @param d number to be scaled by a power of two.
1.1488 + * @param scaleFactor power of 2 used to scale {@code d}
1.1489 + * @return {@code d} × 2<sup>{@code scaleFactor}</sup>
1.1490 + * @since 1.6
1.1491 + */
1.1492 + public static double scalb(double d, int scaleFactor) {
1.1493 + return sun.misc.FpUtils.scalb(d, scaleFactor);
1.1494 + }
1.1495 +
1.1496 + /**
1.1497 + * Return {@code f} ×
1.1498 + * 2<sup>{@code scaleFactor}</sup> rounded as if performed
1.1499 + * by a single correctly rounded floating-point multiply to a
1.1500 + * member of the float value set. See the Java
1.1501 + * Language Specification for a discussion of floating-point
1.1502 + * value sets. If the exponent of the result is between {@link
1.1503 + * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
1.1504 + * answer is calculated exactly. If the exponent of the result
1.1505 + * would be larger than {@code Float.MAX_EXPONENT}, an
1.1506 + * infinity is returned. Note that if the result is subnormal,
1.1507 + * precision may be lost; that is, when {@code scalb(x, n)}
1.1508 + * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
1.1509 + * <i>x</i>. When the result is non-NaN, the result has the same
1.1510 + * sign as {@code f}.
1.1511 + *
1.1512 + * <p>Special cases:
1.1513 + * <ul>
1.1514 + * <li> If the first argument is NaN, NaN is returned.
1.1515 + * <li> If the first argument is infinite, then an infinity of the
1.1516 + * same sign is returned.
1.1517 + * <li> If the first argument is zero, then a zero of the same
1.1518 + * sign is returned.
1.1519 + * </ul>
1.1520 + *
1.1521 + * @param f number to be scaled by a power of two.
1.1522 + * @param scaleFactor power of 2 used to scale {@code f}
1.1523 + * @return {@code f} × 2<sup>{@code scaleFactor}</sup>
1.1524 + * @since 1.6
1.1525 + */
1.1526 + public static float scalb(float f, int scaleFactor) {
1.1527 + return sun.misc.FpUtils.scalb(f, scaleFactor);
1.1528 + }
1.1529 +}