Top Description Inners Fields Constructors Methods
java.lang

public final Class StrictMath

extends Object
Class Inheritance
Imports
java.util.Random, jdk.internal.math.DoubleConsts, jdk.internal.vm.annotation.IntrinsicCandidate

The class StrictMath contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

To help ensure portability of Java programs, the definitions of some of the numeric functions in this package require that they produce the same results as certain published algorithms. These algorithms are available from the well-known network library netlib as the package "Freely Distributable Math Library," fdlibm. These algorithms, which are written in the C programming language, are then to be understood to be transliterated into Java and executed with all floating-point and integer operations following the rules of Java arithmetic. The following transformations are used in the transliteration:

The Java math library is defined with respect to fdlibm version 5.3. Where fdlibm provides more than one definition for a function (such as acos), use the "IEEE 754 core function" version (residing in a file whose name begins with the letter e). The methods which require fdlibm semantics are sin, cos, tan, asin, acos, atan, exp, log, log10, cbrt, atan2, pow, sinh, cosh, tanh, hypot, expm1, and log1p.

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.

IEEE 754 Recommended Operations

The Math class discusses how the shared quality of implementation criteria for selected Math and StrictMath methods relate to the IEEE 754 recommended operations.
Author
Joseph D. Darcy
Since
1.3
See Also
IEEE Standard for Floating-Point Arithmetic

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
public static final double
E

The double value that is closer than any other to e, the base of the natural logarithms.

public static final double
PI

The double value that is closer than any other to pi (π), the ratio of the circumference of a circle to its diameter.

public static final double
TAU

The double value that is closer than any other to tau (τ), the ratio of the circumference of a circle to its radius.

Constructor Summary

AccessConstructor and Description
private
StrictMath()

Don't let anyone instantiate this class.

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

the absolute value of the argument.
abs
(int
the argument whose absolute value is to be determined.
a
)

Returns the absolute value of an int value.

public static long

Returns:

the absolute value of the argument.
abs
(long
the argument whose absolute value is to be determined.
a
)

Returns the absolute value of a long value.

public static float

Returns:

the absolute value of the argument.
abs
(float
the argument whose absolute value is to be determined
a
)

Returns the absolute value of a float value.

public static double

Returns:

the absolute value of the argument.
abs
(double
the argument whose absolute value is to be determined
a
)

Returns the absolute value of a double value.

public static int

Returns:

the absolute value of the argument, unless overflow occurs
absExact
(int
the argument whose absolute value is to be determined
a
)

Returns the mathematical absolute value of an int value if it is exactly representable as an int, throwing ArithmeticException if the result overflows the positive int range.

public static long

Returns:

the absolute value of the argument, unless overflow occurs
absExact
(long
the argument whose absolute value is to be determined
a
)

Returns the mathematical absolute value of an long value if it is exactly representable as an long, throwing ArithmeticException if the result overflows the positive long range.

public static double

Returns:

the arc cosine of the argument.
acos
(double
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 result
addExact
(int
the first value
x
,
int
the second value
y
)

Returns the sum of its arguments, throwing an exception if the result overflows an int.

public static long

Returns:

the result
addExact
(long
the first value
x
,
long
the second value
y
)

Returns the sum of its arguments, throwing an exception if the result overflows a long.

public static double

Returns:

the arc sine of the argument.
asin
(double
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.
atan
(double
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 (rtheta) in polar coordinates that corresponds to the point (xy) in Cartesian coordinates.
atan2
(double
the ordinate coordinate
y
,
double
the abscissa coordinate
x
)

Returns the angle theta from the conversion of rectangular coordinates (xy) to polar coordinates (r, theta).

public static double

Returns:

the cube root of a.
cbrt
(double
a value.
a
)

Returns the cube root of a double value.

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.
ceil
(double
a value.
a
)

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

public static int

Returns:

the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.
ceilDiv
(int
the dividend
x
,
int
the divisor
y
)

Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.

public static long

Returns:

the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.
ceilDiv
(long
the dividend
x
,
int
the divisor
y
)

Returns the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.

public static long

Returns:

the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.
ceilDiv
(long
the dividend
x
,
long
the divisor
y
)

Returns the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.

public static int

Returns:

the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.
ceilDivExact
(int
the dividend
x
,
int
the divisor
y
)

Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.

public static long

Returns:

the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.
ceilDivExact
(long
the dividend
x
,
long
the divisor
y
)

Returns the smallest (closest to negative infinity) long value that is greater than or equal to the algebraic quotient.

public static int

Returns:

the ceiling modulus x - (ceilDiv(x, y) * y)
ceilMod
(int
the dividend
x
,
int
the divisor
y
)

Returns the ceiling modulus of the int arguments.

public static int

Returns:

the ceiling modulus x - (ceilDiv(x, y) * y)
ceilMod
(long
the dividend
x
,
int
the divisor
y
)

Returns the ceiling modulus of the long and int arguments.

public static long

Returns:

the ceiling modulus x - (ceilDiv(x, y) * y)
ceilMod
(long
the dividend
x
,
long
the divisor
y
)

Returns the ceiling modulus of the long arguments.

public static int

Returns:

a clamped value that fits into min..max interval
clamp
(long
value 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 into min..max interval
clamp
(long
value 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 into min..max interval
clamp
(double
value 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 into min..max interval
clamp
(float
value 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 of magnitude and the sign of sign.
copySign
(double
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 of magnitude and the sign of sign.
copySign
(float
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.
cos
(double
an angle, in radians.
a
)

Returns the trigonometric cosine of an angle.

public static double

Returns:

The hyperbolic cosine of x.
cosh
(double
The number whose hyperbolic cosine is to be returned.
x
)

Returns the hyperbolic cosine of a double value.

public static int

Returns:

the result
decrementExact
(int
the value to decrement
a
)

Returns the argument decremented by one, throwing an exception if the result overflows an int.

public static long

Returns:

the result
decrementExact
(long
the value to decrement
a
)

Returns the argument decremented by one, throwing an exception if the result overflows a long.

public static int

Returns:

the quotient x / y
divideExact
(int
the dividend
x
,
int
the divisor
y
)

Returns the quotient of the arguments, throwing an exception if the result overflows an int.

public static long

Returns:

the quotient x / y
divideExact
(long
the dividend
x
,
long
the divisor
y
)

Returns the quotient of the arguments, throwing an exception if the result overflows a long.

public static double

Returns:

the value ea, where e is the base of the natural logarithms.
exp
(double
the exponent to raise e to.
a
)

Returns Euler's number e raised to the power of a double value.

public static double

Returns:

the value ex - 1.
expm1
(double
the exponent to raise e to in the computation of ex -1.
x
)

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.
floor
(double
a value.
a
)

Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.

public static int

Returns:

the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.
floorDiv
(int
the dividend
x
,
int
the divisor
y
)

Returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.

public static long

Returns:

the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.
floorDiv
(long
the dividend
x
,
int
the divisor
y
)

Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.

public static long

Returns:

the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.
floorDiv
(long
the dividend
x
,
long
the divisor
y
)

Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.

public static int

Returns:

the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.
floorDivExact
(int
the dividend
x
,
int
the divisor
y
)

Returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.

public static long

Returns:

the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.
floorDivExact
(long
the dividend
x
,
long
the divisor
y
)

Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.

public static int

Returns:

the floor modulus x - (floorDiv(x, y) * y)
floorMod
(int
the dividend
x
,
int
the divisor
y
)

Returns the floor modulus of the int arguments.

public static int

Returns:

the floor modulus x - (floorDiv(x, y) * y)
floorMod
(long
the dividend
x
,
int
the divisor
y
)

Returns the floor modulus of the long and int arguments.

public static long

Returns:

the floor modulus x - (floorDiv(x, y) * y)
floorMod
(long
the dividend
x
,
long
the divisor
y
)

Returns the floor modulus of the long arguments.

private static double
floorOrCeil(double
the value to be floored or ceiled
a
,
double
result for values in (-1, 0)
negativeBoundary
,
double
result for values in (0, 1)
positiveBoundary
,
double
the sign of the result
sign
)

Internal method to share logic between floor and ceil.

public static double

Returns:

(a × b + c) computed, as if with unlimited range and precision, and rounded once to the nearest double value
fma
(double
a 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 double.

public static float

Returns:

(a × b + c) computed, as if with unlimited range and precision, and rounded once to the nearest float value
fma
(float
a 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 float.

public static int

Returns:

the unbiased exponent of the argument
getExponent
(float
a float value
f
)

Returns the unbiased exponent used in the representation of a float.

public static int

Returns:

the unbiased exponent of the argument
getExponent
(double
a double value
d
)

Returns the unbiased exponent used in the representation of a double.

public static double

Returns:

sqrt(x2 +y2) without intermediate overflow or underflow
hypot
(double
a value
x
,
double
a value
y
)

Returns sqrt(x2 +y2) without intermediate overflow or underflow.

public static double

Returns:

the remainder when f1 is divided by f2.
IEEEremainder
(double
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 result
incrementExact
(int
the value to increment
a
)

Returns the argument incremented by one, throwing an exception if the result overflows an int.

public static long

Returns:

the result
incrementExact
(long
the value to increment
a
)

Returns the argument incremented by one, throwing an exception if the result overflows a long.

public static double

Returns:

the value ln a, the natural logarithm of a.
log
(double
a value
a
)

Returns the natural logarithm (base e) of a double value.

public static double

Returns:

the base 10 logarithm of a.
log10
(double
a value
a
)

Returns the base 10 logarithm of a double value.

public static double

Returns:

the value ln(x + 1), the natural log of x + 1
log1p
(double
a value
x
)

Returns the natural logarithm of the sum of the argument and 1.

public static int

Returns:

the larger of a and b.
max
(int
an argument.
a
,
int
another argument.
b
)

Returns the greater of two int values.

public static long

Returns:

the larger of a and b.
max
(long
an argument.
a
,
long
another argument.
b
)

Returns the greater of two long values.

public static float

Returns:

the larger of a and b.
max
(float
an argument.
a
,
float
another argument.
b
)

Returns the greater of two float values.

public static double

Returns:

the larger of a and b.
max
(double
an argument.
a
,
double
another argument.
b
)

Returns the greater of two double values.

public static int

Returns:

the smaller of a and b.
min
(int
an argument.
a
,
int
another argument.
b
)

Returns the smaller of two int values.

public static long

Returns:

the smaller of a and b.
min
(long
an argument.
a
,
long
another argument.
b
)

Returns the smaller of two long values.

public static float

Returns:

the smaller of a and b.
min
(float
an argument.
a
,
float
another argument.
b
)

Returns the smaller of two float values.

public static double

Returns:

the smaller of a and b.
min
(double
an argument.
a
,
double
another argument.
b
)

Returns the smaller of two double values.

public static int

Returns:

the result
multiplyExact
(int
the first value
x
,
int
the second value
y
)

Returns the product of the arguments, throwing an exception if the result overflows an int.

public static long

Returns:

the result
multiplyExact
(long
the first value
x
,
int
the second value
y
)

Returns the product of the arguments, throwing an exception if the result overflows a long.

public static long

Returns:

the result
multiplyExact
(long
the first value
x
,
long
the second value
y
)

Returns the product of the arguments, throwing an exception if the result overflows a long.

public static long

Returns:

the result
multiplyFull
(int
the first value
x
,
int
the second value
y
)

Returns the exact mathematical product of the arguments.

public static long

Returns:

the result
multiplyHigh
(long
the first value
x
,
long
the second value
y
)

Returns as a long the most significant 64 bits of the 128-bit product of two 64-bit factors.

public static int

Returns:

the result
negateExact
(int
the value to negate
a
)

Returns the negation of the argument, throwing an exception if the result overflows an int.

public static long

Returns:

the result
negateExact
(long
the value to negate
a
)

Returns the negation of the argument, throwing an exception if the result overflows a long.

public static double

Returns:

The floating-point number adjacent to start in the direction of direction.
nextAfter
(double
starting floating-point value
start
,
double
value indicating which of start's neighbors or start should be returned
direction
)

Returns 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 to start in the direction of direction.
nextAfter
(float
starting floating-point value
start
,
double
value indicating which of start's neighbors or start should be returned
direction
)

Returns 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.
nextDown
(double
starting floating-point value
d
)

Returns the floating-point value adjacent to d in the direction of negative infinity.

public static float

Returns:

The adjacent floating-point value closer to negative infinity.
nextDown
(float
starting floating-point value
f
)

Returns the floating-point value adjacent to f in the direction of negative infinity.

public static double

Returns:

The adjacent floating-point value closer to positive infinity.
nextUp
(double
starting floating-point value
d
)

Returns the floating-point value adjacent to d in the direction of positive infinity.

public static float

Returns:

The adjacent floating-point value closer to positive infinity.
nextUp
(float
starting floating-point value
f
)

Returns the floating-point value adjacent to f in the direction of positive infinity.

public static double

Returns:

the value ab.
pow
(double
base.
a
,
double
the exponent.
b
)

Returns the value of the first argument raised to the power of the second argument.

public static double

Returns:

a pseudorandom double greater than or equal to 0.0 and less than 1.0.
random
()

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

public static double

Returns:

the closest floating-point value to a that is equal to a mathematical integer.
rint
(double
a value.
a
)

Returns the double value that is closest in value to the argument and is equal to a mathematical integer.

public static int

Returns:

the value of the argument rounded to the nearest int value.
round
(float
a floating-point value to be rounded to an integer.
a
)

Returns the closest int to the argument, with ties rounding to positive infinity.

public static long

Returns:

the value of the argument rounded to the nearest long value.
round
(double
a floating-point value to be rounded to a long.
a
)

Returns the closest long to the argument, with ties rounding to positive infinity.

public static double

Returns:

d × 2scaleFactor
scalb
(double
number to be scaled by a power of two.
d
,
int
power of 2 used to scale d
scaleFactor
)

Returns d × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply.

public static float

Returns:

f × 2scaleFactor
scalb
(float
number to be scaled by a power of two.
f
,
int
power of 2 used to scale f
scaleFactor
)

Returns f × 2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply.

public static double

Returns:

the signum function of the argument
signum
(double
the 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 argument
signum
(float
the 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.
sin
(double
an angle, in radians.
a
)

Returns the trigonometric sine of an angle.

public static double

Returns:

The hyperbolic sine of x.
sinh
(double
The number whose hyperbolic sine is to be returned.
x
)

Returns the hyperbolic sine of a double value.

public static double

Returns:

the positive square root of a.
sqrt
(double
a value.
a
)

Returns the correctly rounded positive square root of a double value.

public static int

Returns:

the result
subtractExact
(int
the 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 int.

public static long

Returns:

the result
subtractExact
(long
the 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 long.

public static double

Returns:

the tangent of the argument.
tan
(double
an angle, in radians.
a
)

Returns the trigonometric tangent of an angle.

public static double

Returns:

The hyperbolic tangent of x.
tanh
(double
The number whose hyperbolic tangent is to be returned.
x
)

Returns the hyperbolic tangent of a double value.

public static double

Returns:

the measurement of the angle angrad in degrees.
toDegrees
(double
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 int
toIntExact
(long
the long value
value
)

Returns the value of the long argument, throwing an exception if the value overflows an int.

public static double

Returns:

the measurement of the angle angdeg in radians.
toRadians
(double
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 argument
ulp
(double
the 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 argument
ulp
(float
the floating-point value whose ulp is to be returned
f
)

Returns the size of an ulp of the argument.

public static long

Returns:

the result
unsignedMultiplyHigh
(long
the first value
x
,
long
the second value
y
)

Returns as a long the most significant 64 bits of the unsigned 128-bit product of two unsigned 64-bit factors.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait