Top Description Inners Fields Constructors Methods
java.lang

public final Class Math

extends Object
Class Inheritance
Imports
java.math.BigDecimal, java.util.Random, jdk.internal.math.FloatConsts, .DoubleConsts, jdk.internal.vm.annotation.IntrinsicCandidate

The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

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 whose names end with Exact throw an ArithmeticException when the results overflow.

IEEE 754 Recommended Operations

The 2019 revision of the IEEE 754 floating-point standard includes a section of recommended operations and the semantics of those operations if they are included in a programming environment. The recommended operations present in this class include sin, cos, tan, asin, acos, atan, exp, expm1, log, log10, log1p, sinh, cosh, tanh, hypot, and pow. (The sqrt operation is a required part of IEEE 754 from a different section of the standard.) The special case behavior of the recommended operations generally follows the guidance of the IEEE 754 standard. However, the pow method defines different behavior for some arguments, as noted in its specification. The IEEE 754 standard defines its operations to be correctly rounded, which is a more stringent quality of implementation condition than required for most of the methods in question that are also included in this class.
Since
1.0
See Also
IEEE Standard for Floating-Point Arithmetic

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final double
DEGREES_TO_RADIANS

Constant by which to multiply an angular value in degrees to obtain an angular value in radians.

public static final double
E

The double value that is closer than any other to e, the base of the natural logarithms.

private static final long
private static final long
public static final double
PI

The double value that is closer than any other to pi (π), the ratio of the circumference of a circle to its diameter.

private static final double
RADIANS_TO_DEGREES

Constant by which to multiply an angular value in radians to obtain an angular value in degrees.

public static final double
TAU

The double value that is closer than any other to tau (τ), the ratio of the circumference of a circle to its radius.

Constructor Summary

AccessConstructor and Description
private
Math()

Don't let anyone instantiate this class.

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

the absolute value of the argument.
abs
(int
the argument whose absolute value is to be determined
a
)

Returns the absolute value of an int value.

public static long

Returns:

the absolute value of the argument.
abs
(long
the argument whose absolute value is to be determined
a
)

Returns the absolute value of a long value.

public static float

Returns:

the absolute value of the argument.
abs
(float
the argument whose absolute value is to be determined
a
)

Returns the absolute value of a float value.

public static double

Returns:

the absolute value of the argument.
abs
(double
the argument whose absolute value is to be determined
a
)

Returns the absolute value of a double value.

public static int

Returns:

the absolute value of the argument, unless overflow occurs
absExact
(int
the argument whose absolute value is to be determined
a
)

Returns the mathematical absolute value of an int value if it is exactly representable as an int, throwing ArithmeticException if the result overflows the positive int range.

public static long

Returns:

the absolute value of the argument, unless overflow occurs
absExact
(long
the argument whose absolute value is to be determined
a
)

Returns the mathematical absolute value of an long value if it is exactly representable as an long, throwing ArithmeticException if the result overflows the positive long range.

public static double

Returns:

the arc cosine of the argument.
acos
(double
the value whose arc cosine is to be returned.
a
)

Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.

public static int

Returns:

the result
addExact
(int
the first value
x
,
int
the second value
y
)

Returns the sum of its arguments, throwing an exception if the result overflows an int.

public static long

Returns:

the result
addExact
(long
the first value
x
,
long
the second value
y
)

Returns the sum of its arguments, throwing an exception if the result overflows a long.

public static double

Returns:

the arc sine of the argument.
asin
(double
the value whose arc sine is to be returned.
a
)

Returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2.

public static double

Returns:

the arc tangent of the argument.
atan
(double
the value whose arc tangent is to be returned.
a
)

Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2.

public static double

Returns:

the theta component of the point (rtheta) in polar coordinates that corresponds to the point (xy) in Cartesian coordinates.
atan2
(double
the ordinate coordinate
y
,
double
the abscissa coordinate
x
)

Returns the angle theta from the conversion of rectangular coordinates (xy) to polar coordinates (r, theta).

public static double

Returns:

the cube root of a.
cbrt
(double
a value.
a
)

Returns the cube root of a double value.

public static double

Returns:

