JAVA

Basic Math Functions

The java.lang.Math contains a set of basic math functions for obtaining the absolute value, highest and lowest of two values, rounding of values, random values etc. These basic math functions of the Java Math class will be covered in the following sections.

Math.abs()

The Math.abs() function returns the absolute value of the parameter passed to it. The absolute value is the positive value of the parameter. If the parameter value is negative, the negative sign is removed and the positive value corresponding to the negative value without sign is returned. Here are two Math.abs() method examples:

int abs1 = Math.abs(10);  // abs1 = 10
int abs2 = Math.abs(-20); // abs2 = 20

The absolute value of 10 is 10. The absolute value of -20 is 20.

The Math.abs() method is overloaded in 4 versions:

Math.abs(int)
Math.abs(long)
Math.abs(float)
Math.abs(double)

Which of these methods are called depends on the type of the parameter passed to the Math.abs() method.

Math.ceil()

The Math.ceil() function rounds a floating point value up to the nearest integer value. The rounded value is returned as a double. Here is a Math.ceil() Java example:

double ceil = Math.ceil(7.343);  // ceil = 8.0

After executing this Java code the ceil variable will contain the value 8.0 .

Math.floor()

The Math.floor() function rounds a floating point value down to the nearest integer value. The rounded value is returned as a double. Here is a Math.floor() Java example:

double floor = Math.floor(7.343);  // floor = 7.0

After executing this Java code the ceil variable will contain the value 8.0 .

Math.floorDiv()

The Math.floorDiv() method divides one integer (int or long) by another, and rounds the result down to the nearest integer value. If the result is positive, the effect is the same as using the Java / division operator described earlier in this text.

If the result is negative, however, the result is not the same. With the / division operator the fractions are simply truncated. For positive numbers this corresponds to rounding down. For negative numbers though, truncating the fractions correspond to rounding up. The floorDiv() method rounds down to the nearest negative integer, instead of the rounding up that would occur with fraction truncation.

Here is a Math.floorDiv() Java example:

double result3 = Math.floorDiv(-100,9);
System.out.println("result3: " + result3);

double result4 = -100 / 9;
System.out.println("result4: " + result4);

The output printed from this Java code is:

Output:

Command Prompt
result3: -12.0
result4: -11.0

This shows the difference between the / division operator and Math.floorDiv() .

Math.min()

The Math.min() method returns the smallest of two values passed to it as parameter. Here is a Math.min() Java example:

int min = Math.min(10, 20);

After executing this code the min variable will contain the value 10.

Math.max()

The Math.max() method returns the largest of two values passed to it as parameter. Here is a Math.max() Java example:

int max = Math.max(10, 20);

After executing this code the max variable will contain the value 20.

Math.round()

The Math.round() method rounds a float or double to the nearest integer using normal math round rules (either up or down). Here is a Java Math.round() example:

double roundedDown = Math.round(23.445);
double roundedUp   = Math.round(23.545);

After executing these two Java statements the roundedDown variable will contain the value 23.0 , and the roundedUp variable will contain the value 24.0.

Math.random()

The Math.random() method returns a random floating point number between 0 and 1. Of course the number is not fully random, but the result of some calculation which is supposed to make it as unpredictable as possible. Here is a Java Math.random() example:

double random = Math.random();

To get a random value between 0 and e.g. 100, multiply the value returned by Math.random() with the maximum number (e.g. 100). Here is an example of how that might look:

double random = Math.random() * 100D;

If you need an integer value, use the round(), floor() or ceil() method.


Exponential and Logarithmic Math Functions

The Java Math class also contains a set of functions intended for exponential and logarithmic calculations. I will cover some of these math functions in the following sections.

Math.exp()

