Math Class
Package: java.lang
Provides built-in methods that perform a wide variety of mathematical calculations, including
Basic functions, such as calculating an absolute value or a square root
Trigonometry functions, such as sin and cos
Practical functions, such as rounding numbers or generating random numbers
Fields
The Math class provides to static fields that define frequently used mathematical constants.
|
Field |
What It Is |
Value |
|
|
The base of natural logarithms |
2.718281828459045 |
|
|
The constant pi (π), the ratio of a circle’s radius and diameter |
3.141592653589793 |
Note that these constants are only approximate values because both π and e are irrational numbers.
Here’s an example of a statement that calculates the circumference of a circle:
double diameter = 4;
double circumference = Math.PI * diameter;
Functions
All the methods of the Math class are declared as static methods.
|
Method |
Explanation |
|
|
Returns the absolute value of the argument. The argument can be an |
|
|
Returns the cube root of ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access