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.
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.
Modifier and Type | Class and Description |
---|---|
private static class |
Modifier and Type | Field 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 |
private static final long | |
private static final long | |
public static final double | PI
The |
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 |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static int | Returns: the absolute value of the argument.the argument whose absolute value is to be determined a)Returns the absolute value of an |
public static long | Returns: the absolute value of the argument.the argument whose absolute value is to be determined a)Returns the absolute value of a |
public static float | Returns: the absolute value of the argument.the argument whose absolute value is to be determined a)Returns the absolute value of a |
public static double | Returns: the absolute value of the argument.the argument whose absolute value is to be determined a)Returns the absolute value of a |
public static int | Returns: the absolute value of the argument, unless overflow occursthe argument whose absolute value is to be determined a)Returns the mathematical absolute value of an |
public static long | Returns: the absolute value of the argument, unless overflow occursthe argument whose absolute value is to be determined a)Returns the mathematical absolute value of an |
public static double | Returns: the arc cosine of the argument.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 resultthe first value x, int the second value y)Returns the sum of its arguments,
throwing an exception if the result overflows an |
public static long | Returns: the resultthe first value x, long the second value y)Returns the sum of its arguments,
throwing an exception if the result overflows a |
public static double | Returns: the arc sine of the argument.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.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 (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.the ordinate coordinate y, double the abscissa coordinate x)Returns the angle theta from the conversion of rectangular
coordinates ( |
public static double | |
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.a value. a)Returns the smallest (closest to negative infinity)
|
public static int | Returns: the smallest (closest to negative infinity)int value that is greater than or equal to the algebraic quotient.the dividend x, int the divisor y)Returns the smallest (closest to negative infinity)
|
public static long | Returns: the smallest (closest to negative infinity)long value that is greater than or equal to the algebraic quotient.the dividend x, int the divisor y)Returns the smallest (closest to negative infinity)
|
public static long | Returns: the smallest (closest to negative infinity)long value that is greater than or equal to the algebraic quotient.the dividend x, long the divisor y)Returns the smallest (closest to negative infinity)
|
public static int | Returns: the smallest (closest to negative infinity)int value that is greater than or equal to the algebraic quotient.the dividend x, int the divisor y)Returns the smallest (closest to negative infinity)
|
public static long | Returns: the smallest (closest to negative infinity)long value that is greater than or equal to the algebraic quotient.the dividend x, long the divisor y)Returns the smallest (closest to negative infinity)
|
public static int | Returns: the ceiling modulusx - (ceilDiv(x, y) * y) the dividend x, int the divisor y)Returns the ceiling modulus of the |
public static int | Returns: the ceiling modulusx - (ceilDiv(x, y) * y) the dividend x, int the divisor y)Returns the ceiling modulus of the |
public static long | Returns: the ceiling modulusx - (ceilDiv(x, y) * y) the dividend x, long the divisor y)Returns the ceiling modulus of the |
public static int | Returns: a clamped value that fits intomin..max intervalvalue 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 intomin..max intervalvalue 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 intomin..max intervalvalue 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 intomin..max intervalvalue 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 ofmagnitude
and the sign of sign .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 ofmagnitude
and the sign of sign .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.an angle, in radians. a)Returns the trigonometric cosine of an angle. |
public static double | Returns: The hyperbolic cosine ofx .The number whose hyperbolic cosine is to be returned. x)Returns the hyperbolic cosine of a |
public static int | Returns: the resultthe value to decrement a)Returns the argument decremented by one, throwing an exception if the
result overflows an |
public static long | Returns: the resultthe value to decrement a)Returns the argument decremented by one, throwing an exception if the
result overflows a |
public static int | Returns: the quotientx / y the dividend x, int the divisor y)Returns the quotient of the arguments, throwing an exception if the
result overflows an |
public static long | Returns: the quotientx / y the dividend x, long the divisor y)Returns the quotient of the arguments, throwing an exception if the
result overflows a |
public static double | Returns: the value ea ,
where e is the base of the natural logarithms.the exponent to raise e to. a)Returns Euler's number e raised to the power of a
|
public static double | Returns: the value ex - 1.the exponent to raise e to in the computation of
e x)x -1.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.a value. a)Returns the largest (closest to positive infinity)
|
public static int | Returns: the largest (closest to positive infinity)int value that is less than or equal to the algebraic quotient.the dividend x, int the divisor y)Returns the largest (closest to positive infinity)
|
public static long | Returns: the largest (closest to positive infinity)long value that is less than or equal to the algebraic quotient.the dividend x, int the divisor y)Returns the largest (closest to positive infinity)
|
public static long | Returns: the largest (closest to positive infinity)long value that is less than or equal to the algebraic quotient.the dividend x, long the divisor y)Returns the largest (closest to positive infinity)
|
public static int | Returns: the largest (closest to positive infinity)int value that is less than or equal to the algebraic quotient.the dividend x, int the divisor y)Returns the largest (closest to positive infinity)
|
public static long | Returns: the largest (closest to positive infinity)long value that is less than or equal to the algebraic quotient.the dividend x, long the divisor y)Returns the largest (closest to positive infinity)
|
public static int | Returns: the floor modulusx - (floorDiv(x, y) * y) the dividend x, int the divisor y)Returns the floor modulus of the |
public static int | Returns: the floor modulusx - (floorDiv(x, y) * y) the dividend x, int the divisor y)Returns the floor modulus of the |
public static long | Returns: the floor modulusx - (floorDiv(x, y) * y) the dividend x, long the divisor y)Returns the floor modulus of the |
public static double | Returns: (a × b + c) computed, as if with unlimited range and precision, and rounded once to the nearestdouble valuea 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
|
public static float | Returns: (a × b + c) computed, as if with unlimited range and precision, and rounded once to the nearestfloat valuea 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
|
public static int | Returns: the unbiased exponent of the argumenta f)float valueReturns the unbiased exponent used in the representation of a
|
public static int | Returns: the unbiased exponent of the argumenta d)double valueReturns the unbiased exponent used in the representation of a
|
public static double | Returns: sqrt(x2 +y2) without intermediate overflow or underflowa value x, double a value y)Returns sqrt(x2 +y2) without intermediate overflow or underflow. |
public static double | Returns: the remainder whenf1 is divided by
f2 .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 resultthe value to increment a)Returns the argument incremented by one, throwing an exception if the
result overflows an |
public static long | Returns: the resultthe value to increment a)Returns the argument incremented by one, throwing an exception if the
result overflows a |
public static double | Returns: the value lna , the natural logarithm of
a .a value a)Returns the natural logarithm (base e) of a |
public static double | Returns: the base 10 logarithm ofa .a value a)Returns the base 10 logarithm of a |
public static double | Returns: the value ln(x + 1), the natural
log of x + 1a value x)Returns the natural logarithm of the sum of the argument and 1. |
public static int | Returns: the larger ofa and b .an argument. a, int another argument. b)Returns the greater of two |
public static long | Returns: the larger ofa and b .an argument. a, long another argument. b)Returns the greater of two |
public static float | Returns: the larger ofa and b .an argument. a, float another argument. b)Returns the greater of two |
public static double | Returns: the larger ofa and b .an argument. a, double another argument. b)Returns the greater of two |
public static int | Returns: the smaller ofa and b .an argument. a, int another argument. b)Returns the smaller of two |
public static long | Returns: the smaller ofa and b .an argument. a, long another argument. b)Returns the smaller of two |
public static float | Returns: the smaller ofa and b .an argument. a, float another argument. b)Returns the smaller of two |
public static double | Returns: the smaller ofa and b .an argument. a, double another argument. b)Returns the smaller of two |
public static int | Returns: the resultthe first value x, int the second value y)Returns the product of the arguments,
throwing an exception if the result overflows an |
public static long | Returns: the resultthe first value x, int the second value y)Returns the product of the arguments, throwing an exception if the result
overflows a |
public static long | Returns: the resultthe first value x, long the second value y)Returns the product of the arguments,
throwing an exception if the result overflows a |
public static long | Returns: the resultthe first value x, int the second value y)Returns the exact mathematical product of the arguments. |
public static long | Returns: the resultthe first value x, long the second value y)Returns as a |
public static int | Returns: the resultthe value to negate a)Returns the negation of the argument, throwing an exception if the
result overflows an |
public static long | Returns: the resultthe value to negate a)Returns the negation of the argument, throwing an exception if the
result overflows a |
public static double | Returns: The floating-point number adjacent tostart in the
direction of direction .starting floating-point value start, double value indicating which of
direction)start 's neighbors or start should
be returnedReturns 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 tostart in the
direction of direction .starting floating-point value start, double value indicating which of
direction)start 's neighbors or start should
be returnedReturns 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.starting floating-point value d)Returns the floating-point value adjacent to |
public static float | Returns: The adjacent floating-point value closer to negative infinity.starting floating-point value f)Returns the floating-point value adjacent to |
public static double | Returns: The adjacent floating-point value closer to positive infinity.starting floating-point value d)Returns the floating-point value adjacent to |
public static float | Returns: The adjacent floating-point value closer to positive infinity.starting floating-point value f)Returns the floating-point value adjacent to |
public static double | Returns: the valuea b .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 | |
pack-priv static float | |
public static double | Returns: a pseudorandomdouble greater than or equal
to 0.0 and less than 1.0 .Returns a |
public static double | Returns: the closest floating-point value toa that is
equal to a mathematical integer.a a)double value.Returns the |
public static int | Returns: the value of the argument rounded to the nearestint value.a floating-point value to be rounded to an integer. a)Returns the closest |
public static long | Returns: the value of the argument rounded to the nearestlong value.a floating-point value to be rounded to a
a)long .Returns the closest |
public static double | Returns: d × 2scaleFactor number to be scaled by a power of two. d, int power of 2 used to scale scaleFactor)d Returns |
public static float | Returns: f × 2scaleFactor number to be scaled by a power of two. f, int power of 2 used to scale scaleFactor)f Returns |
public static double | Returns: the signum function of the argumentthe 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 argumentthe 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.an angle, in radians. a)Returns the trigonometric sine of an angle. |
public static double | Returns: The hyperbolic sine ofx .The number whose hyperbolic sine is to be returned. x)Returns the hyperbolic sine of a |
public static double | Returns: the positive square root ofa .
If the argument is NaN or less than zero, the result is NaN.a value. a)Returns the correctly rounded positive square root of a
|
public static int | Returns: the resultthe 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 |
public static long | Returns: the resultthe 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 |
public static double | Returns: the tangent of the argument.an angle, in radians. a)Returns the trigonometric tangent of an angle. |
public static double | Returns: The hyperbolic tangent ofx .The number whose hyperbolic tangent is to be returned. x)Returns the hyperbolic tangent of a |
public static double | Returns: the measurement of the angleangrad
in degrees.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 intthe long value value)Returns the value of the |
public static double | Returns: the measurement of the angleangdeg
in radians.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 argumentthe 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 argumentthe floating-point value whose ulp is to be returned f)Returns the size of an ulp of the argument. |
public static long | Returns: the resultthe first value x, long the second value y)Returns as a |