Java lang Math

The  class provides methods for doing math. Go figure.

Static Methods
The  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  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;

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

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

Methods in
You'll see that many of the methods listed here have multiple signatures. Why is this? 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  uses. Which version of the method is actually called depends on what paramter types you give it. In the case of, 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.)

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| = \begin{cases}a & \mbox{if }a > 0 \\ 0 & \mbox{if } a = 0 \\ -a & \mbox{if } a < 0 \end{cases}$$

Signature: public static double ceil(double a)

Returns the "ceiling" of ; that is, the least integer greater than or equal to. For the mathematically inclined, returns an integer $$x$$ such that $$x \ge a$$ and there exists no integer $$y$$ such that $$x > y \ge a$$.

For example:


 * returns 6


 * returns 3


 * returns -2

Signature: public static double floor(double a)

Returns the "floor" of ; that is, the greatest integer less than or equal to. For the mathematically inclined, returns an integer $$x$$ such that $$x \le a$$ and there exists no integer $$y$$ such that $$x < y \le a$$.

For example:


 * returns 5.0


 * returns 3


 * returns -3

, ,
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. is measured in radians.

Degree/Radian Conversion
Signatures: public static double toDegrees(double angrad) public static double toRadians(double angdeg)

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

Exponentiation
Signature: public static double pow(double a, double b) public static double exp(double a)

returns  raised to the power of  : $$a^b$$.

returns $$e$$ raised to the power of : $$e^a$$. This is equivalent to the call.

Roots
Signatures: public static double sqrt(double a) public static double cbrt(double a)

Returns the square root or cubic root  of.

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

Logarithms
Signatures: public static double log(double a) public static double log10(double a)

Returns the logarithm base-$$e$$ or base-10  of.

Note that there is no generic  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. (You could use  method, too, but   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: public static double random

produces a random number between 0 (inclusive) and 1 (exclusive): returns $$x$$ such that $$0 \le x < 1$$.

Rounding
Signatures: public static double rint(double a) public static long round(double a) public static int round(float a)

Rounds  to the nearest mathematical integer.

(from round to integer) returns a  type that is guaranteed to be an integer. If  is equally distant from two integers, this method returns the even integer.

returns a  type of   rounded to an integer. Calculated mathematically by $$\lfloor a + 0.5 \rfloor$$, or.

returns an  type of   rounded to an integer. Calculated mathematically by $$\lfloor a + 0.5 \rfloor$$, or.

and
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 or lesser  of the two arguments. The return type is the same as the parameter type.