|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.lang.Math
public final class Math
Helper class containing useful mathematical functions and constants.
Note that angles are specified in radians. Conversion functions are provided for your convenience.
Field Summary | |
---|---|
static double |
E
The most accurate approximation to the mathematical constant e: 2.718281828459045 . |
static double |
PI
The most accurate approximation to the mathematical constant pi: 3.141592653589793 . |
Method Summary | |
---|---|
static double |
abs(double d)
Take the absolute value of the argument. |
static float |
abs(float f)
Take the absolute value of the argument. |
static int |
abs(int i)
Take the absolute value of the argument. |
static long |
abs(long l)
Take the absolute value of the argument. |
static double |
acos(double a)
The trigonometric function arccos. |
static double |
asin(double a)
The trigonometric function arcsin. |
static double |
atan(double a)
The trigonometric function arcsin. |
static double |
atan2(double y,
double x)
A special version of the trigonometric function arctan, for converting rectangular coordinates (x, y) to polar (r, theta). |
static double |
cbrt(double a)
Take a cube root. |
static double |
ceil(double a)
Take the nearest integer that is that is greater than or equal to the argument. |
static double |
cos(double a)
The trigonometric function cos. |
static double |
cosh(double a)
Returns the hyperbolic cosine of the given value. |
static double |
exp(double a)
Take ea. |
static double |
expm1(double a)
Returns ea - 1. |
static double |
floor(double a)
Take the nearest integer that is that is less than or equal to the argument. |
static double |
hypot(double a,
double b)
Returns the hypotenuse, a2 + b2 ,
without intermediate overflow or underflow. |
static double |
IEEEremainder(double x,
double y)
Get the IEEE 754 floating point remainder on two numbers. |
static double |
log(double a)
Take ln(a) (the natural log). |
static double |
log10(double a)
Returns the base 10 logarithm of the supplied value. |
static double |
log1p(double a)
Returns the natural logarithm resulting from the sum of the argument, a and 1. |
static double |
max(double a,
double b)
Return whichever argument is larger. |
static float |
max(float a,
float b)
Return whichever argument is larger. |
static int |
max(int a,
int b)
Return whichever argument is larger. |
static long |
max(long a,
long b)
Return whichever argument is larger. |
static double |
min(double a,
double b)
Return whichever argument is smaller. |
static float |
min(float a,
float b)
Return whichever argument is smaller. |
static int |
min(int a,
int b)
Return whichever argument is smaller. |
static long |
min(long a,
long b)
Return whichever argument is smaller. |
static double |
pow(double a,
double b)
Raise a number to a power. |
static double |
random()
Get a random number. |
static double |
rint(double a)
Take the nearest integer to the argument. |
static long |
round(double a)
Take the nearest long to the argument. |
static int |
round(float a)
Take the nearest integer to the argument. |
static double |
signum(double a)
Returns the sign of the argument as follows: If a is greater than zero, the result is 1.0. |
static float |
signum(float a)
Returns the sign of the argument as follows: If a is greater than zero, the result is 1.0f. |
static double |
sin(double a)
The trigonometric function sin. |
static double |
sinh(double a)
Returns the hyperbolic sine of the given value. |
static double |
sqrt(double a)
Take a square root. |
static double |
tan(double a)
The trigonometric function tan. |
static double |
tanh(double a)
Returns the hyperbolic tangent of the given value. |
static double |
toDegrees(double rads)
Convert from radians to degrees. |
static double |
toRadians(double degrees)
Convert from degrees to radians. |
static double |
ulp(double d)
Return the ulp for the given double argument. |
static float |
ulp(float f)
Return the ulp for the given float argument. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final double E
2.718281828459045
. Used in natural log and exp.
log(double)
,
exp(double)
,
Constant Field Valuespublic static final double PI
3.141592653589793
. This is the ratio of a circle's diameter
to its circumference.
Method Detail |
---|
public static int abs(int i)
Note that the the largest negative value (Integer.MIN_VALUE) cannot be made positive. In this case, because of the rules of negation in a computer, MIN_VALUE is what will be returned. This is a negative value. You have been warned.
i
- the number to take the absolute value of
Integer.MIN_VALUE
public static long abs(long l)
Note that the the largest negative value (Long.MIN_VALUE) cannot be made positive. In this case, because of the rules of negation in a computer, MIN_VALUE is what will be returned. This is a negative value. You have been warned.
l
- the number to take the absolute value of
Long.MIN_VALUE
public static float abs(float f)
This is equivalent, but faster than, calling
Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a))
.
f
- the number to take the absolute value of
public static double abs(double d)
Double.longBitsToDouble(Double.doubleToLongBits(a)
<< 1) >>> 1);
.
d
- the number to take the absolute value of
public static int min(int a, int b)
a
- the first numberb
- a second number
public static long min(long a, long b)
a
- the first numberb
- a second number
public static float min(float a, float b)
a
- the first numberb
- a second number
public static double min(double a, double b)
a
- the first numberb
- a second number
public static int max(int a, int b)
a
- the first numberb
- a second number
public static long max(long a, long b)
a
- the first numberb
- a second number
public static float max(float a, float b)
a
- the first numberb
- a second number
public static double max(double a, double b)
a
- the first numberb
- a second number
public static double sin(double a)
a
- the angle (in radians)
public static double cos(double a)
a
- the angle (in radians)
public static double tan(double a)
a
- the angle (in radians)
public static double asin(double a)
a
- the sin to turn back into an angle
public static double acos(double a)
a
- the cos to turn back into an angle
public static double atan(double a)
a
- the tan to turn back into an angle
atan2(double, double)
public static double atan2(double y, double x)
This is accurate within 2 ulps, and is semi-monotonic. To get r, use sqrt(x*x+y*y).
y
- the y positionx
- the x position
atan(double)
public static double exp(double a)
log()
. If the
argument is NaN, the result is NaN; if the argument is positive infinity,
the result is positive infinity; and if the argument is negative
infinity, the result is positive zero. This is accurate within 1 ulp,
and is semi-monotonic.
a
- the number to raise to the power
log(double)
,
pow(double, double)
public static double log(double a)
exp()
. If the
argument is NaN or negative, the result is NaN; if the argument is
positive infinity, the result is positive infinity; and if the argument
is either zero, the result is negative infinity. This is accurate within
1 ulp, and is semi-monotonic.
Note that the way to get logb(a) is to do this:
ln(a) / ln(b)
.
a
- the number to take the natural log of
a
exp(double)
public static double sqrt(double a)
For other roots, use pow(a, 1 / rootNumber).
a
- the numeric argument
pow(double, double)
public static double pow(double a, double b)
(In the foregoing descriptions, a floating-point value is
considered to be an integer if and only if it is a fixed point of the
method ceil(double)
or, equivalently, a fixed point of the
method floor(double)
. 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.) This is accurate within 1 ulp, and is semi-monotonic.
a
- the number to raiseb
- the power to raise it to
public static double IEEEremainder(double x, double y)
x - y * n
, where n is the closest
double to x / y
(ties go to the even n); for a zero
remainder, the sign is that of x
. If either argument is NaN,
the first argument is infinite, or the second argument is zero, the result
is NaN; if x is finite but y is infinite, the result is x. This is
accurate within the limits of doubles.
x
- the dividend (the top half)y
- the divisor (the bottom half)
rint(double)
public static double ceil(double a)
Math.ceil(x) == -Math.floor(-x)
.
a
- the value to act upon
a
public static double floor(double a)
Math.ceil(x) == -Math.floor(-x)
.
a
- the value to act upon
a
public static double rint(double a)
a
- the value to act upon
a
public static int round(float a)
(int) Math.floor(a + 0.5f)
. If the argument is NaN, the result
is 0; otherwise if the argument is outside the range of int, the result
will be Integer.MIN_VALUE or Integer.MAX_VALUE, as appropriate.
a
- the argument to round
Integer.MIN_VALUE
,
Integer.MAX_VALUE
public static long round(double a)
(long) Math.floor(a + 0.5)
. If the argument is NaN, the
result is 0; otherwise if the argument is outside the range of long, the
result will be Long.MIN_VALUE or Long.MAX_VALUE, as appropriate.
a
- the argument to round
Long.MIN_VALUE
,
Long.MAX_VALUE
public static double random()
Random.nextDouble()
,
System.currentTimeMillis()
public static double toRadians(double degrees)
degrees
- an angle in degrees
public static double toDegrees(double rads)
rads
- an angle in radians
public static double cbrt(double a)
Take a cube root. If the argument is NaN
, an infinity or
zero, then the original value is returned. The returned result is
within 1 ulp of the exact result. For a finite value, x
,
the cube root of -x
is equal to the negation of the cube root
of x
.
For a square root, use sqrt
. For other roots, use
pow(a, 1 / rootNumber)
.
a
- the numeric argument
sqrt(double)
,
pow(double, double)
public static double cosh(double a)
Returns the hyperbolic cosine of the given value. For a value,
x
, the hyperbolic cosine is (ex +
e-x)/2
with e
being Euler's number. The returned
result is within 2.5 ulps of the exact result.
If the supplied value is NaN
, then the original value is
returned. For either infinity, positive infinity is returned.
The hyperbolic cosine of zero is 1.0.
a
- the numeric argument
a
.public static double expm1(double a)
Returns ea - 1. For values close to 0, the
result of
expm1(a) + 1
tend to be much closer to the
exact result than simply exp(x)
. The result is within
1 ulp of the exact result, and results are semi-monotonic. For finite
inputs, the returned value is greater than or equal to -1.0. Once
a result enters within half a ulp of this limit, the limit is returned.
For NaN
, positive infinity and zero, the original value
is returned. Negative infinity returns a result of -1.0 (the limit).
a
- the numeric argument
ea - 1
public static double hypot(double a, double b)
Returns the hypotenuse, a2 + b2
,
without intermediate overflow or underflow. The returned result is
within 1 ulp of the exact result. If one parameter is held constant,
then the result in the other parameter is semi-monotonic.
If either of the arguments is an infinity, then the returned result
is positive infinity. Otherwise, if either argument is NaN
,
then NaN
is returned.
a
- the first parameter.b
- the second parameter.
public static double log10(double a)
Returns the base 10 logarithm of the supplied value. The returned result is within 1 ulp of the exact result, and the results are semi-monotonic.
Arguments of either NaN
or less than zero return
NaN
. An argument of positive infinity returns positive
infinity. Negative infinity is returned if either positive or negative
zero is supplied. Where the argument is the result of
10n, then
n
is returned.
a
- the numeric argument.
a
.public static double log1p(double a)
Returns the natural logarithm resulting from the sum of the argument,
a
and 1. For values close to 0, the
result of log1p(a)
tend to be much closer to the
exact result than simply log(1.0+a)
. The returned
result is within 1 ulp of the exact result, and the results are
semi-monotonic.
Arguments of either NaN
or less than -1 return
NaN
. An argument of positive infinity or zero
returns the original argument. Negative infinity is returned from an
argument of -1.
a
- the numeric argument.
a
+ 1.public static double signum(double a)
Returns the sign of the argument as follows:
a
is greater than zero, the result is 1.0.a
is less than zero, the result is -1.0.a
is NaN
, the result is NaN
.
a
is positive or negative zero, the result is the
same.
a
- the numeric argument.
public static float signum(float a)
Returns the sign of the argument as follows:
a
is greater than zero, the result is 1.0f.a
is less than zero, the result is -1.0f.a
is NaN
, the result is NaN
.
a
is positive or negative zero, the result is the
same.
a
- the numeric argument.
public static double sinh(double a)
Returns the hyperbolic sine of the given value. For a value,
x
, the hyperbolic sine is (ex -
e-x)/2
with e
being Euler's number. The returned
result is within 2.5 ulps of the exact result.
If the supplied value is NaN
, an infinity or a zero, then the
original value is returned.
a
- the numeric argument
a
.public static double tanh(double a)
Returns the hyperbolic tangent of the given value. For a value,
x
, the hyperbolic tangent is (ex -
e-x)/(ex + e-x)
(i.e. sinh(a)/cosh(a)
)
with e
being Euler's number. The returned
result is within 2.5 ulps of the exact result. The absolute value
of the exact result is always less than 1. Computed results are thus
less than or equal to 1 for finite arguments, with results within
half a ulp of either positive or negative 1 returning the appropriate
limit value (i.e. as if the argument was an infinity).
If the supplied value is NaN
or zero, then the original
value is returned. Positive infinity returns +1.0 and negative infinity
returns -1.0.
a
- the numeric argument
a
.public static double ulp(double d)
Double.MIN_VALUE
is returned.
d
- the double whose ulp should be returned
public static float ulp(float f)
Float.MIN_VALUE
is returned.
f
- the float whose ulp should be returned
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |