java.lang

Class StrictMath

public final strictfp class StrictMath extends Object

Helper class containing useful mathematical functions and constants. This class mirrors {@link 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.

Since: 1.3

Field Summary
static doubleE
The most accurate approximation to the mathematical constant e: 2.718281828459045.
static doublePI
The most accurate approximation to the mathematical constant pi: 3.141592653589793.
Method Summary
static intabs(int i)
Take the absolute value of the argument.
static longabs(long l)
Take the absolute value of the argument.
static floatabs(float f)
Take the absolute value of the argument.
static doubleabs(double d)
Take the absolute value of the argument.
static doubleacos(double x)
The trigonometric function arccos.
static doubleasin(double x)
The trigonometric function arcsin.
static doubleatan(double x)
The trigonometric function arcsin.
static doubleatan2(double y, double x)
A special version of the trigonometric function arctan, for converting rectangular coordinates (x, y) to polar (r, theta).
static doublecbrt(double x)
Returns the cube root of x.
static doubleceil(double a)
Take the nearest integer that is that is greater than or equal to the argument.
static doublecos(double a)
The trigonometric function cos.
static doublecosh(double x)
Returns the hyperbolic cosine of x, which is defined as (exp(x) + exp(-x)) / 2.
static doubleexp(double x)
Take ea.
static doubleexpm1(double x)
Returns ex - 1.
static doublefloor(double a)
Take the nearest integer that is that is less than or equal to the argument.
static doubleIEEEremainder(double x, double y)
Get the IEEE 754 floating point remainder on two numbers.
static doublelog(double x)
Take ln(a) (the natural log).
static intmax(int a, int b)
Return whichever argument is larger.
static longmax(long a, long b)
Return whichever argument is larger.
static floatmax(float a, float b)
Return whichever argument is larger.
static doublemax(double a, double b)
Return whichever argument is larger.
static intmin(int a, int b)
Return whichever argument is smaller.
static longmin(long a, long b)
Return whichever argument is smaller.
static floatmin(float a, float b)
Return whichever argument is smaller.
static doublemin(double a, double b)
Return whichever argument is smaller.
static doublepow(double x, double y)
Raise a number to a power.
static doublerandom()
Get a random number.
static doublerint(double a)
Take the nearest integer to the argument.
static intround(float f)
Take the nearest integer to the argument.
static longround(double d)
Take the nearest long to the argument.
static doublesignum(double a)

Returns the sign of the argument as follows:

  • If a is greater than zero, the result is 1.0.
  • If a is less than zero, the result is -1.0.
  • If a is NaN, the result is NaN.
static floatsignum(float a)

Returns the sign of the argument as follows:

  • If a is greater than zero, the result is 1.0f.
  • If a is less than zero, the result is -1.0f.
  • If a is NaN, the result is NaN.
static doublesin(double a)
The trigonometric function sin.
static doublesinh(double x)
Returns the hyperbolic sine of x which is defined as (exp(x) - exp(-x)) / 2.
static doublesqrt(double x)
Take a square root.
static doubletan(double a)
The trigonometric function tan.
static doubletanh(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 doubletoDegrees(double rads)
Convert from radians to degrees.
static doubletoRadians(double degrees)
Convert from degrees to radians.
static doubleulp(double d)
Return the ulp for the given double argument.
static floatulp(float f)
Return the ulp for the given float argument.

Field Detail

E

public static final double E
The most accurate approximation to the mathematical constant e: 2.718281828459045. Used in natural log and exp.

See Also: StrictMath StrictMath

PI

public static final double PI
The most accurate approximation to the mathematical constant pi: 3.141592653589793. This is the ratio of a circle's diameter to its circumference.

Method Detail

abs

public static int abs(int i)
Take the absolute value of the argument. (Absolute value means make it positive.)

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.

Parameters: i the number to take the absolute value of

Returns: the absolute value

See Also: MIN_VALUE

abs

public static long abs(long l)
Take the absolute value of the argument. (Absolute value means make it positive.)

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.

Parameters: l the number to take the absolute value of

Returns: the absolute value

See Also: MIN_VALUE

abs

public static float abs(float f)
Take the absolute value of the argument. (Absolute value means make it positive.)

Parameters: f the number to take the absolute value of

Returns: the absolute value

abs

public static double abs(double d)
Take the absolute value of the argument. (Absolute value means make it positive.)

Parameters: d the number to take the absolute value of

Returns: the absolute value

acos

public static double acos(double x)
The trigonometric function arccos. The range of angles returned is 0 to pi radians (0 to 180 degrees). If the argument is NaN or its absolute value is beyond 1, the result is NaN.

Parameters: x the cos to turn back into an angle

Returns: arccos(x)

asin

public static double asin(double x)
The trigonometric function arcsin. The range of angles returned is -pi/2 to pi/2 radians (-90 to 90 degrees). If the argument is NaN or its absolute value is beyond 1, the result is NaN; and the arcsine of 0 retains its sign.

Parameters: x the sin to turn back into an angle

Returns: arcsin(x)

atan

public static double atan(double x)
The trigonometric function arcsin. The range of angles returned is -pi/2 to pi/2 radians (-90 to 90 degrees). If the argument is NaN, the result is NaN; and the arctangent of 0 retains its sign.

Parameters: x the tan to turn back into an angle

Returns: arcsin(x)

See Also: StrictMath

atan2

public 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). This computes the arctangent of x/y in the range of -pi to pi radians (-180 to 180 degrees). Special cases:

This returns theta, the angle of the point. To get r, albeit slightly inaccurately, use sqrt(x*x+y*y).

Parameters: y the y position x the x position

Returns: theta in the conversion of (x, y) to (r, theta)

See Also: StrictMath

cbrt

public static double cbrt(double x)
Returns the cube root of x. The sign of the cube root is equal to the sign of x. Special cases:

Parameters: x the number to take the cube root of

Returns: the cube root of x

Since: 1.5

See Also:

ceil

public static double ceil(double a)
Take the nearest integer that is that is greater than or equal to the argument. If the argument is NaN, infinite, or zero, the result is the same; if the argument is between -1 and 0, the result is negative zero. Note that Math.ceil(x) == -Math.floor(-x).

Parameters: a the value to act upon

Returns: the nearest integer >= a

cos

public static double cos(double a)
The trigonometric function cos. The cosine of NaN or infinity is NaN.

Parameters: a the angle (in radians).

Returns: cos(a).

cosh

public static double cosh(double x)
Returns the hyperbolic cosine of x, which is defined as (exp(x) + exp(-x)) / 2. Special cases:

Parameters: x the argument to cosh

Returns: the hyperbolic cosine of x

Since: 1.5

exp

public static double exp(double x)
Take ea. The opposite of 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.

Parameters: x the number to raise to the power

Returns: the number raised to the power of e

See Also: StrictMath StrictMath

expm1

public static double expm1(double x)
Returns ex - 1. Special cases:

Parameters: x the argument to ex - 1.

Returns: e raised to the power x minus one.

See Also: StrictMath

floor

public static double floor(double a)
Take the nearest integer that is that is less than or equal to the argument. If the argument is NaN, infinite, or zero, the result is the same. Note that Math.ceil(x) == -Math.floor(-x).

Parameters: a the value to act upon

Returns: the nearest integer <= a

IEEEremainder

public static double IEEEremainder(double x, double y)
Get the IEEE 754 floating point remainder on two numbers. This is the value of 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.

Parameters: x the dividend (the top half) y the divisor (the bottom half)

Returns: the IEEE 754-defined floating point remainder of x/y

See Also: StrictMath

log

public static double log(double x)
Take ln(a) (the natural log). The opposite of 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).

