java.lang

Class Math


public final class Math
extends Object

Helper class containing useful mathematical functions and constants.

Note that angles are specified in radians. Conversion functions are provided for your convenience.

Since:
1.0

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
IEEEremainder(double x, double y)
Get the IEEE 754 floating point remainder on two numbers.
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
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.
  • If a is less than zero, the result is -1.0.
  • If a is NaN, the result is NaN.
static float
signum(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 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, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

E

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

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.
Field Value:
3.0

Method Details

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. This is accurate within the limits of doubles.
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:
rint(double)

abs

public static double abs(double d)
Take the absolute value of the argument. (Absolute value means make it positive.) This is equivalent, but faster than, calling Double.longBitsToDouble(Double.doubleToLongBits(a) << 1) >>> 1);.
Parameters:
d - the number to take the absolute value of
Returns:
the absolute value

abs

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

This is equivalent, but faster than, calling Float.intBitsToFloat(0x7fffffff & Float.floatToIntBits(a)).

Parameters:
f - the number to take the absolute value of
Returns:
the absolute value

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

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

acos

public static double acos(double a)
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. This is accurate within 1 ulp, and is semi-monotonic.
Parameters:
a - the cos to turn back into an angle
Returns:
arccos(a)

asin

public static double asin(double a)
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. This is accurate within 1 ulp, and is semi-monotonic.
Parameters:
a - the sin to turn back into an angle
Returns:
arcsin(a)

atan

public static double atan(double a)
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. This is accurate within 1 ulp, and is semi-monotonic.
Parameters:
a - the tan to turn back into an angle
Returns:
arcsin(a)

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:
  • If either argument is NaN, the result is NaN.
  • If the first argument is positive zero and the second argument is positive, or the first argument is positive and finite and the second argument is positive infinity, then the result is positive zero.
  • If the first argument is negative zero and the second argument is positive, or the first argument is negative and finite and the second argument is positive infinity, then the result is negative zero.
  • If the first argument is positive zero and the second argument is negative, or the first argument is positive and finite and the second argument is negative infinity, then the result is the double value closest to pi.
  • If the first argument is negative zero and the second argument is negative, or the first argument is negative and finite and the second argument is negative infinity, then the result is the double value closest to -pi.
  • If the first argument is positive and the second argument is positive zero or negative zero, or the first argument is positive infinity and the second argument is finite, then the result is the double value closest to pi/2.
  • If the first argument is negative and the second argument is positive zero or negative zero, or the first argument is negative infinity and the second argument is finite, then the result is the double value closest to -pi/2.
  • If both arguments are positive infinity, then the result is the double value closest to pi/4.
  • If the first argument is positive infinity and the second argument is negative infinity, then the result is the double value closest to 3*pi/4.
  • If the first argument is negative infinity and the second argument is positive infinity, then the result is the double value closest to -pi/4.
  • If both arguments are negative infinity, then the result is the double value closest to -3*pi/4.

This is accurate within 2 ulps, and is semi-monotonic. To get r, 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:
atan(double)

cbrt

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).

Parameters:
a - the numeric argument
Returns:
the cube root of the argument
Since:
1.5

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. This is accurate within 1 ulp, and is semi-monotonic.
Parameters:
a - the angle (in radians)
Returns:
cos(a)

cosh

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.

Parameters:
a - the numeric argument
Returns:
the hyperbolic cosine of a.
Since:
1.5

exp

public static double exp(double a)
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. This is accurate within 1 ulp, and is semi-monotonic.
Parameters:
a - the number to raise to the power
Returns:
the number raised to the power of e

expm1

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).

Parameters:
a - the numeric argument
Returns:
ea - 1
Since:
1.5

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

hypot

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.

Parameters:
a - the first parameter.
b - the second parameter.
Returns:
the hypotenuse matching the supplied parameters.
Since:
1.5

log

public static double log(double a)
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. 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).

