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

MyObject obj = new MyObject();
obj.myMethod();


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:

MyObject.myStaticMethod();


## Fields in Math

### Math.E

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

### Math.PI

Returns the closest double value to ${\displaystyle \pi }$ (pi). ${\displaystyle \pi }$ is the ratio of a circle's circumference to its diameter, ${\displaystyle \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:

public static double abs(double a)
public static float abs(float a)
public static int abs(int a)
public static long abs(long a)


Returns the absolute value of a: ${\displaystyle |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: public static double ceil(double a) Returns the "ceiling" of a; that is, the least integer greater than or equal to a. For the mathematically inclined, returns an integer ${\displaystyle x}$ such that ${\displaystyle x\geq a}$ and there exists no integer ${\displaystyle y}$ such that ${\displaystyle x>y\geq a}$. For example: Math.ceil(5.5) returns 6Math.ceil(3) returns 3Math.ceil(-2.573) returns -2floor(...)Signature: public static double floor(double a) Returns the "floor" of a; that is, the greatest integer less than or equal to a. For the mathematically inclined, returns an integer ${\displaystyle x}$ such that ${\displaystyle x\leq a}$ and there exists no integer ${\displaystyle y}$ such that ${\displaystyle x. For example: Math.floor(5.5) returns 5.0Math.floor(3) returns 3Math.floor(-2.573) returns -3Trigonometric Functionssin(...), cos(...), tan(...)Signatures: public static double cos(double a) public static double sin(double a) public static double tan(double a) Returns the cosine/sine/tangent of a. a is measured in radians. Degree/Radian ConversionSignatures: public static double toDegrees(double angrad) public static double toRadians(double angdeg) 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 LogarithmsExponentiationSignature: public static double pow(double a, double b) public static double exp(double a) pow(double, double) returns a raised to the power of b: ${\displaystyle a^{b}}$. exp(double) returns ${\displaystyle e}$ raised to the power of a: ${\displaystyle e^{a}}$. This is equivalent to the call Math.pow(Math.E, a);. RootsSignatures: public static double sqrt(double a) public static double cbrt(double a) Returns the square root (sqrt(...)) or cubic root (cbrt(...)) of a. Note that there is no generic root(double a, double b) method for ${\displaystyle {\sqrt[{a}]{b}}}$. If you need a root other than square or cubic, use the pow(...) method. Math shows us that ${\displaystyle {\sqrt[{a}]{b}}=b^{1/a}}$, so you can use pow(a, 1/b) for the b-th root of a (${\displaystyle {\sqrt[{b}]{a}}}$). LogarithmsSignatures: public static double log(double a) public static double log10(double a) Returns the logarithm base-${\displaystyle e}$ (log(...)) or base-10 (log10(...)) of a. Note that there is no generic log(double a, double b) method for ${\displaystyle \log _{a}b}$. You can use the mathematical identity ${\displaystyle \log _{a}b={\frac {\log _{x}b}{\log _{x}a}}}$ for any ${\displaystyle x}$. So to find ${\displaystyle \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 GenerationThere 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: public static double random() Math.random() produces a random number between 0 (inclusive) and 1 (exclusive): returns ${\displaystyle x}$ such that ${\displaystyle 0\leq x<1}$. RoundingSignatures: public static double rint(double a) public static long round(double a) public static int round(float a) 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 ${\displaystyle \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 ${\displaystyle \lfloor a+0.5\rfloor }$, or (int)Math.floor(a + 0.5f). max(...) and min(...)Signatures: public static double max(double a, double b) public static float max(float a, float b) public static int max(int a, int b) public static long max(long a, long b) public static double min(double a, double b) public static float min(float a, float b) public static int min(int a, int b) public static long min(long a, long b) Returns the greater (max(...)) or lesser (min(...)) of the two arguments. The return type is the same as the parameter type.