Java lang Math

From Wikiversity
Jump to navigation Jump to search

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

Static Methods[edit]

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[edit]

Math.E[edit]

Returns the closest double value to , Euler's number. is defined .

Math.PI[edit]

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

Methods in Math[edit]

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(...)[edit]

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:

ceil(...) and floor(...)[edit]

ceil(...)[edit]

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 such that and there exists no integer such that .

For example:

Math.ceil(5.5) returns 6
Math.ceil(3) returns 3
Math.ceil(-2.573) returns -2

floor(...)[edit]

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 such that and there exists no integer such that .

For example:

Math.floor(5.5) returns 5.0
Math.floor(3) returns 3
Math.floor(-2.573) returns -3

Trigonometric Functions[edit]

sin(...), cos(...), tan(...)[edit]

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 Conversion[edit]

Signatures:

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 Logarithms[edit]

Exponentiation[edit]

Signature:

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: .

exp(double) returns raised to the power of a: . This is equivalent to the call Math.pow(Math.E, a);.

Roots[edit]

Signatures:

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 . If you need a root other than square or cubic, use the pow(...) method. Math shows us that , so you can use pow(a, 1/b) for the b-th root of a ().

Logarithms[edit]

Signatures:

public static double log(double a)
public static double log10(double a)

Returns the logarithm base- (log(...)) or base-10 (log10(...)) of a.

Note that there is no generic log(double a, double b) method for . You can use the mathematical identity for any . So to find , 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[edit]

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()

Math.random() produces a random number between 0 (inclusive) and 1 (exclusive): returns such that .

Rounding[edit]

Signatures:

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 , or (long)Math.floor(a + 0.5d).

round(float) returns an int type of a rounded to an integer. Calculated mathematically by , or (int)Math.floor(a + 0.5f).

max(...) and min(...)[edit]

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.