public final class StrictMath extends Object
Math
, but is 100% portable, because it uses
no native methods whatsoever. Also, these algorithms are all accurate
to less than 1 ulp, and execute in strictfp
mode, while
Math is allowed to vary in its results for some functions. Unfortunately,
this usually means StrictMath has less efficiency and speed, as Math can
use native methods.
The source of the various algorithms used is the fdlibm library, at:
http://www.netlib.org/fdlibm/
Note that angles are specified in radians. Conversion functions are
provided for your convenience.
Modifier and Type | Field and Description |
---|---|
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 . |
Modifier and Type | Method and Description |
---|---|
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 x)
The trigonometric function arccos.
|
static double |
asin(double x)
The trigonometric function arcsin.
|
static double |
atan(double x)
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 x)
Returns the cube root of
x . |
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 x)
Returns the hyperbolic cosine of
x , which is defined as
(exp(x) + exp(-x)) / 2. |
static double |
exp(double x)
Take ea.
|
static double |
expm1(double x)
Returns ex - 1.
|
static double |
floor(double a)
Take the nearest integer that is that is less than or equal to the
argument.
|
static double |
IEEEremainder(double x,
double y)
Get the IEEE 754 floating point remainder on two numbers.
|
static double |
log(double x)
Take ln(a) (the natural log).
|
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 x,
double y)
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 d)
Take the nearest long to the argument.
|
static int |
round(float f)
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 x)
Returns the hyperbolic sine of
x which is defined as
(exp(x) - exp(-x)) / 2. |
static double |
sqrt(double x)
Take a square root.
|
static double |
tan(double a)
The trigonometric function tan.
|
static double |
tanh(double x)
Returns the hyperbolic tangent of
x , which is defined as
(exp(x) - exp(-x)) / (exp(x) + exp(-x)), i.e. sinh(x) / cosh(x). |
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.
|
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.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 ofInteger.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 ofLong.MIN_VALUE
public static float abs(float f)
f
- the number to take the absolute value ofpublic static double abs(double d)
d
- the number to take the absolute value ofpublic static int min(int a, int b)
a
- the first numberb
- a second numberpublic static long min(long a, long b)
a
- the first numberb
- a second numberpublic static float min(float a, float b)
a
- the first numberb
- a second numberpublic static double min(double a, double b)
a
- the first numberb
- a second numberpublic static int max(int a, int b)
a
- the first numberb
- a second numberpublic static long max(long a, long b)
a
- the first numberb
- a second numberpublic static float max(float a, float b)
a
- the first numberb
- a second numberpublic static double max(double a, double b)
a
- the first numberb
- a second numberpublic 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 x)
x
- the sin to turn back into an anglepublic static double acos(double x)
x
- the cos to turn back into an anglepublic static double atan(double x)
x
- the tan to turn back into an angleatan2(double, double)
public static double atan2(double y, double x)
This returns theta, the angle of the point. To get r, albeit slightly inaccurately, use sqrt(x*x+y*y).
y
- the y positionx
- the x positionatan(double)
public static double sinh(double x)
x
which is defined as
(exp(x) - exp(-x)) / 2.
Special cases:
x
- the argument to sinhx
public static double cosh(double x)
x
, which is defined as
(exp(x) + exp(-x)) / 2.
Special cases:
x
- the argument to coshx
public static double tanh(double x)
x
, which is defined as
(exp(x) - exp(-x)) / (exp(x) + exp(-x)), i.e. sinh(x) / cosh(x).
Special cases:
x
- the argument to tanhx
public static double cbrt(double x)
x
. The sign of the cube root
is equal to the sign of x
.
Special cases:
x
- the number to take the cube root ofx
sqrt(double)
public static double exp(double x)
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.x
- the number to raise to the powerlog(double)
,
pow(double, double)
public static double expm1(double x)
x
- the argument to ex - 1.x
minus one.exp(double)
public static double log(double x)
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.
Note that the way to get logb(a) is to do this:
ln(a) / ln(b)
.
x
- the number to take the natural log ofa
exp(double)
public static double sqrt(double x)
For other roots, use pow(x, 1/rootNumber).
x
- the numeric argumentpow(double, double)
public static double pow(double x, double y)
(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.)
x
- the number to raisey
- the power to raise it topublic 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.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 upona
public static double floor(double a)
Math.ceil(x) == -Math.floor(-x)
.a
- the value to act upona
public static double rint(double a)
a
- the value to act upona
public static int round(float f)
(int) Math.floor(f + 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.f
- the argument to roundInteger.MIN_VALUE
,
Integer.MAX_VALUE
public static long round(double d)
(long) Math.floor(d + 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.d
- the argument to roundLong.MIN_VALUE
,
Long.MAX_VALUE
public static double random()
Random.nextDouble()
,
System.currentTimeMillis()
public static double toRadians(double degrees)
degrees
- an angle in degreespublic static double toDegrees(double rads)
rads
- an angle in radianspublic 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 ulp(double d)
Double.MIN_VALUE
is returned.d
- the double whose ulp should be returnedpublic static float ulp(float f)
Float.MIN_VALUE
is returned.f
- the float whose ulp should be returned