Unlike some of the numeric methods of class
StrictMath, all implementations of the equivalent
functions of class Math are not defined to return the
bit-for-bit same results. This relaxation permits
better-performing implementations where strict reproducibility is
not required.

By default many of the Math methods simply call
the equivalent method in StrictMath for their
implementation. Code generators are encouraged to use
platform-specific native libraries or microprocessor instructions,
where available, to provide higher-performance implementations of
Math methods. Such higher-performance
implementations still must conform to the specification for
Math.

The quality of implementation specifications concern two
properties, accuracy of the returned result and monotonicity of the
method. Accuracy of the floating-point Math methods is
measured in terms of ulps, units in the last place. For a
given floating-point format, an ulp of a
specific real number value is the distance between the two
floating-point values bracketing that numerical value. When
discussing the accuracy of a method as a whole rather than at a
specific argument, the number of ulps cited is for the worst-case
error at any argument. If a method always has an error less than
0.5 ulps, the method always returns the floating-point number
nearest the exact result; such a method is correctly
rounded. A correctly rounded method is generally the best a
floating-point approximation can be; however, it is impractical for
many floating-point methods to be correctly rounded. Instead, for
the Math class, a larger error bound of 1 or 2 ulps is
allowed for certain methods. Informally, with a 1 ulp error bound,
when the exact result is a representable number, the exact result
should be returned as the computed result; otherwise, either of the
two floating-point values which bracket the exact result may be
returned. For exact results large in magnitude, one of the
endpoints of the bracket may be infinite. Besides accuracy at
individual arguments, maintaining proper relations between the
method at different arguments is also important. Therefore, most
methods with more than 0.5 ulp errors are required to be
semi-monotonic: whenever the mathematical function is
non-decreasing, so is the floating-point approximation, likewise,
whenever the mathematical function is non-increasing, so is the
floating-point approximation. Not all approximations that have 1
ulp accuracy will automatically meet the monotonicity requirements.

The platform uses signed two's complement integer arithmetic with
int and long primitive types. The developer should choose
the primitive type to ensure that arithmetic operations consistently
produce correct results, which in some cases means the operations
will not overflow the range of values of the computation.
The best practice is to choose the primitive type and algorithm to avoid
overflow. In cases where the size is int or long and
overflow errors need to be detected, the methods addExact,
subtractExact, multiplyExact, and toIntExact
throw an ArithmeticException when the results overflow.
For other arithmetic operations such as divide, absolute value,
increment, decrement, and negation overflow occurs only with
a specific minimum or maximum value and should be checked against
the minimum or maximum as appropriate.

Causes the current thread to wait until another thread invokes the
notify() method or the
notifyAll() method for this object, or
some other thread interrupts the current thread, or a certain
amount of real time has elapsed.

Public methods

IEEEremainder

Computes the remainder operation on two arguments as prescribed
by the IEEE 754 standard.
The remainder value is mathematically equal to
f1 - f2 × n,
where n is the mathematical integer closest to the exact
mathematical value of the quotient f1/f2, and if two
mathematical integers are equally close to f1/f2,
then n is the integer that is even. If the remainder is
zero, its sign is the same as the sign of the first argument.
Special cases:

If either argument is NaN, or the first argument is infinite,
or the second argument is positive zero or negative zero, then the
result is NaN.

If the first argument is finite and the second argument is
infinite, then the result is the same as the first argument.

atan2

Returns the angle theta from the conversion of rectangular
coordinates (x, y) to polar
coordinates (r, theta).
This method computes the phase theta by computing an arc tangent
of y/x in the range of -pi to pi. Special
cases:

If either argument is NaN, then the result is NaN.

If the first argument is positive zero and the second argument
is positive, or the first argument is positive and finite and the
second argument is positive infinity, then the result is positive
zero.

If the first argument is negative zero and the second argument
is positive, or the first argument is negative and finite and the
second argument is positive infinity, then the result is negative zero.

If the first argument is positive zero and the second argument
is negative, or the first argument is positive and finite and the
second argument is negative infinity, then the result is the
double value closest to pi.

If the first argument is negative zero and the second argument
is negative, or the first argument is negative and finite and the
second argument is negative infinity, then the result is the
double value closest to -pi.

