Math Class

Package: java.lang

Provides built-in methods that perform a wide variety of mathematical calculations, including

check.png Basic functions, such as calculating an absolute value or a square root

check.png Trigonometry functions, such as sin and cos

check.png 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

static double E

The base of natural logarithms

2.718281828459045

static double PI

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

abs( argument)

Returns the absolute value of the argument. The argument can be an int, a long, a float, or a double. The return value is the same type as the argument.

cbrt( argument)

Returns the cube root of ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.