Parameters:
a - the number to take the natural log of
Returns:
the natural log of a
See Also:
exp(double)

log10

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.

Parameters:
a - the numeric argument.
Returns:
the base 10 logarithm of a.
Since:
1.5

log1p

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.

Parameters:
a - the numeric argument.
Returns:
the natural logarithm of a + 1.
Since:
1.5

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

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 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

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

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 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

pow

public static double pow(double a,
                         double b)
Raise a number to a power. Special cases:
  • If the second argument is positive or negative zero, then the result is 1.0.
  • If the second argument is 1.0, then the result is the same as the first argument.
  • If the second argument is NaN, then the result is NaN.
  • If the first argument is NaN and the second argument is nonzero, then the result is NaN.
  • If the absolute value of the first argument is greater than 1 and the second argument is positive infinity, or the absolute value of the first argument is less than 1 and the second argument is negative infinity, then the result is positive infinity.
  • If the absolute value of the first argument is greater than 1 and the second argument is negative infinity, or the absolute value of the first argument is less than 1 and the second argument is positive infinity, then the result is positive zero.
  • If the absolute value of the first argument equals 1 and the second argument is infinite, then the result is NaN.
  • If the first argument is positive zero and the second argument is greater than zero, or the first argument is positive infinity and the second argument is less than zero, then the result is positive zero.
  • If the first argument is positive zero and the second argument is less than zero, or the first argument is positive infinity and the second argument is greater than zero, then the result is positive infinity.
  • If the first argument is negative zero and the second argument is greater than zero but not a finite odd integer, or the first argument is negative infinity and the second argument is less than zero but not a finite odd integer, then the result is positive zero.
  • If the first argument is negative zero and the second argument is a positive finite odd integer, or the first argument is negative infinity and the second argument is a negative finite odd integer, then the result is negative zero.
  • If the first argument is negative zero and the second argument is less than zero but not a finite odd integer, or the first argument is negative infinity and the second argument is greater than zero but not a finite odd integer, then the result is positive infinity.
  • If the first argument is negative zero and the second argument is a negative finite odd integer, or the first argument is negative infinity and the second argument is a positive finite odd integer, then the result is negative infinity.
  • If the first argument is less than zero and the second argument is a finite even integer, then the result is equal to the result of raising the absolute value of the first argument to the power of the second argument.
  • If the first argument is less than zero and the second argument is a finite odd integer, then the result is equal to the negative of the result of raising the absolute value of the first argument to the power of the second argument.
  • If the first argument is finite and less than zero and the second argument is finite and not an integer, then the result is NaN.
  • If both arguments are integers, then the result is exactly equal to the mathematical result of raising the first argument to the power of the second argument if that result can in fact be represented exactly as a double value.

(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.

Parameters:
a - the number to raise
b - the power to raise it to
Returns:
ab

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

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 long round(double a)
Take the nearest long to the argument. This is equivalent to (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.
Parameters:
a - the argument to round
Returns:
the nearest long to the argument

round

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

signum

public static double signum(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.
  • If a is positive or negative zero, the result is the same.
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:
  • 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.
  • If a is positive or negative zero, the result is the same.
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. This is accurate within 1 ulp, and is semi-monotonic.
Parameters:
a - the angle (in radians)
Returns:
sin(a)

sinh

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.

Parameters:
a - the numeric argument
Returns:
the hyperbolic sine of a.
Since:
1.5

sqrt

public static double sqrt(double a)
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. This is accurate within the limits of doubles.

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

Parameters:
a - the numeric argument
Returns:
the square root of the argument

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. This is accurate within 1 ulp, and is semi-monotonic.
Parameters:
a - the angle (in radians)
Returns:
tan(a)

tanh

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.

Parameters:
a - the numeric argument
Returns:
the hyperbolic tangent of a.
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
Since:
1.2

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
Since:
1.2

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 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 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

java.lang.Math -- common mathematical functions, native allowed Copyright (C) 1998, 2001, 2002, 2003, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.