Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Fall 2014
The java.lang.Math Library Class
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

See the full documentation at http://java.sun.com/javase/7/docs/api/java/lang/Math.html. You'll find the following useful in general:

# Constants

• double PI

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

• double E

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

# Methods

## General-purpose

• double abs(double a)

Parameters: a - the argument whose absolute value is to be determined

Returns:  the absolute value of a.

Note: Also defined for int

• double random()

Parameters: none.

Returns: a pseudo-randomly generated value, greater than or equal to 0.0 and less than 1.0.

• double exp(double a)

Parameters: a - a value.

Returns: exponential: ea.

• double log(double a)

Parameters: a - a value.

Returns: the natural logarithm of a: logea

• double pow(double a, double b)

Parameters: a, b - values.

Returns: ab.

• double sqrt(double a)

Parameters: a - a value.

Returns: the positive square root of a. If the argument is NaN or less than zero, the result is NaN.

## Trigonometric

• double cos(double a)

Parameters: a - an angle, in radians.

Returns: the cosine of the argument.

• double sin(double a)

Parameters: a - an angle, in radians.

Returns: the sine of the argument.

• double tan(double a)

Parameters: a - an angle, in radians.

Returns: the tangent of the argument.

• double acos(double a)

Parameters: a - an angle, in radians.

Returns:  the arc cosine of the argument; the returned angle is in the range 0.0 through pi.

• double asin(double a)

Parameters: a - an angle, in radians.

Returns:  the arc sine of the argument; the returned angle is in the range -pi/2 through pi/2.

• double atan(double a)

Parameters: a - an angle, in radians.

Returns:  the arc tangent of the argument; the returned angle is in the range -pi/2 through pi/2.

Converts an angle measured in degrees to an approximately equivalent angle measured in radians. The conversion from degrees to radians is generally inexact.

Parameters: d: an angle, in degrees

Returns: the measurement of the angle d, in radians.

• double toDegrees(double d)

Converts an angle measured in radians to an approximately equivalent angle measured in degrees. The conversion from radians to degrees is generally inexact; users should not expect cos(toRadians(90.0)) to exactly equal 0.0.

Parameters: d: an angle, in degrees

Returns: the measurement of the angle d, in radians.

John H. E. Lasseter