If the first argument is positive and the second argument is
positive zero or negative zero, or the first argument is positive
infinity and the second argument is finite, then the result is the
double value closest to pi/2.

If the first argument is negative and the second argument is
positive zero or negative zero, or the first argument is negative
infinity and the second argument is finite, then the result is the
double value closest to -pi/2.

If both arguments are positive infinity, then the result is the
double value closest to pi/4.

If the first argument is positive infinity and the second argument
is negative infinity, then the result is the double
value closest to 3*pi/4.

If the first argument is negative infinity and the second argument
is positive infinity, then the result is the double value
closest to -pi/4.

If both arguments are negative infinity, then the result is the
double value closest to -3*pi/4.

The computed result must be within 2 ulps of the exact result.
Results must be semi-monotonic.

Parameters

y

double: the ordinate coordinate

x

double: the abscissa coordinate

Returns

double

the theta component of the point
(r, theta)
in polar coordinates that corresponds to the point
(x, y) in Cartesian coordinates.

cbrt

Returns the cube root of a double value. For
positive finite x, cbrt(-x) ==
-cbrt(x); that is, the cube root of a negative value is
the negative of the cube root of that value's magnitude.
Special cases:

If the argument is NaN, then the result is NaN.

If the argument is infinite, then the result is an infinity
with the same sign as the argument.

If the argument is zero, then the result is a zero with the
same sign as the argument.

copySign

Returns the first floating-point argument with the sign of the
second floating-point argument. Note that unlike the StrictMath.copySign
method, this method does not require NaN sign
arguments to be treated as positive values; implementations are
permitted to treat some NaN arguments as positive and other NaN
arguments as negative to allow greater performance.

Parameters

magnitude

float: the parameter providing the magnitude of the result

sign

float: the parameter providing the sign of the result

Returns

float

a value with the magnitude of magnitude
and the sign of sign.

copySign

Returns the first floating-point argument with the sign of the
second floating-point argument. Note that unlike the StrictMath.copySign
method, this method does not require NaN sign
arguments to be treated as positive values; implementations are
permitted to treat some NaN arguments as positive and other NaN
arguments as negative to allow greater performance.

expm1

Returns ex -1. Note that for values of
x near 0, the exact sum of
expm1(x) + 1 is much closer to the true
result of ex than exp(x).

Special cases:

If the argument is NaN, the result is NaN.

If the argument is positive infinity, then the result is
positive infinity.

If the argument is negative infinity, then the result is
-1.0.

If the argument is zero, then the result is a zero with the
same sign as the argument.

The computed result must be within 1 ulp of the exact result.
Results must be semi-monotonic. The result of
expm1 for any finite input must be greater than or
equal to -1.0. Note that once the exact result of
ex - 1 is within 1/2
ulp of the limit value -1, -1.0 should be
returned.

floorDiv

Returns the largest (closest to positive infinity)
int value that is less than or equal to the algebraic quotient.
There is one special case, if the dividend is the
Integer.MIN_VALUE and the divisor is -1,
then integer overflow occurs and
the result is equal to the Integer.MIN_VALUE.

Normal integer division operates under the round to zero rounding mode
(truncation). This operation instead acts under the round toward
negative infinity (floor) rounding mode.
The floor rounding mode gives different results than truncation
when the exact result is negative.

If the signs of the arguments are the same, the results of
floorDiv and the / operator are the same.
For example, floorDiv(4, 3) == 1 and (4 / 3) == 1.

If the signs of the arguments are different, the quotient is negative and
floorDiv returns the integer less than or equal to the quotient
and the / operator returns the integer closest to zero.
For example, floorDiv(-4, 3) == -2,
whereas (-4 / 3) == -1.

Parameters

x

int: the dividend

y

int: the divisor

Returns

int

the largest (closest to positive infinity)
int value that is less than or equal to the algebraic quotient.

floorDiv

Returns the largest (closest to positive infinity)
long value that is less than or equal to the algebraic quotient.
There is one special case, if the dividend is the
Long.MIN_VALUE and the divisor is -1,
then integer overflow occurs and
the result is equal to the Long.MIN_VALUE.

Normal integer division operates under the round to zero rounding mode
(truncation). This operation instead acts under the round toward
negative infinity (floor) rounding mode.
The floor rounding mode gives different results than truncation
when the exact result is negative.

