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.


Field Summary
static double E
          The mathematical constant e.
static double NaN
           
static double NEGATIVE_INFINITY
           
static double PI
          The mathematical constant pi.
static double POSITIVE_INFINITY
           
 
Constructor Summary
Math()
           
 
Method Summary
static double abs(double a)
          Take the absolute value of the argument.
static int abs(int a)
          Take the absolute value of the argument.
static long abs(long a)
          Take the absolute value of the argument.
static double acos(double x)
          The trigonometric function arccos.
static double asin(double x)
          The trigonometric function arcsin.
static double atan(double x)
          The trigonometric function arctan.
static double atan2(double y, double x)
          A special version of the trigonometric function arctan.
static double ceil(double x)
          Take the nearest integer that is that is greater than or equal to the argument.
static double cos(double x)
          The trigonometric function cos.
static double exp(double x)
          Take ex.
static double floor(double x)
          Take the nearest integer that is that is less than or equal to the argument.
static double IEEEremainder(double x, double y)
          Get the floating point remainder on two numbers, which really does the following: Takes x/y and finds the nearest integer n to the quotient.
static boolean isNaN(double d)
           
static double log(double x)
          Take ln(x) (the natural log).
static double max(double a, double 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 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 x, double y)
          Take a number to a power.
static double rint(double x)
          Take the nearest integer to the argument.
static long round(double a)
          Take the nearest integer to the argument.
static double sin(double x)
          The trigonometric function sin.
static double sqrt(double x)
          Take a square root.
static double tan(double x)
          The trigonometric function tan.
static double toDegrees(double rads)
          Convert from radians to degrees.
static double toRadians(double degrees)
          Convert from degrees to radians.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, toString, wait, wait
 

Field Detail

NaN

public static final double NaN

POSITIVE_INFINITY

public static final double POSITIVE_INFINITY

NEGATIVE_INFINITY

public static final double NEGATIVE_INFINITY

E

public static final double E
The mathematical constant e. Used in natural log and exp.
See Also:
log(double), exp(double)

PI

public static final double PI
The mathematical constant pi. This is the ratio of a circle's diameter to its circumference.
Constructor Detail

Math

public Math()
Method Detail

abs

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

Note that 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:
a - the number to take the absolute value of.
Returns:
the absolute value.

abs

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

Note that 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:
a - the number to take the absolute value of.
Returns:
the absolute value.

abs

public static double abs(double a)
Take the absolute value of the argument. (Absolute value means make it positive.)
Parameters:
a - the number to take the absolute value of.
Returns:
the absolute value.

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 double min(double a,
                         double b)
Return whichever argument is smaller.
Parameters:
a - the first number
b - a second number
Returns:
the smaller 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.

max

public static double max(double a,
                         double b)
Return whichever argument is larger.
Parameters:
a - the first number
b - a second number
Returns:
the larger of the two numbers.

sin

public static double sin(double x)
The trigonometric function sin.
Parameters:
x - the angle (in radians).
Returns:
sin(x).

cos

public static double cos(double x)
The trigonometric function cos.
Parameters:
x - the angle (in radians).
Returns:
cos(x).

tan

public static double tan(double x)
The trigonometric function tan.
Parameters:
x - the angle (in radians).
Returns:
tan(x).

asin

public static double asin(double x)
The trigonometric function arcsin. The range of angles you will get are from -pi/2 to pi/2 radians (-90 to 90 degrees)
Parameters:
x - the sin to turn back into an angle.
Returns:
arcsin(x).

acos

public static double acos(double x)
The trigonometric function arccos. The range of angles you will get are from 0 to pi radians (0 to 180 degrees).
Parameters:
x - the cos to turn back into an angle.
Returns:
arccos(x).

atan

public static double atan(double x)
The trigonometric function arctan. The range of angles you will get are from -pi/2 to pi/2 radians (-90 to 90 degrees)
Parameters:
x - the tan to turn back into an angle.
Returns:
arctan(x).
See Also:
atan2(double,double)

atan2

public static double atan2(double y,
                           double x)
A special version of the trigonometric function arctan. Given a position (x,y), this function will give you the angle of that position. The range of angles you will get are from -pi to pi radians (-180 to 180 degrees), the whole spectrum of angles. That is what makes this function so much more useful than the other atan().
Parameters:
y - the y position
x - the x position
Returns:
arctan(y/x).
See Also:
atan(double)

exp

public static double exp(double x)
Take ex. The opposite of log().
Parameters:
x - the number to raise to the power.
Returns:
the number raised to the power of e.
See Also:
log(double), pow(double,double)

log

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

sqrt

public static double sqrt(double x)
Take a square root. For other roots, to pow(x,1/rootNumber).
Parameters:
x - the numeric argument
Returns:
the square root of the argument.
See Also:
pow(double,double)

pow

public static double pow(double x,
                         double y)
Take a number to a power.
Parameters:
x - the number to raise.
y - the power to raise it to.
Returns:
xy.

IEEEremainder

public static double IEEEremainder(double x,
                                   double y)
Get the floating point remainder on two numbers, which really does the following:

  1. Takes x/y and finds the nearest integer n to the quotient. (Uses the rint() function to do this.
  2. Takes x - y*n.
  3. If x = y*n, then the result is 0 if x is positive and -0 if x is negative.
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)

ceil

public static double ceil(double x)
Take the nearest integer that is that is greater than or equal to the argument.
Parameters:
x - the value to act upon.
Returns:
the nearest integer >= x.

floor

public static double floor(double x)
Take the nearest integer that is that is less than or equal to the argument.
Parameters:
x - the value to act upon.
Returns:
the nearest integer <= x.

rint

public static double rint(double x)
Take the nearest integer to the argument. If it is exactly between two integers, the even integer is taken.
Parameters:
x - the value to act upon.
Returns:
the nearest integer to x.

round

public static long round(double a)
Take the nearest integer to the argument. If it is exactly between two integers, then the lower of the two (-10 lower than -9) is taken. If the argument is less than Long.MIN_VALUE or negative infinity, Long.MIN_VALUE will be returned. If the argument is greater than Long.MAX_VALUE, Long.MAX_VALUE will be returned.
Parameters:
a - the argument to round.
Returns:
the nearest integer to the argument.

toRadians

public static double toRadians(double degrees)
Convert from degrees to radians. The formula for this is radians = degrees * (pi/180).
Parameters:
degrees - an angle in degrees
Returns:
the angle in radians

toDegrees

public static double toDegrees(double rads)
Convert from radians to degrees. The formula for this is degrees = radians * (180/pi).
Parameters:
rads - an angle in radians
Returns:
the angle in degrees

isNaN

public static boolean isNaN(double d)