Class StrictMath
public final class StrictMath extends Object
In contrast to class Math, the methods in this class return exactly
the same results on all platforms. Algorithms based on these methods thus
behave the same (e.g. regarding numerical convergence) on all platforms,
complying with the slogan "write once, run everywhere". On the other side,
the implementation of class StrictMath may be less efficient than that of
class Math, as class StrictMath cannot utilize platform specific features
such as an extended precision math co-processors.
The methods in this class are specified using the "Freely Distributable Math Library" (fdlibm), version 5.3.
-
Field Summary
-
Method Summary
Modifier and Type Method Description static doubleabs(double d)Returns the absolute value of the argument.static floatabs(float f)Returns the absolute value of the argument.static intabs(int i)Returns the absolute value of the argument.static longabs(long l)Returns the absolute value of the argument.static doubleacos(double d)Returns the closest double approximation of the arc cosine of the argument within the range[0..pi].static doubleasin(double d)Returns the closest double approximation of the arc sine of the argument within the range[-pi/2..pi/2].static doubleatan(double d)Returns the closest double approximation of the arc tangent of the argument within the range[-pi/2..pi/2].static doubleatan2(double y, double x)Returns the closest double approximation of the arc tangent ofy/xwithin the range[-pi..pi].static doublecbrt(double d)Returns the closest double approximation of the cube root of the argument.static doubleceil(double d)Returns the double conversion of the most negative (closest to negative infinity) integer value greater than or equal to the argument.static doublecopySign(double magnitude, double sign)Returns a double with the given magnitude and the sign ofsign.static floatcopySign(float magnitude, float sign)Returns a float with the given magnitude and the sign ofsign.static doublecos(double d)Returns the closest double approximation of the cosine of the argument.static doublecosh(double d)Returns the closest double approximation of the hyperbolic cosine of the argument.static doubleexp(double d)Returns the closest double approximation of the raising "e" to the power of the argument.static doubleexpm1(double d)Returns the closest double approximation ofed- 1.static doublefloor(double d)Returns the double conversion of the most positive (closest to positive infinity) integer less than or equal to the argument.static intgetExponent(double d)Returns the exponent of doubled.static intgetExponent(float f)Returns the exponent of floatf.static doublehypot(double x, double y)Returnssqrt(x2+y2).static doubleIEEEremainder(double x, double y)Returns the remainder of dividingxbyyusing the IEEE 754 rules.static doublelog(double d)Returns the closest double approximation of the natural logarithm of the argument.static doublelog10(double d)Returns the closest double approximation of the base 10 logarithm of the argument.static doublelog1p(double d)Returns the closest double approximation of the natural logarithm of the sum of the argument and 1.static doublemax(double d1, double d2)Returns the most positive (closest to positive infinity) of the two arguments.static floatmax(float f1, float f2)Returns the most positive (closest to positive infinity) of the two arguments.static intmax(int i1, int i2)Returns the most positive (closest to positive infinity) of the two arguments.static longmax(long l1, long l2)Returns the most positive (closest to positive infinity) of the two arguments.static doublemin(double d1, double d2)Returns the most negative (closest to negative infinity) of the two arguments.static floatmin(float f1, float f2)Returns the most negative (closest to negative infinity) of the two arguments.static intmin(int i1, int i2)Returns the most negative (closest to negative infinity) of the two arguments.static longmin(long l1, long l2)Returns the most negative (closest to negative infinity) of the two arguments.static doublenextAfter(double start, double direction)Returns the next double afterstartin the givendirection.static floatnextAfter(float start, double direction)Returns the next float afterstartin the givendirection.static doublenextUp(double d)Returns the next double larger thand.static floatnextUp(float f)Returns the next float larger thanf.static doublepow(double x, double y)Returns the closest double approximation of the result of raisingxto the power ofy.static doublerandom()Returns a pseudo-random number between 0.0 (inclusive) and 1.0 (exclusive).static doublerint(double d)Returns the double conversion of the result of rounding the argument to an integer.static longround(double d)Returns the result of rounding the argument to an integer.static intround(float f)Returns the result of rounding the argument to an integer.static doublescalb(double d, int scaleFactor)Returnsd* 2^scaleFactor.static floatscalb(float d, int scaleFactor)Returnsd* 2^scaleFactor.static doublesignum(double d)Returns the signum function of the argument.static floatsignum(float f)Returns the signum function of the argument.static doublesin(double d)Returns the closest double approximation of the sine of the argument.static doublesinh(double d)Returns the closest double approximation of the hyperbolic sine of the argument.static doublesqrt(double d)Returns the closest double approximation of the square root of the argument.static doubletan(double d)Returns the closest double approximation of the tangent of the argument.static doubletanh(double d)Returns the closest double approximation of the hyperbolic tangent of the argument.static doubletoDegrees(double angrad)Returns the measure in degrees of the supplied radian angle.static doubletoRadians(double angdeg)Returns the measure in radians of the supplied degree angle.static doubleulp(double d)Returns the argument's ulp (unit in the last place).static floatulp(float f)Returns the argument's ulp (unit in the last place).
-
Field Details
-
E
public static final double EThe double value closest to e, the base of the natural logarithm.- See Also:
- Constant Field Values
-
PI
public static final double PIThe double value closest to pi, the ratio of a circle's circumference to its diameter.- See Also:
- Constant Field Values
-
-
Method Details
-
abs
public static double abs(double d)Returns the absolute value of the argument.Special cases:
abs(-0.0) = +0.0abs(+infinity) = +infinityabs(-infinity) = +infinityabs(NaN) = NaN
-
abs
public static float abs(float f)Returns the absolute value of the argument.Special cases:
abs(-0.0) = +0.0abs(+infinity) = +infinityabs(-infinity) = +infinityabs(NaN) = NaN
-
abs
public static int abs(int i)Returns the absolute value of the argument.If the argument is
Integer.MIN_VALUE,Integer.MIN_VALUEis returned. -
abs
public static long abs(long l)Returns the absolute value of the argument.If the argument is
Long.MIN_VALUE,Long.MIN_VALUEis returned. -
acos
public static double acos(double d)Returns the closest double approximation of the arc cosine of the argument within the range[0..pi].Special cases:
acos((anything > 1) = NaNacos((anything < -1) = NaNacos(NaN) = NaN
- Parameters:
d- the value to compute arc cosine of.- Returns:
- the arc cosine of the argument.
-
asin
public static double asin(double d)Returns the closest double approximation of the arc sine of the argument within the range[-pi/2..pi/2].Special cases:
asin((anything > 1)) = NaNasin((anything < -1)) = NaNasin(NaN) = NaN
- Parameters:
d- the value whose arc sine has to be computed.- Returns:
- the arc sine of the argument.
-
atan
public static double atan(double d)Returns the closest double approximation of the arc tangent of the argument within the range[-pi/2..pi/2].Special cases:
atan(+0.0) = +0.0atan(-0.0) = -0.0atan(+infinity) = +pi/2atan(-infinity) = -pi/2atan(NaN) = NaN
- Parameters:
d- the value whose arc tangent has to be computed.- Returns:
- the arc tangent of the argument.
-
atan2
public static double atan2(double y, double x)Returns the closest double approximation of the arc tangent ofy/xwithin the range[-pi..pi]. This is the angle of the polar representation of the rectangular coordinates (x,y).Special cases:
atan2((anything), NaN ) = NaN;atan2(NaN , (anything) ) = NaN;atan2(+0.0, +(anything but NaN)) = +0.0atan2(-0.0, +(anything but NaN)) = -0.0atan2(+0.0, -(anything but NaN)) = +piatan2(-0.0, -(anything but NaN)) = -piatan2(+(anything but 0 and NaN), 0) = +pi/2atan2(-(anything but 0 and NaN), 0) = -pi/2atan2(+(anything but infinity and NaN), +infinity)=+0.0atan2(-(anything but infinity and NaN), +infinity)=-0.0atan2(+(anything but infinity and NaN), -infinity) = +piatan2(-(anything but infinity and NaN), -infinity) = -piatan2(+infinity, +infinity ) = +pi/4atan2(-infinity, +infinity ) = -pi/4atan2(+infinity, -infinity ) = +3pi/4atan2(-infinity, -infinity ) = -3pi/4atan2(+infinity, (anything but,0, NaN, and infinity))=+pi/2atan2(-infinity, (anything but,0, NaN, and infinity))=-pi/2
- Parameters:
y- the numerator of the value whose atan has to be computed.x- the denominator of the value whose atan has to be computed.- Returns:
- the arc tangent of
y/x.
-
cbrt
public static double cbrt(double d)Returns the closest double approximation of the cube root of the argument.Special cases:
cbrt(+0.0) = +0.0cbrt(-0.0) = -0.0cbrt(+infinity) = +infinitycbrt(-infinity) = -infinitycbrt(NaN) = NaN
- Parameters:
d- the value whose cube root has to be computed.- Returns:
- the cube root of the argument.
-
ceil
public static double ceil(double d)Returns the double conversion of the most negative (closest to negative infinity) integer value greater than or equal to the argument.Special cases:
ceil(+0.0) = +0.0ceil(-0.0) = -0.0ceil((anything in range (-1,0)) = -0.0ceil(+infinity) = +infinityceil(-infinity) = -infinityceil(NaN) = NaN
-
cosh
public static double cosh(double d)Returns the closest double approximation of the hyperbolic cosine of the argument.Special cases:
cosh(+infinity) = +infinitycosh(-infinity) = +infinitycosh(NaN) = NaN
- Parameters:
d- the value whose hyperbolic cosine has to be computed.- Returns:
- the hyperbolic cosine of the argument.
-
cos
public static double cos(double d)Returns the closest double approximation of the cosine of the argument.Special cases:
cos(+infinity) = NaNcos(-infinity) = NaNcos(NaN) = NaN
- Parameters:
d- the angle whose cosine has to be computed, in radians.- Returns:
- the cosine of the argument.
-
exp
public static double exp(double d)Returns the closest double approximation of the raising "e" to the power of the argument.Special cases:
exp(+infinity) = +infinityexp(-infinity) = +0.0exp(NaN) = NaN
- Parameters:
d- the value whose exponential has to be computed.- Returns:
- the exponential of the argument.
-
expm1
public static double expm1(double d)Returns the closest double approximation ofed- 1. If the argument is very close to 0, it is much more accurate to useexpm1(d)+1thanexp(d)(due to cancellation of significant digits).Special cases:
expm1(+0.0) = +0.0expm1(-0.0) = -0.0expm1(+infinity) = +infinityexpm1(-infinity) = -1.0expm1(NaN) = NaN
- Parameters:
d- the value to compute theed- 1of.- Returns:
- the
ed- 1value of the argument.
-
floor
public static double floor(double d)Returns the double conversion of the most positive (closest to positive infinity) integer less than or equal to the argument.Special cases:
floor(+0.0) = +0.0floor(-0.0) = -0.0floor(+infinity) = +infinityfloor(-infinity) = -infinityfloor(NaN) = NaN
-
hypot
public static double hypot(double x, double y)Returnssqrt(x2+y2). The final result is without medium underflow or overflow.Special cases:
hypot(+infinity, (anything including NaN)) = +infinityhypot(-infinity, (anything including NaN)) = +infinityhypot((anything including NaN), +infinity) = +infinityhypot((anything including NaN), -infinity) = +infinityhypot(NaN, NaN) = NaN
- Parameters:
x- a double number.y- a double number.- Returns:
- the
sqrt(x2+y2)value of the arguments.
-
IEEEremainder
public static double IEEEremainder(double x, double y)Returns the remainder of dividingxbyyusing the IEEE 754 rules. The result isx-round(x/p)*pwhereround(x/p)is the nearest integer (rounded to even), but without numerical cancellation problems.Special cases:
IEEEremainder((anything), 0) = NaNIEEEremainder(+infinity, (anything)) = NaNIEEEremainder(-infinity, (anything)) = NaNIEEEremainder(NaN, (anything)) = NaNIEEEremainder((anything), NaN) = NaNIEEEremainder(x, +infinity) = xwhere x is anything but +/-infinityIEEEremainder(x, -infinity) = xwhere x is anything but +/-infinity
- Parameters:
x- the numerator of the operation.y- the denominator of the operation.- Returns:
- the IEEE754 floating point reminder of of
x/y.
-
log
public static double log(double d)Returns the closest double approximation of the natural logarithm of the argument.Special cases:
log(+0.0) = -infinitylog(-0.0) = -infinitylog((anything < 0) = NaNlog(+infinity) = +infinitylog(-infinity) = NaNlog(NaN) = NaN
- Parameters:
d- the value whose log has to be computed.- Returns:
- the natural logarithm of the argument.
-
log10
public static double log10(double d)Returns the closest double approximation of the base 10 logarithm of the argument.Special cases:
log10(+0.0) = -infinitylog10(-0.0) = -infinitylog10((anything < 0) = NaNlog10(+infinity) = +infinitylog10(-infinity) = NaNlog10(NaN) = NaN
- Parameters:
d- the value whose base 10 log has to be computed.- Returns:
- the natural logarithm of the argument.
-
log1p
public static double log1p(double d)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 uselog1p(d)thanlog(1.0+d)(due to numerical cancellation).Special cases:
log1p(+0.0) = +0.0log1p(-0.0) = -0.0log1p((anything < 1)) = NaNlog1p(-1.0) = -infinitylog1p(+infinity) = +infinitylog1p(-infinity) = NaNlog1p(NaN) = NaN
- Parameters:
d- the value to compute theln(1+d)of.- Returns:
- the natural logarithm of the sum of the argument and 1.
-
max
public static double max(double d1, double d2)Returns the most positive (closest to positive infinity) of the two arguments.Special cases:
max(NaN, (anything)) = NaNmax((anything), NaN) = NaNmax(+0.0, -0.0) = +0.0max(-0.0, +0.0) = +0.0
-
max
public static float max(float f1, float f2)Returns the most positive (closest to positive infinity) of the two arguments.Special cases:
max(NaN, (anything)) = NaNmax((anything), NaN) = NaNmax(+0.0, -0.0) = +0.0max(-0.0, +0.0) = +0.0
-
max
public static int max(int i1, int i2)Returns the most positive (closest to positive infinity) of the two arguments. -
max
public static long max(long l1, long l2)Returns the most positive (closest to positive infinity) of the two arguments. -
min
public static double min(double d1, double d2)Returns the most negative (closest to negative infinity) of the two arguments.Special cases:
min(NaN, (anything)) = NaNmin((anything), NaN) = NaNmin(+0.0, -0.0) = -0.0min(-0.0, +0.0) = -0.0
-
min
public static float min(float f1, float f2)Returns the most negative (closest to negative infinity) of the two arguments.Special cases:
min(NaN, (anything)) = NaNmin((anything), NaN) = NaNmin(+0.0, -0.0) = -0.0min(-0.0, +0.0) = -0.0
-
min
public static int min(int i1, int i2)Returns the most negative (closest to negative infinity) of the two arguments. -
min
public static long min(long l1, long l2)Returns the most negative (closest to negative infinity) of the two arguments. -
pow
public static double pow(double x, double y)Returns the closest double approximation of the result of raisingxto the power ofy.Special cases:
pow((anything), +0.0) = 1.0pow((anything), -0.0) = 1.0pow(x, 1.0) = xpow((anything), NaN) = NaNpow(NaN, (anything except 0)) = NaNpow(+/-(|x| > 1), +infinity) = +infinitypow(+/-(|x| > 1), -infinity) = +0.0pow(+/-(|x| < 1), +infinity) = +0.0pow(+/-(|x| < 1), -infinity) = +infinitypow(+/-1.0 , +infinity) = NaNpow(+/-1.0 , -infinity) = NaNpow(+0.0, (+anything except 0, NaN)) = +0.0pow(-0.0, (+anything except 0, NaN, odd integer)) = +0.0pow(+0.0, (-anything except 0, NaN)) = +infinitypow(-0.0, (-anything except 0, NAN, odd integer))=+infinitypow(-0.0, (odd integer)) = -pow( +0 , (odd integer) )pow(+infinity, (+anything except 0, NaN)) = +infinitypow(+infinity, (-anything except 0, NaN)) = +0.0pow(-infinity, (anything)) = -pow(0, (-anything))pow((-anything), (integer))=pow(-1,(integer))*pow(+anything,integer)pow((-anything except 0 and infinity), (non-integer))=NAN
- Parameters:
x- the base of the operation.y- the exponent of the operation.- Returns:
xto the power ofy.
-
random
public static double random()Returns a pseudo-random number between 0.0 (inclusive) and 1.0 (exclusive).- Returns:
- a pseudo-random number.
-
rint
public static double rint(double d)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.0rint(-0.0) = -0.0rint(+infinity) = +infinityrint(-infinity) = -infinityrint(NaN) = NaN
- Parameters:
d- the value to be rounded.- Returns:
- the closest integer to the argument (as a double).
-
round
public static long round(double d)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.0round(-0.0) = +0.0round((anything > Long.MAX_VALUE) = Long.MAX_VALUEround((anything < Long.MIN_VALUE) = Long.MIN_VALUEround(+infinity) = Long.MAX_VALUEround(-infinity) = Long.MIN_VALUEround(NaN) = +0.0
- Parameters:
d- the value to be rounded.- Returns:
- the closest integer to the argument.
-
round
public static int round(float f)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.0round(-0.0) = +0.0round((anything > Integer.MAX_VALUE) = Integer.MAX_VALUEround((anything < Integer.MIN_VALUE) = Integer.MIN_VALUEround(+infinity) = Integer.MAX_VALUEround(-infinity) = Integer.MIN_VALUEround(NaN) = +0.0
- Parameters:
f- the value to be rounded.- Returns:
- the closest integer to the argument.
-
signum
public static double signum(double d)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.0signum(-0.0) = -0.0signum(+infinity) = +1.0signum(-infinity) = -1.0signum(NaN) = NaN
- Parameters:
d- the value whose signum has to be computed.- Returns:
- the value of the signum function.
-
signum
public static float signum(float f)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.0signum(-0.0) = -0.0signum(+infinity) = +1.0signum(-infinity) = -1.0signum(NaN) = NaN
- Parameters:
f- the value whose signum has to be computed.- Returns:
- the value of the signum function.
-
sinh
public static double sinh(double d)Returns the closest double approximation of the hyperbolic sine of the argument.Special cases:
sinh(+0.0) = +0.0sinh(-0.0) = -0.0sinh(+infinity) = +infinitysinh(-infinity) = -infinitysinh(NaN) = NaN
- Parameters:
d- the value whose hyperbolic sine has to be computed.- Returns:
- the hyperbolic sine of the argument.
-
sin
public static double sin(double d)Returns the closest double approximation of the sine of the argument.Special cases:
sin(+0.0) = +0.0sin(-0.0) = -0.0sin(+infinity) = NaNsin(-infinity) = NaNsin(NaN) = NaN
- Parameters:
d- the angle whose sin has to be computed, in radians.- Returns:
- the sine of the argument.
-
sqrt
public static double sqrt(double d)Returns the closest double approximation of the square root of the argument.Special cases:
sqrt(+0.0) = +0.0sqrt(-0.0) = -0.0sqrt( (anything < 0) ) = NaNsqrt(+infinity) = +infinitysqrt(NaN) = NaN
-
tan
public static double tan(double d)Returns the closest double approximation of the tangent of the argument.Special cases:
tan(+0.0) = +0.0tan(-0.0) = -0.0tan(+infinity) = NaNtan(-infinity) = NaNtan(NaN) = NaN
- Parameters:
d- the angle whose tangent has to be computed, in radians.- Returns:
- the tangent of the argument.
-
tanh
public static double tanh(double d)Returns the closest double approximation of the hyperbolic tangent of the argument. The absolute value is always less than 1.Special cases:
tanh(+0.0) = +0.0tanh(-0.0) = -0.0tanh(+infinity) = +1.0tanh(-infinity) = -1.0tanh(NaN) = NaN
- Parameters:
d- the value whose hyperbolic tangent has to be computed.- Returns:
- the hyperbolic tangent of the argument
-
toDegrees
public static double toDegrees(double angrad)Returns the measure in degrees of the supplied radian angle. The result isangrad * 180 / pi.Special cases:
toDegrees(+0.0) = +0.0toDegrees(-0.0) = -0.0toDegrees(+infinity) = +infinitytoDegrees(-infinity) = -infinitytoDegrees(NaN) = NaN
- Parameters:
angrad- an angle in radians.- Returns:
- the degree measure of the angle.
-
toRadians
public static double toRadians(double angdeg)Returns the measure in radians of the supplied degree angle. The result isangdeg / 180 * pi.Special cases:
toRadians(+0.0) = +0.0toRadians(-0.0) = -0.0toRadians(+infinity) = +infinitytoRadians(-infinity) = -infinitytoRadians(NaN) = NaN
- Parameters:
angdeg- an angle in degrees.- Returns:
- the radian measure of the angle.
-
ulp
public static double ulp(double d)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-NaNx,ulp(-x) == ulp(x).Special cases:
ulp(+0.0) = Double.MIN_VALUEulp(-0.0) = Double.MIN_VALUEulp(+infinity) = infinityulp(-infinity) = infinityulp(NaN) = NaN
- Parameters:
d- the floating-point value to compute ulp of.- Returns:
- the size of a ulp of the argument.
-
ulp
public static float ulp(float f)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-NaNx,ulp(-x) == ulp(x).Special cases:
ulp(+0.0) = Float.MIN_VALUEulp(-0.0) = Float.MIN_VALUEulp(+infinity) = infinityulp(-infinity) = infinityulp(NaN) = NaN
- Parameters:
f- the floating-point value to compute ulp of.- Returns:
- the size of a ulp of the argument.
-
copySign
public static double copySign(double magnitude, double sign)Returns a double with the given magnitude and the sign ofsign. Ifsignis NaN, the sign of the result is positive.- Since:
- 1.6
-
copySign
public static float copySign(float magnitude, float sign)Returns a float with the given magnitude and the sign ofsign. Ifsignis NaN, the sign of the result is positive.- Since:
- 1.6
-
getExponent
public static int getExponent(float f)Returns the exponent of floatf.- Since:
- 1.6
-
getExponent
public static int getExponent(double d)Returns the exponent of doubled.- Since:
- 1.6
-
nextAfter
public static double nextAfter(double start, double direction)Returns the next double afterstartin the givendirection.- Since:
- 1.6
-
nextAfter
public static float nextAfter(float start, double direction)Returns the next float afterstartin the givendirection.- Since:
- 1.6
-
nextUp
public static double nextUp(double d)Returns the next double larger thand.- Since:
- 1.6
-
nextUp
public static float nextUp(float f)Returns the next float larger thanf.- Since:
- 1.6
-
scalb
public static double scalb(double d, int scaleFactor)Returnsd* 2^scaleFactor. The result may be rounded.- Since:
- 1.6
-
scalb
public static float scalb(float d, int scaleFactor)Returnsd* 2^scaleFactor. The result may be rounded.- Since:
- 1.6
-