The Math.exp() function returns e (Euler's number) raised to the power of the value provided as parameter. Here is a Java Math.exp() example:

double exp1 = Math.exp(1);
System.out.println("exp1 = " + exp1);

double exp2 = Math.exp(2);
System.out.println("exp2 = " + exp2);

When this Java math code is executed it will print this output:

Output:

Command Prompt
exp1 = 2.718281828459045
exp2 = 7.38905609893065

Math.log()

The Math.log() method provides the logarithm of the given parameter. The base for the logarithm is i (Euler's number). Thus, Math.log() provides the reverse function of Math.exp(). Here is a Java Math.log() example:


double log1  = Math.log(1);
System.out.println("log1 = " + log1);

double log10 = Math.log(10);
System.out.println("log10 = " + log10);

The output from this Math.log() example is:

Output:

Command Prompt
log1 = 0.0
log10 = 2.302585092994046

Math.log10()

The Math.log10 method works like the Math.log() method except is uses 10 as is base for calculating the logarithm instead of e (Euler's Number). Here is a Math.log10() Java example:

double log10_1   = Math.log10(1);
System.out.println("log10_1 = " + log10_1);

double log10_100 = Math.log10(100);
System.out.println("log10_100 = " + log10_100);

The output printed from this Java Math.log10() example would be:

Output:

Command Prompt
log10_1 = 0.0
log10_100 = 2.0

Math.pow()

The Math.pow() function takes two parameters. The method returns the value of the first parameter raised to the power of the second parameter. Here is a Math.pow() Java example:

double pow2 = Math.pow(2,2);
System.out.println("pow2 = " + pow2);

double pow8 = Math.pow(2,8);
System.out.println("pow8 = " + pow8);

The output from this Math.pow() example would be:

Output:

Command Prompt
pow2 = 4.0
pow8 = 256.0

In other words, the Math.pow() example calculate the values of 22 and 28 which are 4 and 256.

Math.sqrt()

The Math.sqrt() method calculates the square root of the parameter given to it. Here are a few Java Math.sqrt() example:

double sqrt4 = Math.sqrt(4);
System.out.println("sqrt4 = " + sqrt4);

double sqrt9 = Math.sqrt(9);
System.out.println("sqrt9 = " + sqrt9);

The output printed from these Java Math.sqrt() examples would be:

Output:

Command Prompt
sqrt4 = 2.0
sqrt9 = 3.0

Trigonometric Math Functions

The Java Math class contains a set of trigonometric functions. These functions can calculate values used in trigonometry, like sine, cosine, tangens etc. I will cover the most used trigonometry functions in the following sections. If you are looking for a trigonometric function and you cannot find it here, check the JavaDoc for the Java Math class. The Math class just might have the function you are looking for, even if I have not described it here.

Math.PI

The Math.PI constant is a double with a value that is very close to the value of PI - the mathematical definition of PI. You will often need the Math.PI field when making trigonometric calculations.

Math.sin()

The Math.sin() method calculates the sine value of some angle value in radians. Here is a Java Math.sin() example:

double sin = Math.sin(Math.PI);
System.out.println("sin = " + sin);

Math.cos()

The Math.cos() method calculates the cosine value of some angle value in radians. Here is a Java Math.cos() example:

double cos = Math.cos(Math.PI);
System.out.println("cos = " + cos);

Math.tan()

The Math.tan() method calculates the tangens value of some angle value in radians. Here is a Java Math.tan() example:

double tan = Math.tan(Math.PI);
System.out.println("tan = " + tan);

Math.asin()

The Math.asin() method calculates the arc sine value of a value between 1 and -1. Here is a Java Math.asin() example:

double asin = Math.asin(1.0);
System.out.println("asin = " + asin);

Math.acos()

The Math.acos() method calculates the arc cosine value of a value between 1 and -1. Here is a Java Math.acos() example:

double acos = Math.acos(1.0);
System.out.println("acos = " + acos);

Math.atan()

The Math.atan() method calculates the arc tangens value of a value between 1 and -1. Here is a Java Math.atan() example:

double atan = Math.atan(1.0);
System.out.println("atan = " + atan);

Math.atan2()

I am not exactly sure what Math.atan2() method does mathematically. Here is what the JavaDoc says:

"Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta)".

If you need this method, please read the JavaDoc. But now you know at least that it exists.

Math.sinh()

The Math.sinh() method calculates the hyperbolic sine value of a value between 1 and -1. Here is a Java Math.sinh() example:

double sinh = Math.sinh(1.0);
System.out.println("sinh = " + sinh);

Math.cosh()

The Math.cosh() method calculates the hyperbolic cosine value of a value between 1 and -1. Here is a Java Math.cosh() example:

double cosh = Math.cosh(1.0);
System.out.println("cosh = " + cosh);

Math.tanh()

The Math.tanh() method calculates the hyperbolic tangens value of a value between 1 and -1. Here is a Java Math.tanh() example:

double tanh = Math.tanh(1.0);
System.out.println("tanh = " + tanh);

Math.toDegrees()

The Math.toDegrees() method converts an angle in radians to degrees. Here is a Java Math.toDegrees() example:

double degrees = Math.toDegrees(Math.PI);
System.out.println("degrees = " + degrees);

Math.toRadians()

The Math.toRadians() method converts an angle in degrees to radians. Here is a Java Math.toRadians() example:

double radians = Math.toRadians(180);
System.out.println("radians = " + radians);



Subscribe us on Youtube

Share This Page on


Ask Question