## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

### 2.8. Mathematical Functions and Constants

Sooner or later you are likely to need mathematical functions in your programs, even if it's only to obtain an absolute value or calculate a square root. Java provides a range of methods that support such functions as part of the standard library that is stored in the package java.lang, and all these are available in your program automatically.

The methods that support various additional mathematical functions are implemented in the Math class as static methods, so to reference a particular function you can just write Math and the name of the method you wish to use separated by a period. For example, the sqrt() method calculates the square root of whatever you place between the parentheses. To use the sqrt() method to produce the square root of the floating-point value that you've stored in a variable, aNumber, you would write Math.sqrt(aNumber).

The class Math includes a range of methods for standard trigonometric functions:

MethodFunctionArgument TypeResult Type
sin(arg)sine of the argumentdouble in radiansdouble
cos(arg)cosine of the argumentdouble in radiansdouble
tan(arg)tangent of the argumentdouble in radiansdouble
asin(arg)sin−1 (arc sine) of the argumentdoubledouble in radians, with values from −π/2 to π/2.
acos(arg)cos−1 (arc cosine) of the argumentdoubledouble in radians, with values from 0.0 to π.
atan(arg)tan−1 (arc tangent) of the argumentdoubledouble in radians, with values from −π/2 to π/2.
atan2 (arg1,arg2)tan−1 (arc tangent) ...

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required