Parameters: x the number to take the natural log of

Returns: the natural log of a

See Also: StrictMath

max

public static int max(int a, int b)
Return whichever argument is larger.

Parameters: a the first number b a second number

Returns: the larger of the two numbers

max

public static long max(long a, long b)
Return whichever argument is larger.

Parameters: a the first number b a second number

Returns: the larger of the two numbers

max

public static float max(float a, float b)
Return whichever argument is larger. If either argument is NaN, the result is NaN, and when comparing 0 and -0, 0 is always larger.

Parameters: a the first number b a second number

Returns: the larger of the two numbers

max

public static double max(double a, double b)
Return whichever argument is larger. If either argument is NaN, the result is NaN, and when comparing 0 and -0, 0 is always larger.

Parameters: a the first number b a second number

Returns: the larger of the two numbers

min

public static int min(int a, int b)
Return whichever argument is smaller.

Parameters: a the first number b a second number

Returns: the smaller of the two numbers

min

public static long min(long a, long b)
Return whichever argument is smaller.

Parameters: a the first number b a second number

Returns: the smaller of the two numbers

min

public static float min(float a, float b)
Return whichever argument is smaller. If either argument is NaN, the result is NaN, and when comparing 0 and -0, -0 is always smaller.

Parameters: a the first number b a second number