the smallest (closest to negative infinity) floating-point value that is greater than or equal to the argument and is equal to a mathematical integer.
ceil
(double
a value.
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.

public static int

Returns:

the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.
ceilDiv
(int
the dividend
x
,
int
the divisor
y
)

Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.

public static long

Returns:

the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.
ceilDiv
(long
the dividend
x
,
int
the divisor
y
)

Returns the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.

public static long

Returns:

the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.
ceilDiv
(long
the dividend
x
,
long
the divisor
y
)

Returns the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.

public static int

Returns:

the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.
ceilDivExact
(int
the dividend
x
,
int
the divisor
y
)

Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.

public static long

Returns:

the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.
ceilDivExact
(long
the dividend
x
,
long
the divisor
y
)

Returns the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.

public static int

Returns:

the ceiling modulus x - (ceilDiv(x, y) * y)
ceilMod
(int
the dividend
x
,
int
the divisor
y
)

Returns the ceiling modulus of the int arguments.

public static int

Returns:

the ceiling modulus x - (ceilDiv(x, y) * y)
ceilMod
(long
the dividend
x
,
int
the divisor
y
)

Returns the ceiling modulus of the long and int arguments.

public static long

Returns:

the ceiling modulus x - (ceilDiv(x, y) * y)
ceilMod
(long
the dividend
x
,
long
the divisor
y
)

Returns the ceiling modulus of the long arguments.

public static int

Returns:

a clamped value that fits into min..max interval
clamp
(long
value to clamp
value
,
int
minimal allowed value
min
,
int
maximal allowed value
max
)

Clamps the value to fit between min and max.

public static long

Returns:

a clamped value that fits into min..max interval
clamp
(long
value to clamp
value
,
long
minimal allowed value
min
,
long
maximal allowed value
max
)

Clamps the value to fit between min and max.

public static double

Returns:

a clamped value that fits into min..max interval
clamp
(double
value to clamp
value
,
double
minimal allowed value
min
,
double
maximal allowed value
max
)

Clamps the value to fit between min and max.

public static float

Returns:

a clamped value that fits into min..max interval
clamp
(float
value to clamp
value
,
float
minimal allowed value
min
,
float
maximal allowed value
max
)

Clamps the value to fit between min and max.

public static double

Returns:

a value with the magnitude of magnitude and the sign of sign.
copySign
(double
the parameter providing the magnitude of the result
magnitude
,
double
the parameter providing the sign of the result
sign
)

Returns the first floating-point argument with the sign of the second floating-point argument.

public static float

Returns:

a value with the magnitude of magnitude and the sign of sign.
copySign
(float
the parameter providing the magnitude of the result
magnitude
,
float
the parameter providing the sign of the result
sign
)

Returns the first floating-point argument with the sign of the second floating-point argument.

public static double

Returns:

the cosine of the argument.
cos
(double
an angle, in radians.
a
)

Returns the trigonometric cosine of an angle.

public static double

Returns:

The hyperbolic cosine of x.
cosh
(double
The number whose hyperbolic cosine is to be returned.
x
)

Returns the hyperbolic cosine of a double value.

public static int

Returns:

the result
decrementExact
(int
the value to decrement
a
)

Returns the argument decremented by one, throwing an exception if the result overflows an int.

public static long

Returns:

the result
decrementExact
(long
the value to decrement
a
)

Returns the argument decremented by one, throwing an exception if the result overflows a long.

public static int

Returns:

the quotient x / y
divideExact
(int
the dividend
x
,
int
the divisor
y
)

Returns the quotient of the arguments, throwing an exception if the result overflows an int.

public static long

Returns:

the quotient x / y
divideExact
(long
the dividend
x
,
long
the divisor
y
)

Returns the quotient of the arguments, throwing an exception if the result overflows a long.

public static double

Returns:

the value ea, where e is the base of the natural logarithms.
exp
(double
the exponent to raise e to.
a
)

Returns Euler's number e raised to the power of a double value.

public static double

Returns:

the value ex - 1.
expm1
(double
the exponent to raise e to in the computation of ex -1.
x
)

Returns ex -1.

public static double

Returns:

the largest (closest to positive infinity) floating-point value that less than or equal to the argument and is equal to a mathematical integer.
floor
(double
a value.
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.

public static int

Returns:

the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.
floorDiv
(int
the dividend
x
,
int
the divisor
y
)

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

public static long

Returns:

the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.
floorDiv
(long
the dividend
x
,
int
the divisor
y
)

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

public static long

Returns:

the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.
floorDiv
(long
the dividend
x
,
long
the divisor
y
)

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

public static int

Returns:

the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.
floorDivExact
(int
the dividend
x
,
int
the divisor
y
)

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

public static long

Returns:

the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.
floorDivExact
(long
the dividend
x
,
long
the divisor
y
)

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

public static int

Returns:

the floor modulus x - (floorDiv(x, y) * y)
floorMod
(int
the dividend
x
,
int
the divisor
y
)

Returns the floor modulus of the int arguments.

public static int

Returns:

the floor modulus x - (floorDiv(x, y) * y)
floorMod
(long
the dividend
x
,
int
the divisor
y
)

Returns the floor modulus of the long and int arguments.

public static long

Returns:

the floor modulus x - (floorDiv(x, y) * y)
floorMod
(long
the dividend
x
,
long
the divisor
y
)

Returns the floor modulus of the long arguments.

public static double

Returns:

(a × b + c) computed, as if with unlimited range and precision, and rounded once to the nearest double value
fma
(double
a value
a
,
double
a value
b
,
double
a value
c
)

Returns the fused multiply add of the three arguments; that is, returns the exact product of the first two arguments summed with the third argument and then rounded once to the nearest double.

public static float

Returns:

(a × b + c) computed, as if with unlimited range and precision, and rounded once to the nearest float value
fma
(float
a value
a
,
float
a value
b
,
float
a value
c
)

Returns the fused multiply add of the three arguments; that is, returns the exact product of the first two arguments summed with the third argument and then rounded once to the nearest float.

public static int

Returns:

the unbiased exponent of the argument
getExponent
(float
a float value
f
)

Returns the unbiased exponent used in the representation of a float.

public static int

Returns:

the unbiased exponent of the argument
getExponent
(double
a double value
d
)

Returns the unbiased exponent used in the representation of a double.

public static double

Returns:

sqrt(x2 +y2) without intermediate overflow or underflow
hypot
(double
a value
x
,
double
a value
y
)

Returns sqrt(x2 +y2) without intermediate overflow or underflow.

public static double

Returns:

the remainder when f1 is divided by f2.
IEEEremainder
(double
the dividend.
f1
,
double
the divisor.
f2
)

Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.

public static int

Returns:

the result
incrementExact
(int
the value to increment
a
)

Returns the argument incremented by one, throwing an exception if the result overflows an int.

public static long

Returns:

the result
incrementExact
(long
the value to increment
a
)

Returns the argument incremented by one, throwing an exception if the result overflows a long.

public static double

Returns:

the value ln a, the natural logarithm of a.
log
(double
a value
a
)

Returns the natural logarithm (base e) of a double value.

public static double

Returns:

the base 10 logarithm of a.
log10
(double
a value
a
)

Returns the base 10 logarithm of a double value.

public static double

Returns:

the value ln(x + 1), the natural log of x + 1
log1p
(double
a value
x
)

Returns the natural logarithm of the sum of the argument and 1.

public static int

Returns:

the larger of a and b.
max
(int
an argument.
a
,
int
another argument.
b
)

Returns the greater of two int values.

public static long

Returns:

the larger of a and b.
max
(long
an argument.
a
,
long
another argument.
b
)

Returns the greater of two long values.

public static float

Returns:

the larger of a and b.
max
(float
an argument.
a
,
float
another argument.
b
)

Returns the greater of two float values.

public static double

Returns:

the larger of a and b.
max
(double
an argument.
a
,
double
another argument.
b
)

Returns the greater of two double values.

public static int

Returns:

the smaller of a and b.
min
(int
an argument.
a
,
int
another argument.
b
)

Returns the smaller of two int values.

public static long

Returns:

the smaller of a and b.
min
(long
an argument.
a
,
long
another argument.
b
)

Returns the smaller of two long values.

public static float

Returns:

the smaller of a and b.
min
(float
an argument.
a
,
float
another argument.
b
)

Returns the smaller of two float values.

public static double

Returns:

the smaller of a and b.
min
(double
an argument.
a
,
double
another argument.
b
)

Returns the smaller of two double values.

public static int

Returns:

the result
multiplyExact
(int
the first value
x
,
int
the second value
y
)

Returns the product of the arguments, throwing an exception if the result overflows an int.

public static long

Returns:

the result
multiplyExact
(long
the first value
x
,
int
the second value
y
)

Returns the product of the arguments, throwing an exception if the result overflows a long.

public static long

Returns:

the result
multiplyExact
(long
the first value
x
,
long
the second value
y
)

Returns the product of the arguments, throwing an exception if the result overflows a long.

public static long

Returns:

the result
multiplyFull
(int
the first value
x
,
int
the second value
y
)

Returns the exact mathematical product of the arguments.

public static long

Returns:

the result
multiplyHigh
(long
the first value
x
,
long
the second value
y
)

Returns as a long the most significant 64 bits of the 128-bit product of two 64-bit factors.

public static int

Returns:

the result
negateExact
(int
the value to negate
a
)

Returns the negation of the argument, throwing an exception if the result overflows an int.

public static long

Returns:

the result
negateExact
(long
the value to negate
a
)

Returns the negation of the argument, throwing an exception if the result overflows a long.

public static double

Returns:

The floating-point number adjacent to start in the direction of direction.
nextAfter
(double
starting floating-point value
start
,
double
value indicating which of start's neighbors or start should be returned
direction
)

Returns the floating-point number adjacent to the first argument in the direction of the second argument.

public static float

Returns:

The floating-point number adjacent to start in the direction of direction.
nextAfter
(float
starting floating-point value
start
,
double
value indicating which of start's neighbors or start should be returned
direction
)

Returns the floating-point number adjacent to the first argument in the direction of the second argument.

public static double

Returns:

The adjacent floating-point value closer to negative infinity.
nextDown
(double
starting floating-point value
d
)

Returns the floating-point value adjacent to d in the direction of negative infinity.

public static float

Returns:

The adjacent floating-point value closer to negative infinity.
nextDown
(float
starting floating-point value
f
)

Returns the floating-point value adjacent to f in the direction of negative infinity.

public static double

Returns:

The adjacent floating-point value closer to positive infinity.
nextUp
(double
starting floating-point value
d
)

Returns the floating-point value adjacent to d in the direction of positive infinity.

public static float

Returns:

The adjacent floating-point value closer to positive infinity.
nextUp
(float
starting floating-point value
f
)

Returns the floating-point value adjacent to f in the direction of positive infinity.

public static double

Returns:

the value ab.
pow
(double
the base.
a
,
double
the exponent.
b
)

Returns the value of the first argument raised to the power of the second argument.

pack-priv static double
powerOfTwoD(int n)

Returns a floating-point power of two in the normal range.

pack-priv static float
powerOfTwoF(int n)

Returns a floating-point power of two in the normal range.

public static double

Returns:

a pseudorandom double greater than or equal to 0.0 and less than 1.0.
random
()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

public static double

Returns:

the closest floating-point value to a that is equal to a mathematical integer.
rint
(double
a double value.
a
)

Returns the double value that is closest in value to the argument and is equal to a mathematical integer.

public static int

Returns:

the value of the argument rounded to the nearest int value.
round
(float
a floating-point value to be rounded to an integer.
a
)

Returns the closest int to the argument, with ties rounding to positive infinity.

public static long

Returns:

the value of the argument rounded to the nearest long value.
round
(double
a floating-point value to be rounded to a long.
a
)

Returns the closest long to the argument, with ties rounding to positive infinity.

public static double

Returns:

d × 2scaleFactor
scalb
(double
number to be scaled by a power of two.
d
,
int
power of 2 used to scale d
scaleFactor
)

Returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply.

public static float

Returns:

f × 2scaleFactor
scalb
(float
number to be scaled by a power of two.
f
,
int
power of 2 used to scale f
scaleFactor
)

Returns f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply.

public static double

Returns:

the signum function of the argument
signum
(double
the floating-point value whose signum is to be returned
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.

public static float

Returns:

the signum function of the argument
signum
(float
the floating-point value whose signum is to be returned
f
)

Returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.

public static double

Returns:

the sine of the argument.
sin
(double
an angle, in radians.
a
)

Returns the trigonometric sine of an angle.

public static double

Returns:

The hyperbolic sine of x.
sinh
(double
The number whose hyperbolic sine is to be returned.
x
)

Returns the hyperbolic sine of a double value.

public static double

Returns:

the positive square root of a. If the argument is NaN or less than zero, the result is NaN.
sqrt
(double
a value.
a
)

Returns the correctly rounded positive square root of a double value.

public static int

Returns:

the result
subtractExact
(int
the first value
x
,
int
the second value to subtract from the first
y
)

Returns the difference of the arguments, throwing an exception if the result overflows an int.

public static long

Returns:

the result
subtractExact
(long
the first value
x
,
long
the second value to subtract from the first
y
)

Returns the difference of the arguments, throwing an exception if the result overflows a long.

public static double

Returns:

the tangent of the argument.
tan
(double
an angle, in radians.
a
)

Returns the trigonometric tangent of an angle.

public static double

Returns:

The hyperbolic tangent of x.
tanh
(double
The number whose hyperbolic tangent is to be returned.
x
)

Returns the hyperbolic tangent of a double value.

public static double

Returns:

the measurement of the angle angrad in degrees.
toDegrees
(double
an angle, in radians
angrad
)

Converts an angle measured in radians to an approximately equivalent angle measured in degrees.

public static int

Returns:

the argument as an int
toIntExact
(long
the long value
value
)

Returns the value of the long argument, throwing an exception if the value overflows an int.

public static double

Returns:

the measurement of the angle angdeg in radians.
toRadians
(double
an angle, in degrees
angdeg
)

Converts an angle measured in degrees to an approximately equivalent angle measured in radians.

public static double

Returns:

the size of an ulp of the argument
ulp
(double
the floating-point value whose ulp is to be returned
d
)

Returns the size of an ulp of the argument.

public static float

Returns:

the size of an ulp of the argument
ulp
(float
the floating-point value whose ulp is to be returned
f
)

Returns the size of an ulp of the argument.

public static long

Returns:

the result
unsignedMultiplyHigh
(long
the first value
x
,
long
the second value
y
)

Returns as a long the most significant 64 bits of the unsigned 128-bit product of two unsigned 64-bit factors.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait