java.lang.Object | |
↳ | java.lang.Math |
Class Math provides basic math constants and operations such as trigonometric functions, hyperbolic functions, exponential, logarithms, etc.
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
double | E | The double value closest to e, the base of the natural logarithm. | |||||||||
double | PI | The double value closest to pi, the ratio of a circle's circumference to its diameter. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns the remainder of dividing
x by y using the IEEE
754 rules. | |||||||||||
Returns the absolute value of the argument.
| |||||||||||
Returns the absolute value of the argument.
| |||||||||||
Returns the absolute value of the argument.
| |||||||||||
Returns the absolute value of the argument.
| |||||||||||
Returns the closest double approximation of the arc cosine of the
argument within the range
[0..pi] . | |||||||||||
Returns the closest double approximation of the arc sine of the argument
within the range
[-pi/2..pi/2] . | |||||||||||
Returns the closest double approximation of the arc tangent of the
argument within the range
[-pi/2..pi/2] . | |||||||||||
Returns the closest double approximation of the arc tangent of
y/x within the range [-pi..pi] . | |||||||||||
Returns the closest double approximation of the cube root of the
argument.
| |||||||||||
Returns the double conversion of the most negative (closest to negative
infinity) integer value which is greater than the argument.
| |||||||||||
Returns a double with the given magnitude and the sign of
sign . | |||||||||||
Returns a float with the given magnitude and the sign of
sign . | |||||||||||
Returns the closest double approximation of the cosine of the argument.
| |||||||||||
Returns the closest double approximation of the hyperbolic cosine of the
argument.
| |||||||||||
Returns the closest double approximation of the raising "e" to the power
of the argument.
| |||||||||||
Returns the closest double approximation of
e d - 1 . | |||||||||||
Returns the double conversion of the most positive (closest to positive
infinity) integer value which is less than the argument.
| |||||||||||
Returns the unbiased base-2 exponent of float
f . | |||||||||||
Returns the unbiased base-2 exponent of double
d . | |||||||||||
Returns
sqrt( x 2 +
y 2 ) . | |||||||||||
Returns the closest double approximation of the natural logarithm of the
argument.
| |||||||||||
Returns the closest double approximation of the base 10 logarithm of the
argument.
| |||||||||||
Returns the closest double approximation of the natural logarithm of the
sum of the argument and 1.
| |||||||||||
Returns the most positive (closest to positive infinity) of the two
arguments.
| |||||||||||
Returns the most positive (closest to positive infinity) of the two
arguments.
| |||||||||||
Returns the most positive (closest to positive infinity) of the two
arguments.
| |||||||||||
Returns the most positive (closest to positive infinity) of the two
arguments.
| |||||||||||
Returns the most negative (closest to negative infinity) of the two
arguments.
| |||||||||||
Returns the most negative (closest to negative infinity) of the two
arguments.
| |||||||||||
Returns the most negative (closest to negative infinity) of the two
arguments.
| |||||||||||
Returns the most negative (closest to negative infinity) of the two
arguments.
| |||||||||||
Returns the next float after
start in the given direction . | |||||||||||
Returns the next double after
start in the given direction . | |||||||||||
Returns the next double larger than
d . | |||||||||||
Returns the next float larger than
f . | |||||||||||
Returns the closest double approximation of the result of raising
x to the power of y . | |||||||||||
Returns a pseudo-random double
n , where n >= 0.0 && n < 1.0 . | |||||||||||
Returns the double conversion of the result of rounding the argument to
an integer.
| |||||||||||
Returns the result of rounding the argument to an integer.
| |||||||||||
Returns the result of rounding the argument to an integer.
| |||||||||||
Returns
d * 2^scaleFactor . | |||||||||||
Returns
d * 2^scaleFactor . | |||||||||||
Returns the signum function of the argument.
| |||||||||||
Returns the signum function of the argument.
| |||||||||||
Returns the closest double approximation of the sine of the argument.
| |||||||||||
Returns the closest double approximation of the hyperbolic sine of the
argument.
| |||||||||||
Returns the closest double approximation of the square root of the
argument.
| |||||||||||
Returns the closest double approximation of the tangent of the argument.
| |||||||||||
Returns the closest double approximation of the hyperbolic tangent of the
argument.
| |||||||||||
Returns the measure in degrees of the supplied radian angle.
| |||||||||||
Returns the measure in radians of the supplied degree angle.
| |||||||||||
Returns the argument's ulp (unit in the last place).
| |||||||||||
Returns the argument's ulp (unit in the last place).
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
The double value closest to e, the base of the natural logarithm.
The double value closest to pi, the ratio of a circle's circumference to its diameter.
Returns the remainder of dividing x
by y
using the IEEE
754 rules. The result is x-round(x/p)*p
where round(x/p)
is the nearest integer (rounded to even), but without numerical
cancellation problems.
Special cases:
IEEEremainder((anything), 0) = NaN
IEEEremainder(+infinity, (anything)) = NaN
IEEEremainder(-infinity, (anything)) = NaN
IEEEremainder(NaN, (anything)) = NaN
IEEEremainder((anything), NaN) = NaN
IEEEremainder(x, +infinity) = x
where x is anything but
+/-infinityIEEEremainder(x, -infinity) = x
where x is anything but
+/-infinityx | the numerator of the operation. |
---|---|
y | the denominator of the operation. |
x/y
.
Returns the absolute value of the argument.
Special cases:
abs(-0.0) = +0.0
abs(+infinity) = +infinity
abs(-infinity) = +infinity
abs(NaN) = NaN
d | the value whose absolute value has to be computed. |
---|
Returns the absolute value of the argument. If the argument is Long.MIN_VALUE
, Long.MIN_VALUE
is returned.
l | the value whose absolute value has to be computed. |
---|
Returns the absolute value of the argument.
Special cases:
abs(-0.0) = +0.0
abs(+infinity) = +infinity
abs(-infinity) = +infinity
abs(NaN) = NaN
f | the value whose absolute value has to be computed. |
---|
Returns the absolute value of the argument.
If the argument is Integer.MIN_VALUE
, Integer.MIN_VALUE
is returned.
i | the value whose absolute value has to be computed. |
---|
Returns the closest double approximation of the arc cosine of the
argument within the range [0..pi]
. The returned result is within
1 ulp (unit in the last place) of the real result.
Special cases:
acos((anything > 1) = NaN
acos((anything < -1) = NaN
acos(NaN) = NaN
d | the value to compute arc cosine of. |
---|
Returns the closest double approximation of the arc sine of the argument
within the range [-pi/2..pi/2]
. The returned result is within 1
ulp (unit in the last place) of the real result.
Special cases:
asin((anything > 1)) = NaN
asin((anything < -1)) = NaN
asin(NaN) = NaN
d | the value whose arc sine has to be computed. |
---|
Returns the closest double approximation of the arc tangent of the
argument within the range [-pi/2..pi/2]
. The returned result is
within 1 ulp (unit in the last place) of the real result.
Special cases:
atan(+0.0) = +0.0
atan(-0.0) = -0.0
atan(+infinity) = +pi/2
atan(-infinity) = -pi/2
atan(NaN) = NaN
d | the value whose arc tangent has to be computed. |
---|
Returns the closest double approximation of the arc tangent of y/x
within the range [-pi..pi]
. This is the angle of the polar
representation of the rectangular coordinates (x,y). The returned result
is within 2 ulps (units in the last place) of the real result.
Special cases:
atan2((anything), NaN ) = NaN;
atan2(NaN , (anything) ) = NaN;
atan2(+0.0, +(anything but NaN)) = +0.0
atan2(-0.0, +(anything but NaN)) = -0.0
atan2(+0.0, -(anything but NaN)) = +pi
atan2(-0.0, -(anything but NaN)) = -pi
atan2(+(anything but 0 and NaN), 0) = +pi/2
atan2(-(anything but 0 and NaN), 0) = -pi/2
atan2(+(anything but infinity and NaN), +infinity)
=
+0.0
atan2(-(anything but infinity and NaN), +infinity)
=
-0.0
atan2(+(anything but infinity and NaN), -infinity) = +pi
atan2(-(anything but infinity and NaN), -infinity) = -pi
atan2(+infinity, +infinity ) = +pi/4
atan2(-infinity, +infinity ) = -pi/4
atan2(+infinity, -infinity ) = +3pi/4
atan2(-infinity, -infinity ) = -3pi/4
atan2(+infinity, (anything but,0, NaN, and infinity))
=
+pi/2
atan2(-infinity, (anything but,0, NaN, and infinity))
=
-pi/2
y | the numerator of the value whose atan has to be computed. |
---|---|
x | the denominator of the value whose atan has to be computed. |
y/x
.
Returns the closest double approximation of the cube root of the argument.
Special cases:
cbrt(+0.0) = +0.0
cbrt(-0.0) = -0.0
cbrt(+infinity) = +infinity
cbrt(-infinity) = -infinity
cbrt(NaN) = NaN
d | the value whose cube root has to be computed. |
---|
Returns the double conversion of the most negative (closest to negative infinity) integer value which is greater than the argument.
Special cases:
ceil(+0.0) = +0.0
ceil(-0.0) = -0.0
ceil((anything in range (-1,0)) = -0.0
ceil(+infinity) = +infinity
ceil(-infinity) = -infinity
ceil(NaN) = NaN
d | the value whose closest integer value has to be computed. |
---|
Returns a double with the given magnitude and the sign of sign
.
If sign
is NaN, the sign of the result is arbitrary.
If you need a determinate sign in such cases, use StrictMath.copySign
.
Returns a float with the given magnitude and the sign of sign
.
If sign
is NaN, the sign of the result is arbitrary.
If you need a determinate sign in such cases, use StrictMath.copySign
.
Returns the closest double approximation of the cosine of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.
Special cases:
cos(+infinity) = NaN
cos(-infinity) = NaN
cos(NaN) = NaN
d | the angle whose cosine has to be computed, in radians. |
---|
Returns the closest double approximation of the hyperbolic cosine of the argument. The returned result is within 2.5 ulps (units in the last place) of the real result.
Special cases:
cosh(+infinity) = +infinity
cosh(-infinity) = +infinity
cosh(NaN) = NaN
d | the value whose hyperbolic cosine has to be computed. |
---|
Returns the closest double approximation of the raising "e" to the power of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.
Special cases:
exp(+infinity) = +infinity
exp(-infinity) = +0.0
exp(NaN) = NaN
d | the value whose exponential has to be computed. |
---|
Returns the closest double approximation of e
d
- 1
. If the argument is very close to 0, it is much more
accurate to use expm1(d)+1
than exp(d)
(due to
cancellation of significant digits). The returned result is within 1 ulp
(unit in the last place) of the real result.
For any finite input, the result is not less than -1.0. If the real result is within 0.5 ulp of -1, -1.0 is returned.
Special cases:
expm1(+0.0) = +0.0
expm1(-0.0) = -0.0
expm1(+infinity) = +infinity
expm1(-infinity) = -1.0
expm1(NaN) = NaN
d | the value to compute the e d
- 1 of. |
---|
e
d
- 1
value of the
argument.
Returns the double conversion of the most positive (closest to positive infinity) integer value which is less than the argument.
Special cases:
floor(+0.0) = +0.0
floor(-0.0) = -0.0
floor(+infinity) = +infinity
floor(-infinity) = -infinity
floor(NaN) = NaN
d | the value whose closest integer value has to be computed. |
---|
Returns the unbiased base-2 exponent of float f
.
Returns the unbiased base-2 exponent of double d
.
Returns sqrt(
x
2
+
y
2
)
. The final result is without
medium underflow or overflow. The returned result is within 1 ulp (unit
in the last place) of the real result. If one parameter remains constant,
the result should be semi-monotonic.
Special cases:
hypot(+infinity, (anything including NaN)) = +infinity
hypot(-infinity, (anything including NaN)) = +infinity
hypot((anything including NaN), +infinity) = +infinity
hypot((anything including NaN), -infinity) = +infinity
hypot(NaN, NaN) = NaN
x | a double number. |
---|---|
y | a double number. |
sqrt(
x
2
+
y
2
)
value of the
arguments.
Returns the closest double approximation of the natural logarithm of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.
Special cases:
log(+0.0) = -infinity
log(-0.0) = -infinity
log((anything < 0) = NaN
log(+infinity) = +infinity
log(-infinity) = NaN
log(NaN) = NaN
d | the value whose log has to be computed. |
---|
Returns the closest double approximation of the base 10 logarithm of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.
Special cases:
log10(+0.0) = -infinity
log10(-0.0) = -infinity
log10((anything < 0) = NaN
log10(+infinity) = +infinity
log10(-infinity) = NaN
log10(NaN) = NaN
d | the value whose base 10 log has to be computed. |
---|
Returns the closest double approximation of the natural logarithm of the
sum of the argument and 1. If the argument is very close to 0, it is much
more accurate to use log1p(d)
than log(1.0+d)
(due to
numerical cancellation). The returned result is within 1 ulp (unit in the
last place) of the real result and is semi-monotonic.
Special cases:
log1p(+0.0) = +0.0
log1p(-0.0) = -0.0
log1p((anything < 1)) = NaN
log1p(-1.0) = -infinity
log1p(+infinity) = +infinity
log1p(-infinity) = NaN
log1p(NaN) = NaN
d | the value to compute the ln(1+d) of. |
---|
Returns the most positive (closest to positive infinity) of the two arguments.
l1 | the first argument. |
---|---|
l2 | the second argument. |
l1
and l2
.
Returns the most positive (closest to positive infinity) of the two arguments.
i1 | the first argument. |
---|---|
i2 | the second argument. |
i1
and i2
.
Returns the most positive (closest to positive infinity) of the two arguments.
Special cases:
max(NaN, (anything)) = NaN
max((anything), NaN) = NaN
max(+0.0, -0.0) = +0.0
max(-0.0, +0.0) = +0.0
d1 | the first argument. |
---|---|
d2 | the second argument. |
d1
and d2
.
Returns the most positive (closest to positive infinity) of the two arguments.
Special cases:
max(NaN, (anything)) = NaN
max((anything), NaN) = NaN
max(+0.0, -0.0) = +0.0
max(-0.0, +0.0) = +0.0
f1 | the first argument. |
---|---|
f2 | the second argument. |
f1
and f2
.
Returns the most negative (closest to negative infinity) of the two arguments.
i1 | the first argument. |
---|---|
i2 | the second argument. |
i1
and i2
.
Returns the most negative (closest to negative infinity) of the two arguments.
l1 | the first argument. |
---|---|
l2 | the second argument. |
l1
and l2
.
Returns the most negative (closest to negative infinity) of the two arguments.
Special cases:
min(NaN, (anything)) = NaN
min((anything), NaN) = NaN
min(+0.0, -0.0) = -0.0
min(-0.0, +0.0) = -0.0
d1 | the first argument. |
---|---|
d2 | the second argument. |
d1
and d2
.
Returns the most negative (closest to negative infinity) of the two arguments.
Special cases:
min(NaN, (anything)) = NaN
min((anything), NaN) = NaN
min(+0.0, -0.0) = -0.0
min(-0.0, +0.0) = -0.0
f1 | the first argument. |
---|---|
f2 | the second argument. |
f1
and f2
.
Returns the next float after start
in the given direction
.
Returns the next double after start
in the given direction
.
Returns the closest double approximation of the result of raising x
to the power of y
.
Special cases:
pow((anything), +0.0) = 1.0
pow((anything), -0.0) = 1.0
pow(x, 1.0) = x
pow((anything), NaN) = NaN
pow(NaN, (anything except 0)) = NaN
pow(+/-(|x| > 1), +infinity) = +infinity
pow(+/-(|x| > 1), -infinity) = +0.0
pow(+/-(|x| < 1), +infinity) = +0.0
pow(+/-(|x| < 1), -infinity) = +infinity
pow(+/-1.0 , +infinity) = NaN
pow(+/-1.0 , -infinity) = NaN
pow(+0.0, (+anything except 0, NaN)) = +0.0
pow(-0.0, (+anything except 0, NaN, odd integer)) = +0.0
pow(+0.0, (-anything except 0, NaN)) = +infinity
pow(-0.0, (-anything except 0, NAN, odd integer))
=
+infinity
pow(-0.0, (odd integer)) = -pow( +0 , (odd integer) )
pow(+infinity, (+anything except 0, NaN)) = +infinity
pow(+infinity, (-anything except 0, NaN)) = +0.0
pow(-infinity, (anything)) = -pow(0, (-anything))
pow((-anything), (integer))
=
pow(-1,(integer))*pow(+anything,integer)
pow((-anything except 0 and inf), (non-integer)) = NAN
x | the base of the operation. |
---|---|
y | the exponent of the operation. |
x
to the power of y
.
Returns a pseudo-random double n
, where n >= 0.0 && n < 1.0
.
This method reuses a single instance of Random
.
This method is thread-safe because access to the Random
is synchronized,
but this harms scalability. Applications may find a performance benefit from
allocating a Random
for each of their threads.
Returns the double conversion of the result of rounding the argument to an integer. Tie breaks are rounded towards even.
Special cases:
rint(+0.0) = +0.0
rint(-0.0) = -0.0
rint(+infinity) = +infinity
rint(-infinity) = -infinity
rint(NaN) = NaN
d | the value to be rounded. |
---|
Returns the result of rounding the argument to an integer. The result is
equivalent to (long) Math.floor(d+0.5)
.
Special cases:
round(+0.0) = +0.0
round(-0.0) = +0.0
round((anything > Long.MAX_VALUE) = Long.MAX_VALUE
round((anything < Long.MIN_VALUE) = Long.MIN_VALUE
round(+infinity) = Long.MAX_VALUE
round(-infinity) = Long.MIN_VALUE
round(NaN) = +0.0
d | the value to be rounded. |
---|
Returns the result of rounding the argument to an integer. The result is
equivalent to (int) Math.floor(f+0.5)
.
Special cases:
round(+0.0) = +0.0
round(-0.0) = +0.0
round((anything > Integer.MAX_VALUE) = Integer.MAX_VALUE
round((anything < Integer.MIN_VALUE) = Integer.MIN_VALUE
round(+infinity) = Integer.MAX_VALUE
round(-infinity) = Integer.MIN_VALUE
round(NaN) = +0.0
f | the value to be rounded. |
---|
Returns d
* 2^scaleFactor
. The result may be rounded.
Returns d
* 2^scaleFactor
. The result may be rounded.
Returns the signum function of the argument. If the argument is less than zero, it returns -1.0. If the argument is greater than zero, 1.0 is returned. If the argument is either positive or negative zero, the argument is returned as result.
Special cases:
signum(+0.0) = +0.0
signum(-0.0) = -0.0
signum(+infinity) = +1.0
signum(-infinity) = -1.0
signum(NaN) = NaN
d | the value whose signum has to be computed. |
---|
Returns the signum function of the argument. If the argument is less than zero, it returns -1.0. If the argument is greater than zero, 1.0 is returned. If the argument is either positive or negative zero, the argument is returned as result.
Special cases:
signum(+0.0) = +0.0
signum(-0.0) = -0.0
signum(+infinity) = +1.0
signum(-infinity) = -1.0
signum(NaN) = NaN
f | the value whose signum has to be computed. |
---|
Returns the closest double approximation of the sine of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.
Special cases:
sin(+0.0) = +0.0
sin(-0.0) = -0.0
sin(+infinity) = NaN
sin(-infinity) = NaN
sin(NaN) = NaN
d | the angle whose sin has to be computed, in radians. |
---|
Returns the closest double approximation of the hyperbolic sine of the argument. The returned result is within 2.5 ulps (units in the last place) of the real result.
Special cases:
sinh(+0.0) = +0.0
sinh(-0.0) = -0.0
sinh(+infinity) = +infinity
sinh(-infinity) = -infinity
sinh(NaN) = NaN
d | the value whose hyperbolic sine has to be computed. |
---|
Returns the closest double approximation of the square root of the argument.
Special cases:
sqrt(+0.0) = +0.0
sqrt(-0.0) = -0.0
sqrt( (anything < 0) ) = NaN
sqrt(+infinity) = +infinity
sqrt(NaN) = NaN
d | the value whose square root has to be computed. |
---|
Returns the closest double approximation of the tangent of the argument. The returned result is within 1 ulp (unit in the last place) of the real result.
Special cases:
tan(+0.0) = +0.0
tan(-0.0) = -0.0
tan(+infinity) = NaN
tan(-infinity) = NaN
tan(NaN) = NaN
d | the angle whose tangent has to be computed, in radians. |
---|
Returns the closest double approximation of the hyperbolic tangent of the argument. The absolute value is always less than 1. The returned result is within 2.5 ulps (units in the last place) of the real result. If the real result is within 0.5ulp of 1 or -1, it should return exactly +1 or -1.
Special cases:
tanh(+0.0) = +0.0
tanh(-0.0) = -0.0
tanh(+infinity) = +1.0
tanh(-infinity) = -1.0
tanh(NaN) = NaN
d | the value whose hyperbolic tangent has to be computed. |
---|
Returns the measure in degrees of the supplied radian angle. The result
is angrad * 180 / pi
.
Special cases:
toDegrees(+0.0) = +0.0
toDegrees(-0.0) = -0.0
toDegrees(+infinity) = +infinity
toDegrees(-infinity) = -infinity
toDegrees(NaN) = NaN
angrad | an angle in radians. |
---|
Returns the measure in radians of the supplied degree angle. The result
is angdeg / 180 * pi
.
Special cases:
toRadians(+0.0) = +0.0
toRadians(-0.0) = -0.0
toRadians(+infinity) = +infinity
toRadians(-infinity) = -infinity
toRadians(NaN) = NaN
angdeg | an angle in degrees. |
---|
Returns the argument's ulp (unit in the last place). The size of a ulp of
a float value is the positive distance between this value and the float
value next larger in magnitude. For non-NaN x
, ulp(-x) ==
ulp(x)
.
Special cases:
ulp(+0.0) = Float.MIN_VALUE
ulp(-0.0) = Float.MIN_VALUE
ulp(+infinity) = infinity
ulp(-infinity) = infinity
ulp(NaN) = NaN
f | the floating-point value to compute ulp of. |
---|
Returns the argument's ulp (unit in the last place). The size of a ulp of
a double value is the positive distance between this value and the double
value next larger in magnitude. For non-NaN x
, ulp(-x) ==
ulp(x)
.
Special cases:
ulp(+0.0) = Double.MIN_VALUE
ulp(-0.0) = Double.MIN_VALUE
ulp(+infinity) = infinity
ulp(-infinity) = infinity
ulp(NaN) = NaN
d | the floating-point value to compute ulp of. |
---|