Math Functions
Math Functions
- Empty values — a function of one number, such as
abs,sqrtorcos, returns an empty value for an empty number.pow,atan2,copySign,getExponent(x, y),IEEEremainder,nextAfter(x, y)andscalbreturn an empty value when either argument is empty, andround,quotient,mod,floorDivandremainderreturn one for an empty number.isNaNandisInfinitereturnfalsefor an empty value. A value of a primitive type, such asdouble, is never empty: it is0until it is set. - Number types —
absreturns a number of the type of its argument. The other functions compute inDouble, and the variants forFloatkeep the precision of aFloat.
| Function | Description |
|---|---|
| abs(Integer / Long / Float / Double / BigInteger / BigDecimal a) | Returns the absolute value of a number of the same type. For a BigInteger or a BigDecimal the result is exact. |
| acos(Double a) | Returns the arc cosine of a value. The returned angle is in the range 0.0 through pi. |
| asin(Double a) | Returns the arc sine of a value. The returned angle is in the range -pi/2 through pi/2. |
| atan(Double a) | Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2. |
| atan2(Double y, Double x) | Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). |
| cbrt(Double a) | Returns the cube root of a double value. |
| ceil(Double a) | Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. |
| copySign(Double magnitude, Double sign) / (Float magnitude, Float sign) |
Returns the first floating-point argument with the sign of the second floating-point argument. |
| cos(Double a) | Returns the trigonometric cosine of an angle. |
| cosh(Double x) | Returns the hyperbolic cosine of a double value. |
| exp(Double a) | Returns Euler’s number e raised to the power of a double value. |
| expm1(Double x) | Returns e raised to the power of x, minus 1. |
| floor(Double a) | Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. |
| floorDiv(a, b) | Floor division: $\lfloor a / b \rfloor$ — the largest integer less than or equal to the algebraic quotient. Rounds toward negative infinity. Differs from quotient only when $a$ and $b$ have opposite signs and $a$ is not a multiple of $b$ (for example, $\mathrm{floorDiv}(-10, 3) = -4$ while $\mathrm{quotient}(-10, 3) = -3$). |
| getExponent(Double a) / (Float a) | Returns the unbiased exponent used in the representation of a. |
| getExponent(Double x, Double y) | Returns the square root of x * x + y * y, the length of the hypotenuse, without intermediate overflow or underflow. |
| IEEEremainder(Double f1, Double f2) | Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. |
| isInfinite(number) | Determines whether an input value is infinitely large in magnitude. |
| isNaN(number) | Determines whether an input value is a non-numeric value. |
| log(Double a) | Returns the natural logarithm (base e) of a double value. |
| log10(Double a) | Returns the base 10 logarithm of a double value. |
| log1p(Double x) | Returns the natural logarithm of the sum of the argument and 1. |
| max(value1, value2, …) | Returns the greatest of the values. Also accepts an array. Empty values are skipped, and a NaN value makes the result NaN. |
| min(value1, value2, …) | Returns the smallest of the values. Also accepts an array. Empty values are skipped, and a NaN value makes the result NaN. |
| mod(a, b) | Floor modulo: $a - b \cdot \lfloor a / b \rfloor$. For a finite, non-zero $b$ the result has the same sign as $b$ and satisfies $0 \le \lvert \mathrm{mod}(a, b) \rvert < \lvert b \rvert$. If $b = 0$, returns $a$ (does not throw). |
| nextAfter(Double start, Double direction) / (Float start, Float direction) |
Returns the floating-point number adjacent to the first argument in the direction of the second argument. |
| pow(Double a, Double b) | Returns the value of the first argument raised to the power of the second argument. a ** b is a short form of pow(a, b). |
| pow(BigInteger a, Integer n) / (BigDecimal a, Integer n) |
Returns a raised to the whole power n exactly, so pow((BigDecimal) 1.5, 2) is 2.25. A negative n gives 1 divided by the power for a BigDecimal, rounded to 34 significant digits, and an error for a BigInteger. a ** n is a short form of pow(a, n). |
| quotient(a, b) | Truncated quotient: the integer part of $a / b$, rounding toward zero — i.e., $\lfloor a / b \rfloor$ when $a / b \ge 0$ and $\lceil a / b \rceil$ when $a / b < 0$. Differs from floorDiv only when $a$ and $b$ have opposite signs and $a$ is not a multiple of $b$. |
| random() | Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. |
| remainder(a, b) | Truncated remainder: $a - b \cdot \mathrm{quotient}(a, b)$. For a finite, non-zero $b$ the result has the same sign as the dividend $a$ and satisfies $0 \le \lvert \mathrm{remainder}(a, b) \rvert < \lvert b \rvert$. Pairs with quotient via the division identity $a = b \cdot \mathrm{quotient}(a, b) + \mathrm{remainder}(a, b)$. Not to be confused with IEEEremainder, which rounds the quotient to the nearest integer instead of truncating. a % b is a short form of remainder(a, b). |
| rint(Double a) | Returns the double value that is closest in value to the argument and is equal to a mathematical integer. |
| round(value) | Returns the closest whole number to the argument, with ties rounding away from zero, as an Integer, or a BigInteger for a BigDecimal. A value beyond the Integer range gives 2147483647 or -2147483648. |
| round(value, int scale, int roundingMethod) | Returns a number which scale is the specified value, and which unscaled value is determined by multiplying or dividing this number’s unscaled value by the appropriate power of ten to maintain its overall value. A whole number gives a Double. An empty scale or roundingMethod counts as 0. |
| roundStrict(double value) | Returns the closest value to the argument without adding ulp. |
| scalb(Double a, Integer scaleFactor) / (Float a, Integer scaleFactor) |
Returns a multiplied by 2 raised to the power of scaleFactor, rounded as if performed by a single correctly rounded floating-point multiply. |
| signum(Double d) | Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero. |
| sin(Double a) | Returns the trigonometric sine of an angle. |
| sinh(Double x) | Returns the hyperbolic sine of a double value. |
| sqrt(Double a) | Returns the correctly rounded positive square root of a double value. |
| tan(Double a) | Returns the trigonometric tangent of an angle. |
| tanh(Double x) | Returns the hyperbolic tangent of a double value. |
| toDegrees(Double angrad) | Converts an angle measured in radians to an approximately equivalent angle measured in degrees. |
| toRadians(Double angdeg) | Converts an angle measured in degrees to an approximately equivalent angle measured in radians. |
| ulp(Double value) / (Float value) | Returns the size of an ulp of the argument. |