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

Method | Function | Argument Type | Result Type |
---|---|---|---|

sin(arg) | sine of the argument | double in radians | double |

cos(arg) | cosine of the argument | double in radians | double |

tan(arg) | tangent of the argument | double in radians | double |

asin(arg) | sin^{−1} (arc sine) of the argument | double | double in radians, with values from −π/2 to π/2. |

acos(arg) | cos^{−1} (arc cosine) of the argument | double | double in radians, with values from 0.0 to π. |

atan(arg) | tan^{−1} (arc tangent) of the argument | double | double in radians, with values from −π/2 to π/2. |

atan2 (arg1,arg2) | tan^{−1} (arc tangent) ... |

Get *Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition* now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.