floorMod

The floor modulus is x - (floorDiv(x, y) * y),
has the same sign as the divisor y, and
is in the range of -abs(y) < r < +abs(y).

The relationship between floorDiv and floorMod is such that:

floorDiv(x, y) * y + floorMod(x, y) == x

The difference in values between floorMod and
the % operator is due to the difference between
floorDiv that returns the integer less than or equal to the quotient
and the / operator that returns the integer closest to zero.

Examples:

If the signs of the arguments are the same, the results
of floorMod and the % operator are the same.

floorMod(4, 3) == 1; and (4 % 3) == 1

If the signs of the arguments are different, the results differ from the % operator.

floorMod(+4, -3) == -2; and (+4 % -3) == +1

floorMod(-4, +3) == +2; and (-4 % +3) == -1

floorMod(-4, -3) == -1; and (-4 % -3) == -1

If the signs of arguments are unknown and a positive modulus
is needed it can be computed as (floorMod(x, y) + abs(y)) % abs(y).

log1p

Returns the natural logarithm of the sum of the argument and 1.
Note that for small values x, the result of
log1p(x) is much closer to the true result of ln(1
+ x) than the floating-point evaluation of
log(1.0+x).

Special cases:

If the argument is NaN or less than -1, then the result is
NaN.

If the argument is positive infinity, then the result is
positive infinity.

If the argument is negative one, then the result is
negative infinity.

If the argument is zero, then the result is a zero with the
same sign as the argument.

The computed result must be within 1 ulp of the exact result.
Results must be semi-monotonic.

max

Returns the greater of two float values. That is,
the result is the argument closer to positive infinity. If the
arguments have the same value, the result is that same
value. If either value is NaN, then the result is NaN. Unlike
the numerical comparison operators, this method considers
negative zero to be strictly smaller than positive zero. If one
argument is positive zero and the other negative zero, the
result is positive zero.

Parameters

a

float: an argument.

b

float: another argument.

Returns

float

the larger of a and b.

max

Returns the greater of two double values. That
is, the result is the argument closer to positive infinity. If
the arguments have the same value, the result is that same
value. If either value is NaN, then the result is NaN. Unlike
the numerical comparison operators, this method considers
negative zero to be strictly smaller than positive zero. If one
argument is positive zero and the other negative zero, the
result is positive zero.

Parameters

a

double: an argument.

b

double: another argument.

Returns

double

the larger of a and b.

min

Returns the smaller of two float values. That is,
the result is the value closer to negative infinity. If the
arguments have the same value, the result is that same
value. If either value is NaN, then the result is NaN. Unlike
the numerical comparison operators, this method considers
negative zero to be strictly smaller than positive zero. If
one argument is positive zero and the other is negative zero,
the result is negative zero.

Parameters

a

float: an argument.

b

float: another argument.

Returns

float

the smaller of a and b.

min

Returns the smaller of two double values. That
is, the result is the value closer to negative infinity. If the
arguments have the same value, the result is that same
value. If either value is NaN, then the result is NaN. Unlike
the numerical comparison operators, this method considers
negative zero to be strictly smaller than positive zero. If one
argument is positive zero and the other is negative zero, the
result is negative zero.

nextDown

Returns the floating-point value adjacent to d in
the direction of negative infinity. This method is
semantically equivalent to nextAfter(d,
Double.NEGATIVE_INFINITY); however, a
nextDown implementation may run faster than its
equivalent nextAfter call.

Special Cases:

If the argument is NaN, the result is NaN.

If the argument is negative infinity, the result is
negative infinity.

If the argument is zero, the result is
-Double.MIN_VALUE

Parameters

d

double: starting floating-point value

Returns

double

The adjacent floating-point value closer to negative
infinity.

nextDown

Returns the floating-point value adjacent to f in
the direction of negative infinity. This method is
semantically equivalent to nextAfter(f,
Float.NEGATIVE_INFINITY); however, a
nextDown implementation may run faster than its
equivalent nextAfter call.

Special Cases:

If the argument is NaN, the result is NaN.

If the argument is negative infinity, the result is
negative infinity.

If the argument is zero, the result is
-Float.MIN_VALUE

Parameters

f

float: starting floating-point value

Returns

