Unit 3: Math
Table of Contents
What is the Math class?
The Math class is a built-in class in Java that allows you to perform more advanced calculations. You do not need to import this class (unlike Scanner, for example).
However, you will need to prepend all method calls with Math. (dot notation). For example, to use the method max(a, b), you would do Math.max(a, b). The same goes for accessing constants in the Math class.
Math constants
| Constant (symbol) | Syntax | Value |
|---|---|---|
| pi (π) | Math.PI | 3.1415926... |
| Euler’s number (e) | Math.E | 2.71828... |
Math methods
Note that for brevity the Math. is omitted from the method name.
| Method | Description |
|---|---|
max(a, b) | Returns the maximum of a and b |
min(a, b) | Returns the minimum of a and b |
pow(a, b) | Returns a b |
exp(x) | Returns e x |
log(x) | Returns ln x |
log10(x) | Returns log 10 x |
sqrt(x) | Returns √x |
toRadians(deg) | Returns the result of converting deg to radians |
toDegrees(rad) | Returns the result of converting rad to degrees |
sin(x) | Returns sin x (Note that x is in radians) |
cos(x) | Returns cos x (Note that x is in radians) |
tan(x) | Returns tan x (Note that x is in radians) |
asin(x) | Returns arcsin x (Note that x is in radians) |
acos(x) | Returns arccos x (Note that x is in radians) |
atan(x) | Returns arctan x (Note that x is in radians) |
abs(x) | Returns |x | |
floor(x) | Returns the floor of x |
ceil(x) | Returns the ceiling of x |
round(x) | Returns x rounded to the nearest unit |