Returns: the smaller of the two numbers

min

public static double min(double a, double b)
Return whichever argument is smaller. If either argument is NaN, the result is NaN, and when comparing 0 and -0, -0 is always smaller.

Parameters: a the first number b a second number

Returns: the smaller of the two numbers

pow

public static double pow(double x, double y)
Raise a number to a power. Special cases:

(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 {@link #ceil(double)} or, equivalently, a fixed point of the method {@link #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.)

Parameters: x the number to raise y the power to raise it to

Returns: xy

random

public static double random()
Get a random number. This behaves like Random.nextDouble(), seeded by System.currentTimeMillis() when first called. In other words, the number is from a pseudorandom sequence, and lies in the range [+0.0, 1.0). This random sequence is only used by this method, and is threadsafe, although you may want your own random number generator if it is shared among threads.

Returns: a random number

See Also: nextDouble currentTimeMillis

rint

public static double rint(double a)
Take the nearest integer to the argument. If it is exactly between two integers, the even integer is taken. If the argument is NaN, infinite, or zero, the result is the same.

Parameters: a the value to act upon

Returns: the nearest integer to a

round

public static int round(float f)
Take the nearest integer to the argument. This is equivalent to (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.

Parameters: f the argument to round

Returns: the nearest integer to the argument

See Also: MIN_VALUE MAX_VALUE

round

public static long round(double d)
Take the nearest long to the argument. This is equivalent to (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.

Parameters: d the argument to round

Returns: the nearest long to the argument

See Also: MIN_VALUE MAX_VALUE

signum

public static double signum(double a)

Returns the sign of the argument as follows:

Parameters: a the numeric argument.

Returns: the sign of the argument.

Since: 1.5.

signum

public static float signum(float a)

Returns the sign of the argument as follows:

Parameters: a the numeric argument.

Returns: the sign of the argument.

Since: 1.5.

sin

public static double sin(double a)
The trigonometric function sin. The sine of NaN or infinity is NaN, and the sine of 0 retains its sign.

Parameters: a the angle (in radians)

Returns: sin(a)

sinh

public static double sinh(double x)
Returns the hyperbolic sine of x which is defined as (exp(x) - exp(-x)) / 2. Special cases:

Parameters: x the argument to sinh

Returns: the hyperbolic sine of x

Since: 1.5

sqrt

public static double sqrt(double x)
Take a square root. 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 result is either zero, the result is the same.

For other roots, use pow(x, 1/rootNumber).

Parameters: x the numeric argument

Returns: the square root of the argument

See Also: StrictMath

tan

public static double tan(double a)
The trigonometric function tan. The tangent of NaN or infinity is NaN, and the tangent of 0 retains its sign.

Parameters: a the angle (in radians)

Returns: tan(a)

tanh

public 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). Special cases:

Parameters: x the argument to tanh

Returns: the hyperbolic tagent of x

Since: 1.5

toDegrees

public static double toDegrees(double rads)
Convert from radians to degrees. The formula for this is degrees = radians * (180/pi); however it is not always exact given the limitations of floating point numbers.

Parameters: rads an angle in radians

Returns: the angle in degrees

toRadians

public static double toRadians(double degrees)
Convert from degrees to radians. The formula for this is radians = degrees * (pi/180); however it is not always exact given the limitations of floating point numbers.

Parameters: degrees an angle in degrees

Returns: the angle in radians

ulp

public static double ulp(double d)
Return the ulp for the given double argument. The ulp is the difference between the argument and the next larger double. Note that the sign of the double argument is ignored, that is, ulp(x) == ulp(-x). If the argument is a NaN, then NaN is returned. If the argument is an infinity, then +Inf is returned. If the argument is zero (either positive or negative), then {@link Double#MIN_VALUE} is returned.

Parameters: d the double whose ulp should be returned

Returns: the difference between the argument and the next larger double

Since: 1.5

ulp

public static float ulp(float f)
Return the ulp for the given float argument. The ulp is the difference between the argument and the next larger float. Note that the sign of the float argument is ignored, that is, ulp(x) == ulp(-x). If the argument is a NaN, then NaN is returned. If the argument is an infinity, then +Inf is returned. If the argument is zero (either positive or negative), then {@link Float#MIN_VALUE} is returned.

Parameters: f the float whose ulp should be returned

Returns: the difference between the argument and the next larger float

Since: 1.5