Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.lang

Class Math

java.lang.Object
|
+--java.lang.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:Authors:

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 doubleIEEEremainder(double x, double y)

Get the IEEE 754 floating point remainder on two numbers.
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 a)

The trigonometric function arccos.
static doubleasin(double a)

The trigonometric function arcsin.
static doubleatan(double a)

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 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 doubleexp(double a)

Take ea.
static doublefloor(double a)

Take the nearest integer that is that is less than or equal to the argument.
static doublelog(double a)

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 a, double b)

Raise a number to a power.
static synchronized doublerandom()

Get a random number.
static doublerint(double a)

Take the nearest integer to the argument.
static intround(float a)

Take the nearest integer to the argument.
static longround(double a)

Take the nearest long to the argument.
static doublesin(double a)

The trigonometric function sin.
static doublesqrt(double a)

Take a square root.
static doubletan(double a)

The trigonometric function tan.
static doubletoDegrees(double rads)

Convert from radians to degrees.
static doubletoRadians(double degrees)

Convert from degrees to radians.

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.

See Also:


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 Details

IEEEremainder

public native 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 infinte, the result is x. This is accurate within the limits of doubles.

Parameters:

Returns:

See Also:


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:

Returns:


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:

Returns:


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:

Returns:

See Also:


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:

Returns:

See Also:


acos

public native 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:

Returns:


asin

public native 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:

Returns:


atan

public native 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:

Returns:

See Also:


atan2

public native 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 is accurate within 2 ulps, and is semi-monotonic. To get r, use sqrt(x*x+y*y).

Parameters:

Returns:

See Also:


ceil

public native 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:

Returns:


cos

public native 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:

Returns:


exp

public native 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:

Returns:

See Also:


floor

public native 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:

Returns:


log

public native 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:

Returns:

See Also:


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:

Returns:


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:

Returns:


max

public static int max(int a, int b)

Return whichever argument is larger.

Parameters:

Returns:


max

public static long max(long a, long b)

Return whichever argument is larger.

Parameters:

Returns:


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:

Returns:


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:

Returns:


min

public static int min(int a, int b)

Return whichever argument is smaller.

Parameters:

Returns:


min

public static long min(long a, long b)

Return whichever argument is smaller.

Parameters:

Returns:


pow

public native static double pow(double a, double b)

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

Returns:


random

public static synchronized 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:

See Also:


rint

public native 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:

Returns:


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:

Returns:

See Also:


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:

Returns:

See Also:


sin

public native 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:

Returns:


sqrt

public native 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:

Returns:

See Also:


tan

public native 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:

Returns:


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.

Since:Parameters:

Returns:


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.

Since:Parameters:

Returns: