# Java lang Math

The java.lang.Math class provides methods for doing math. Go figure.

## Static Methods

The Math class's methods are static. This is a fairly advanced concept, but here's a brief overview.
Normal (non-static) methods are run inside a specific object. To use them, you have to have a specific object, usually through a constructor (with the new keyword):
Static methods, however, run inside the entire class. To use them, append a period and the name of the method, as usual, to the name of the class:

## Fields in Math
### Math.E

Returns the closest double value to $e$, Euler's number. $e$ is defined $e=\lim _{x\to \infty }\left(1+{\frac {1}{x}}\right)^{x}\approx 2.718281828459045$.

### Math.PI

Returns the closest double value to $\pi$ (pi). $\pi$ is the ratio of a circle's circumference to its diameter, $\approx 3.141592653589573$.

## Methods in Math

You'll see that many of the methods listed here have multiple signatures. Why is this? Math is a utility class, which means it exists to make your code as simple as possible. It often has multiple versions of the same method with different parameters and return types so you don't have to bother with converting your variable to a type that Math uses. Which version of the method is actually called depends on what paramter types you give it. In the case of Math, it doesn't matter; all versions of the same method do the same thing, just with different return types to match the parameter type.
(Incidentally, the technique of having methods with the same name is called overloading methods.)

### abs(...)

Signatures:
Returns the absolute value of a: $|a|={\begin{cases}a&{\mbox{if }}a>0\\0&{\mbox{if }}a=0\\-a&{\mbox{if }}a<0\end{cases}}$

### ceil(...) and floor(...)
#### ceil(...)

Signature:
Returns the "ceiling" of a; that is, the least integer greater than or equal to a. For the mathematically inclined, returns an integer $x$ such that $x\geq a$ and there exists no integer $y$ such that $x>y\geq a$.
For example:
    Math.ceil(5.5) returns 6
    Math.ceil(3) returns 3
    Math.ceil(-2.573) returns -2

#### floor(...)

Signature:
Returns the "floor" of a; that is, the greatest integer less than or equal to a. For the mathematically inclined, returns an integer $x$ such that $x\leq a$ and there exists no integer $y$ such that $x<y\leq a$.
For example:
    Math.floor(5.5) returns 5.0
    Math.floor(3) returns 3
    Math.floor(-2.573) returns -3

### Trigonometric Functions
#### sin(...), cos(...), tan(...)

Signatures:
Returns the cosine/sine/tangent of a. a is measured in radians.

#### Degree/Radian Conversion

Signatures:
Convert between degrees and radians. toDegrees accepts a radian measurement and returns the equivalent degree measurement; toRadians works the other way around.

### Exponents, Roots, and Logarithms
#### Exponentiation

Signature:
pow(double, double) returns a raised to the power of b: $a^{b}$.
exp(double) returns $e$ raised to the power of a: $e^{a}$. This is equivalent to the call Math.pow(Math.E, a);.

#### Roots

Signatures:
Returns the square root (sqrt(...)) or cubic root (cbrt(...)) of a.
Note that there is no generic root(double a, double b) method for ${\sqrt[{a}]{b}}$. If you need a root other than square or cubic, use the pow(...) method. Math shows us that ${\sqrt[{a}]{b}}=b^{1/a}$, so you can use pow(a, 1/b) for the b-th root of a (${\sqrt[{b}]{a}}$).

#### Logarithms

Signatures:
Returns the logarithm base-$e$ (log(...)) or base-10 (log10(...)) of a.
Note that there is no generic log(double a, double b) method for $\log _{a}b$. You can use the mathematical identity $\log _{a}b={\frac {\log _{x}b}{\log _{x}a}}$ for any $x$. So to find $\log _{a}b$, use the code Math.log(b) / Math.log(a). (You could use Math.log10(...) method, too, but log involves less typing.)

### (Pseudo-)Random Number Generation

There is no truely random number generator; the best computers can come up with are algorithms for generating unpredictable numbers based on a seed value. These unpredictable numbers are called pseudo-random.
Signature:
Math.random produces a random number between 0 (inclusive) and 1 (exclusive): returns $x$ such that $0\leq x<1$.

### Rounding

Signatures:
Rounds a to the nearest mathematical integer.
rint(double) (from round to integer) returns a double type that is guaranteed to be an integer. If a is equally distant from two integers, this method returns the even integer.
round(double) returns a long type of a rounded to an integer. Calculated mathematically by $\lfloor a+0.5\rfloor$, or (long)Math.floor(a + 0.5d).
round(float) returns an int type of a rounded to an integer. Calculated mathematically by $\lfloor a+0.5\rfloor$, or (int)Math.floor(a + 0.5f).

### max(...) and min(...)

Signatures:
Returns the greater (max(...)) or lesser (min(...)) of the two arguments. The return type is the same as the parameter type.