float

The adjacent floating-point value closer to negative
infinity.

nextUp

Returns the floating-point value adjacent to f in
the direction of positive infinity. This method is
semantically equivalent to nextAfter(f,
Float.POSITIVE_INFINITY); however, a nextUp
implementation may run faster than its equivalent
nextAfter call.

Special Cases:

If the argument is NaN, the result is NaN.

If the argument is positive infinity, the result is
positive infinity.

nextUp

Returns the floating-point value adjacent to d in
the direction of positive infinity. This method is
semantically equivalent to nextAfter(d,
Double.POSITIVE_INFINITY); however, a nextUp
implementation may run faster than its equivalent
nextAfter call.

Special Cases:

If the argument is NaN, the result is NaN.

If the argument is positive infinity, the result is
positive infinity.

pow

Returns the value of the first argument raised to the power of the
second argument. Special cases:

If the second argument is positive or negative zero, then the
result is 1.0.

If the second argument is 1.0, then the result is the same as the
first argument.

If the second argument is NaN, then the result is NaN.

If the first argument is NaN and the second argument is nonzero,
then the result is NaN.

If

the absolute value of the first argument is greater than 1
and the second argument is positive infinity, or

the absolute value of the first argument is less than 1 and
the second argument is negative infinity,

then the result is positive infinity.

If

the absolute value of the first argument is greater than 1 and
the second argument is negative infinity, or

the absolute value of the
first argument is less than 1 and the second argument is positive
infinity,

then the result is positive zero.

If the absolute value of the first argument equals 1 and the
second argument is infinite, then the result is NaN.

If

the first argument is positive zero and the second argument
is greater than zero, or

the first argument is positive infinity and the second
argument is less than zero,

then the result is positive zero.

If

the first argument is positive zero and the second argument
is less than zero, or

the first argument is positive infinity and the second
argument is greater than zero,

then the result is positive infinity.

If

the first argument is negative zero and the second argument
is greater than zero but not a finite odd integer, or

the first argument is negative infinity and the second
argument is less than zero but not a finite odd integer,

then the result is positive zero.

If

the first argument is negative zero and the second argument
is a positive finite odd integer, or

the first argument is negative infinity and the second
argument is a negative finite odd integer,

then the result is negative zero.

If

the first argument is negative zero and the second argument
is less than zero but not a finite odd integer, or

the first argument is negative infinity and the second
argument is greater than zero but not a finite odd integer,

then the result is positive infinity.

If

the first argument is negative zero and the second argument
is a negative finite odd integer, or

the first argument is negative infinity and the second
argument is a positive finite odd integer,

then the result is negative infinity.

If the first argument is finite and less than zero

if the second argument is a finite even integer, the
result is equal to the result of raising the absolute value of
the first argument to the power of the second argument

if the second argument is a finite odd integer, the result
is equal to the negative of the result of raising the absolute
value of the first argument to the power of the second
argument

if the second argument is finite and not an integer, then
the result is NaN.

If both arguments are integers, then the result is exactly equal
to the mathematical result of raising the first argument to the power
of the second argument if that result can in fact be represented
exactly as a double value.

(In the foregoing descriptions, a floating-point value is
considered to be an integer if and only if it is finite and a
fixed point of the method ceil or,
equivalently, a fixed point of the method floor. A value is a fixed point of a one-argument
method if and only if the result of applying the method to the
value is equal to the value.)

The computed result must be within 1 ulp of the exact result.
Results must be semi-monotonic.

random

Returns a double value with a positive sign, greater
than or equal to 0.0 and less than 1.0.
Returned values are chosen pseudorandomly with (approximately)
uniform distribution from that range.

When this method is first called, it creates a single new
pseudorandom-number generator, exactly as if by the expression

new java.util.Random()

This new pseudorandom-number generator is used thereafter for
all calls to this method and is used nowhere else.

This method is properly synchronized to allow correct use by
more than one thread. However, if many threads need to generate
pseudorandom numbers at a great rate, it may reduce contention
for each thread to have its own pseudorandom-number generator.

rint

Returns the double value that is closest in value
to the argument and is equal to a mathematical integer. If two
double values that are mathematical integers are
equally close, the result is the integer value that is
even. Special cases:

