☞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()>
☞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);
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));
}
}
-5 43.44 235 -35
☞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)
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));
}
}
6 46.44 3435 -30
☞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)
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));
}
}
8.0 8.0 0.001 -8.0 16.0 0.125
☞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)
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));
}
}
2.0 4.0 NaN
☞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)
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));
}
}
2.0 3.0 -2.0
☞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)
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));
}
}
2.0794415416798357 3.295836866004329 NaN
☞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)
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));
}
}
8.5 27 8
☞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)
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));
}
}
8 9 9 -8 -8 -9
☞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)
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));
}
}
8.0 8.0 9.0 -8.0 -8.0 -9.0
☞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)
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));
}
}
9.0 9.0 -8.0 -8.0
☞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)
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));
}
}
8.0 8.0 -9.0 -9.0
☞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)
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));
}
}
2.718281828459045 518.012824668342 0.0019304541362277093