Mathematical Functions in Java


☞They are built-in methods which help the user to perform certain tasks easily and quickly.

☞These mathematical functions are included in Math class under java.lang package.

java.lang package is a default package. Hence, we do not import it.

☞Syntax to use any mathematical function : Math.<function_name()>


min() function

Description : It returns the smallest of two numbers.

Return Data Type : It returns int/long/double type value depending upon the type of arguments passed to it.

Syntax : <return_data_type> <variable_name> = Math.min(argument1, argument2);

Example

public class MinExample {
    public static void main(String[] args) {
        
        System.out.println(Math.min(6, -5));
        System.out.println(Math.min(46.44, 43.44));
        System.out.println(Math.min(235, 3435));
        System.out.println(Math.min(-35, -30));
    
    }
}

Output

-5
43.44
235
-35

max() function

Description : It returns the greatest of two numbers.

Return Data Type : It returns int/long/double type value depending upon the type of arguments passed to it.

Syntax : <return_data_type> <variable_name> = Math.max(argument1, argument2)

Example

public class MaxExample {
    public static void main(String[] args) {
        
        System.out.println(Math.max(6, -5));
        System.out.println(Math.max(46.44, 43.44));
        System.out.println(Math.max(235, 3435));
        System.out.println(Math.max(-35, -30));
    
    }
}

Output

6
46.44
3435
-30

pow() function

Description : It is used to find the power raised to a given base value.

Return Data Type : It returns double type value.

Syntax : <return_data_type> <variable_name> = Math.pow(argument1, argument2)

Example

public class PowExample {
    public static void main(String[] args) {
        
        System.out.println(Math.pow(2.0, 3.0));
        System.out.println(Math.pow(2, 3));
        System.out.println(Math.pow(10, -3));
        System.out.println(Math.pow(-2, 3));
        System.out.println(Math.pow(-2, 4));
        System.out.println(Math.pow(2, -3));
        
    }
}

Output

8.0
8.0
0.001
-8.0
16.0
0.125

sqrt() function

Description : It is used to find the square root of a positive number.

Return Data Type : It returns double type value.

Syntax : <return_data_type> <variable_name> = Math.sqrt(argument)

Example

public class SqrtExample {
    public static void main(String[] args) {
        
        System.out.println(Math.sqrt(4.0));
        System.out.println(Math.sqrt(16));
        System.out.println(Math.sqrt(-4));
        
    }
}

Output

2.0
4.0
NaN

cbrt() function

Description : It is used to find the cube root of a positive or negative number.

Return Data Type : It returns double type value.

Syntax : <return_data_type> <variable_name> = Math.cbrt(argument)

Example

public class CbrtExample {
    public static void main(String[] args) {
    
        System.out.println(Math.cbrt(8.0));
        System.out.println(Math.cbrt(27));
        System.out.println(Math.cbrt(-8));
        
    }
}

Output

2.0
3.0
-2.0

log() function

Description : It is used to find the natural logarithmic value of a given argument.

Return Data Type : It returns double type value.

Syntax : <return_data_type> <variable_name> = Math.log(argument)

Example

public class LogExample {
    public static void main(String[] args) {
        
        System.out.println(Math.log(8.0));
        System.out.println(Math.log(27));
        System.out.println(Math.log(-8));
        
    }
}

Output

2.0794415416798357
3.295836866004329
NaN

abs() function

Description : It returns magnitude of the number without its sign.

Return Data Type : It returns int/long/double type value depending upon the type of arguments passed to it.

Syntax : <return_data_type> <variable_name> = Math.abs(argument)

Example

public class AbsExample {
    public static void main(String[] args) {
        
        System.out.println(Math.abs(8.5));
        System.out.println(Math.abs(27));
        System.out.println(Math.abs(-8));
        
    }
}

Output

8.5
27
8

round() function

Description : It returns the value of a number rounded to its nearest integer.

☞If fractional part < 0.5, then it returns the same integer value, otherwise it returns next higher integer.

Return Data Type : It returns int/long type value.

Syntax : <return_data_type> <variable_name> = Math.round(argument)

Example

public class RoundExample {
    public static void main(String[] args) {
        
        System.out.println(Math.round(8.4));
        System.out.println(Math.round(8.5));
        System.out.println(Math.round(8.6));
        System.out.println(Math.round(-8.4));
        System.out.println(Math.round(-8.5));
        System.out.println(Math.round(-8.6));
        
    }
}

Output

8
9
9
-8
-8
-9

rint() function

Description : It returns the nearest integer of a given fractional number.

Return Data Type : It returns double type value.

Syntax : <return_data_type> <variable_name> = Math.rint(argument)

Example

public class RintExample {
    public static void main(String[] args) {
        
        System.out.println(Math.rint(8.4));
        System.out.println(Math.rint(8.5));
        System.out.println(Math.rint(8.6));
        System.out.println(Math.rint(-8.4));
        System.out.println(Math.rint(-8.5));
        System.out.println(Math.rint(-8.6));
    }
}

Output

8.0
8.0
9.0
-8.0
-8.0
-9.0

ceil() function

Description : It returns the next higher integer number that is greater than or equal to the argument.

Return Data Type : It returns double type value.

Syntax : <return_data_type> <variable_name> = Math.ceil(argument)

Example

public class CeilExample {
    public static void main(String[] args) {
        
        System.out.println(Math.ceil(8.5));
        System.out.println(Math.ceil(8.6));
        System.out.println(Math.ceil(-8.5));
        System.out.println(Math.ceil(-8.6));
        
    }
}

Output

9.0
9.0
-8.0
-8.0

floor() function

Description : It returns the lower integer number that is less than or equal to the argument.

Return Data Type : It returns double type value.

Syntax : <return_data_type> <variable_name> = Math.floor(argument)

Example

public class FloorExample {
    public static void main(String[] args) {
        
        System.out.println(Math.floor(8.5));
        System.out.println(Math.floor(8.6));
        System.out.println(Math.floor(-8.5));
        System.out.println(Math.floor(-8.6));
        
    }
}

Output

8.0
8.0
-9.0
-9.0

exp() function

Description : It results in the exponential value of an argument x(i.e. ex).

Value of e : 2.718281828459

Return Data Type : It returns double type value.

Syntax : <return_data_type> <variable_name> = Math.exp(argument)

Example

public class ExponentExample {
    public static void main(String[] args) {
        
        System.out.println(Math.exp(1));
        System.out.println(Math.exp(6.25));
        System.out.println(Math.exp(-6.25));
        
    }
}

Output

2.718281828459045
518.012824668342
0.0019304541362277093