If the argument value is already equal to a mathematical
integer, then the result is the same as the argument.

If the argument is NaN or an infinity or positive zero or negative
zero, then the result is the same as the argument.

Parameters

a

double: a double value.

Returns

double

the closest floating-point value to a that is
equal to a mathematical integer.

scalb

Returns f ×
2scaleFactor rounded as if performed
by a single correctly rounded floating-point multiply to a
member of the float value set. See the Java
Language Specification for a discussion of floating-point
value sets. If the exponent of the result is between Float.MIN_EXPONENT and Float.MAX_EXPONENT, the
answer is calculated exactly. If the exponent of the result
would be larger than Float.MAX_EXPONENT, an
infinity is returned. Note that if the result is subnormal,
precision may be lost; that is, when scalb(x, n)
is subnormal, scalb(scalb(x, n), -n) may not equal
x. When the result is non-NaN, the result has the same
sign as f.

Special cases:

If the first argument is NaN, NaN is returned.

If the first argument is infinite, then an infinity of the
same sign is returned.

If the first argument is zero, then a zero of the same
sign is returned.

Parameters

f

float: number to be scaled by a power of two.

scaleFactor

int: power of 2 used to scale f

Returns

float

f × 2scaleFactor

scalb

Returns d ×
2scaleFactor rounded as if performed
by a single correctly rounded floating-point multiply to a
member of the double value set. See the Java
Language Specification for a discussion of floating-point
value sets. If the exponent of the result is between Double.MIN_EXPONENT and Double.MAX_EXPONENT, the
answer is calculated exactly. If the exponent of the result
would be larger than Double.MAX_EXPONENT, an
infinity is returned. Note that if the result is subnormal,
precision may be lost; that is, when scalb(x, n)
is subnormal, scalb(scalb(x, n), -n) may not equal
x. When the result is non-NaN, the result has the same
sign as d.

Special cases:

If the first argument is NaN, NaN is returned.

If the first argument is infinite, then an infinity of the
same sign is returned.

If the first argument is zero, then a zero of the same
sign is returned.

tanh

Returns the hyperbolic tangent of a double value.
The hyperbolic tangent of x is defined to be
(ex - e-x)/(ex + e-x),
in other words, sinh(x)/cosh(x). Note
that the absolute value of the exact tanh is always less than
1.

Special cases:

If the argument is NaN, then the result is NaN.

If the argument is zero, then the result is a zero with the
same sign as the argument.

If the argument is positive infinity, then the result is
+1.0.

If the argument is negative infinity, then the result is
-1.0.

The computed result must be within 2.5 ulps of the exact result.
The result of tanh for any finite input must have
an absolute value less than or equal to 1. Note that once the
exact result of tanh is within 1/2 of an ulp of the limit value
of ±1, correctly signed ±1.0 should
be returned.

Parameters

x

double: The number whose hyperbolic tangent is to be returned.

Returns

double

The hyperbolic tangent of x.

toDegrees

Converts an angle measured in radians to an approximately
equivalent angle measured in degrees. The conversion from
radians to degrees is generally inexact; users should
not expect cos(toRadians(90.0)) to exactly
equal 0.0.

ulp

Returns the size of an ulp of the argument. An ulp, unit in
the last place, of a double value is the positive
distance between this floating-point value and the double value next larger in magnitude. Note that for non-NaN
x, ulp(-x) == ulp(x).

Special Cases:

If the argument is NaN, then the result is NaN.

If the argument is positive or negative infinity, then the
result is positive infinity.

If the argument is positive or negative zero, then the result is
Double.MIN_VALUE.

If the argument is ±Double.MAX_VALUE, then
the result is equal to 2971.

Parameters

d

double: the floating-point value whose ulp is to be returned

Returns

double

the size of an ulp of the argument

ulp

Returns the size of an ulp of the argument. An ulp, unit in
the last place, of a float value is the positive
distance between this floating-point value and the float value next larger in magnitude. Note that for non-NaN
x, ulp(-x) == ulp(x).

Special Cases:

If the argument is NaN, then the result is NaN.

If the argument is positive or negative infinity, then the
result is positive infinity.

If the argument is positive or negative zero, then the result is
Float.MIN_VALUE.

If the argument is ±Float.MAX_VALUE, then
the result is equal to 2104.