Java Math With Examples

In the field of programming, we perform various numerical calculations. The value of evaluation and its performance in terms of space and time complexity is very critical. In order to ease the performance part, Java has Math class.  This class comprises various methods that perform basic numeric operations like the elementary exponential, square root, logarithm, and series of trigonometric functions.  

Math is part of java.lang library (This is a library which is an implicit part of every Java program).

So let us discuss a range of methods that Math class provides. It must be noted that all the methods of this class are static. So these are class members and can be easily called using the following format:

Math.<methodName>(<series of parameters applicable>)

The list of methods mentioned below are given in alphabetical order and very frequently put to use.

Java.lang.Math.abs(x inputParam):

Name: abs(x inputParam)
Purpose: It returns the absolute value of inputParam value. x is a numerical primitive data type i.e float/double/int/long. The data type of inputParam and return will be the same.

Example:

public class MyMath{
     public static void main(String []args){
        float myFloat=-123.123f;
        double myDouble=-121.4354;
        int myInt=12;
        long myLong=-1213123;
        
        System.out.println("abs() of float "+myFloat+" is "+Math.abs(myFloat));
        System.out.println("abs() of double "+myDouble+" is "+Math.abs(myDouble));
        System.out.println("abs() of int "+myInt+" is "+Math.abs(myInt));
        System.out.println("abs() of long "+myLong+" is "+Math.abs(myLong));
     }
}

Output:

abs() of float -123.123 is 123.123
abs() of double -121.4354 is 121.4354
abs() of int 12 is 12
abs() of long -1213123 is 1213123

Java.lang.Math.acos(double inputParam):

Name: acos(double inputParam)
Return: double
Purpose: It returns the arc cosine of inputParam value. The value is between 0 and pi.

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=0.9;
        System.out.println("acos("+myDouble+") = "+Math.acos(myDouble));
     }
}

Output:

acos(0.9) = 0.45102681179626236

Java.lang.Math.asin(double inputParam):

Name: asin(double inputParam)
Return: double
Purpose: It returns the arc sine of inputParam value. The value is between –pi/2 and pi/2.

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=0.9;
        System.out.println("asin("+myDouble+") = "+Math.asin(myDouble));
     }
}

Output:

asin(0.9) = 1.1197695149986342

Java.lang.Math.atan(double inputParam):

Name: atan(double inputParam)
Return: double
Purpose: It returns the arc tangent of inputParam value. The value is between –pi/2 and pi/2.

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=0.9;
        System.out.println("atan("+myDouble+") = "+Math.atan(myDouble));
     }
}

Output:

atan(0.9) = 0.7328151017865066

Java.lang.Math.atan2(double xCord, double yCord):

Name: atan2(double xCord, double yCord)
Return: double
Purpose: It returns the angle value after converting rectangular co-ord to polar co-ord(r,theta)

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=1.7;
        double myDouble2=4.6;
        System.out.println("atan2("+myDouble+","+myDouble2+") = "+Math.atan2(myDouble,myDouble2));
     }
}

Output:

atan2(1.7,4.6) = 0.35399743683189616

Java.lang.Math.cbrt(double inputParam):

Name: cbrt(double inputParam)
Return: double
Purpose: It returns the cube root of the inputParam.

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=1743.56;
        System.out.println("cbrt("+myDouble+") = "+Math.cbrt(myDouble));
     }
}

Output:

cbrt(1743.56) = 12.03591094498721

Java.lang.Math.cos(double inputParam):

Name: cos(double inputParam)
Return: double
Purpose: It returns the trigonometric cosine of an angle

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=1743.56;
        System.out.println("cos("+myDouble+") = "+Math.cos(myDouble));
     }
}

Output:

cos(1743.56) = -0.9997138648461751

Java.lang.Math.cosh(double inputParam):

Name: cosh(double inputParam)
Return: double
Purpose: It returns the hyperbolic trigonometric cosine of an angle

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=1.56;
        System.out.println("cosh("+myDouble+") = "+Math.cosh(myDouble));
     }
}

Output:

cosh(1.56) = 2.4844786581693095

Java.lang.Math.exp(double inputParam):

Name: exp(double inputParam)
Return: double
Purpose: It returns, e raised to the power inputParam

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=1.56;
        System.out.println("exp("+myDouble+") = "+Math.exp(myDouble));
     }
}

Output:

exp(1.56) = 4.758821245137854

Java.lang.Math.floor(double inputParam):

Name: floor(double inputParam)
Return: double
Purpose: It returns the double value which is slightly less or equal to inputParam and is also a mathematical integer

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=1.56;
        System.out.println("floor("+myDouble+") = "+Math.floor(myDouble));
     }
}

Output:

floor(1.56) = 1.0

Java.lang.Math.hypot(double x, double y):

Name: hypot(double x, double y)
Return: double
Purpose: It returns the hypotenuse value considering x and y as two sides of a triangle

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=3;
        double myDouble2=4;
        System.out.println("hypot("+myDouble+","+myDouble2+") = "+Math.hypot(myDouble,myDouble2));
     }
}

Output:

hypot(3.0,4.0) = 5.0

Java.lang.Math.log(double inputParam):

Name: log(double inputParam)
Return: double
Purpose: It returns the natural logarithm(base e)  value of inputParam

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=3;
        System.out.println("log("+myDouble+") = "+Math.log(myDouble));
     }
}

Output:

log(3.0) = 1.0986122886681098

Java.lang.Math.log10(double inputParam):

Name: log10(double inputParam)
Return: double
Purpose: It returns the natural logarithm(base 10)  value of inputParam

Example:

public class MyMath{
     public static void main(String []args){
        double myDouble=10;
        System.out.println("log10("+myDouble+") = "+Math.log10(myDouble));
     }
}

Output:

log10(10.0) = 1.0

Java.lang.Math.max(x a , x b):

Name: max(x a , x b)
Return: x
Purpose: It returns the maximum among the two input parameters a and b. Here x is a numerical primitive data type i.e float/double/int/long. The data type of inputParam and return will be the same.

Example:

public class MyMath{
     public static void main(String []args){         
        double myDouble=121.435;
        double myDouble2=123.123;
        
        int myInt=12;
        int myInt2=43;
        
        long myLong=4334332;
        long myLong2=2423432;
        
        float myFloat = 123.12f;
        float myFloat2 = 154.12f;
        
        System.out.println("Float max("+myFloat+","+myFloat2+") is "+Math.max(myFloat,myFloat2));
        System.out.println("Long max("+myLong+","+myLong2+") is "+Math.max(myLong,myLong2));
        System.out.println("Int max("+myInt+","+myInt2+") is "+Math.max(myInt,myInt2));
        System.out.println("Double max("+myDouble+","+myDouble2+") is "+Math.max(myDouble,myDouble2));
     }
}

Output:

Float max(123.12,154.12) is 154.12
Long max(4334332,2423432) is 4334332
Int max(12,43) is 43
Double max(121.435,123.123) is 123.123

Java.lang.Math.min(x a , x b):

Name: min(x a , x b)
Return: x
Purpose: It returns the minimum among the two input parameters a and b. Here x is a numerical primitive data type i.e float/double/int/long. The data type of inputParam and return will be the same.

Example:

public class MyMath{
     public static void main(String []args){         
        double myDouble=121.435;
        double myDouble2=123.123;
        
        int myInt=12;
        int myInt2=43;
        
        long myLong=4334332;
        long myLong2=2423432;
        
        float myFloat = 123.12f;
        float myFloat2 = 154.12f;
        
        System.out.println("Float min("+myFloat+","+myFloat2+") is "+Math.min(myFloat,myFloat2));
        System.out.println("Long min("+myLong+","+myLong2+") is "+Math.min(myLong,myLong2));
        System.out.println("Int min("+myInt+","+myInt2+") is "+Math.min(myInt,myInt2));
        System.out.println("Double min("+myDouble+","+myDouble2+") is "+Math.min(myDouble,myDouble2));
     }
}

Output:

Float min(123.12,154.12) is 123.12
Long min(4334332,2423432) is 2423432
Int min(12,43) is 12
Double min(121.435,123.123) is 121.435

Java.lang.Math.pow(double x, double y):

Name: pow(double x, double y)
Return: double
Purpose: It returns the value of x raised to the power y.

Example:

public class MyMath{
     public static void main(String []args){         
        double myDouble=2;
        double myDouble2=3;        
        System.out.println("pow("+myDouble+","+myDouble2+") is "+Math.pow(myDouble,myDouble2));
     }
}

Output:

pow(2.0,3.0) is 8.0

Java.lang.Math.random():

Name: random()
Return: double
Purpose: It returns a value in range [0,1.0)

Example:

public class MyMath{
     public static void main(String []args){         
        System.out.println("1 random() : "+Math.random());
        System.out.println("2 random() : "+Math.random());
        System.out.println("3 random() : "+Math.random());
     }
}

Output:

1 random() : 0.08393546021648257
2 random() : 0.2142775463552603
3 random() : 0.33068510951362495

Java.lang.Math.round(double/float inputParam):

Name: round(double/float inputParam)
Return:double/float
Purpose: It returns the closest long/int to the inputParam rounding up.

Example:

public class MyMath{
     public static void main(String []args){         
        double myDouble=121.435;
        float myFloat = 154.12f;        
        System.out.println("round("+myDouble+") = "+Math.round(myDouble));
        System.out.println("round("+myFloat+") = "+Math.round(myFloat));
     }
}

Output:

round(121.435) = 121
round(154.12) = 154

Java.lang.Math.sin(double inputParam):

Name: sin(double inputParam)
Return: double
Purpose: It returns the trigonometric sine of an angle

Example:

public class MyMath{
     public static void main(String []args){         
        double myDouble=62.435;
        
        System.out.println("sin("+myDouble+") = "+Math.sin(myDouble));
     }
}

Output:

sin(62.435) = -0.3865179060390371

Java.lang.Math.sinh(double inputParam):

Name: sinh(double inputParam)
Return: double
Purpose: It returns the hyperbolic trigonometric sine of an angle

Example:

public class MyMath{
     public static void main(String []args){         
        double myDouble=0.5;        
        System.out.println("sinh("+myDouble+") = "+Math.sinh(myDouble));
     }
}

Output:

sinh(0.5) = 0.5210953054937474

Java.lang.Math.sqrt(double inputParam):

Name: sqrt(double inputParam)
Return: double
Purpose: It returns the square root of the inputParam

Example:

public class MyMath{
     public static void main(String []args){         
        double myDouble=5;        
        System.out.println("sqrt("+myDouble+") = "+Math.sqrt(myDouble));
     }
}

Output:

sqrt(5.0) = 2.23606797749979

Java.lang.Math.tan(double inputParam):

Name: tan(double inputParam)
Return: double
Purpose: It returns the trigonometric tangent of an angle

Example:

public class MyMath{
     public static void main(String []args){       
        double myDouble=1;        
        System.out.println("tan("+myDouble+") = "+Math.tan(myDouble));
     }
}

Output:

tan(1.0) = 1.5574077246549023

Java.lang.Math.tanh(double inputParam):

Name: tanh(double inputParam)
Return: double
Purpose: It returns the hyperbolic trigonometric tangent of an angle

Example:

public class MyMath{
     public static void main(String []args){         
        double myDouble=0.96;       
        System.out.println("tanh("+myDouble+") = "+Math.tanh(myDouble));
     }
}

Output:

tanh(0.96) = 0.7442768673618372

Java.lang.Math.toDegrees(double inputParam):

Name: toDegrees(double inputParam)
Return: double
Purpose: It returns the inputParam radian to degrees

Example:

public class MyMath{
     public static void main(String []args){         
        double myDouble=1.57;        
        System.out.println("toDegrees("+myDouble+") = "+Math.toDegrees(myDouble));
     }
}

Output:

toDegrees(1.57) = 89.95437383553926

Java.lang.Math.toRadians(double inputParam):

Name: toRadians(double inputParam)
Return: double
Purpose: It returns the inputParam degrees to radian

Example:

public class MyMath{
     public static void main(String []args){   
        double myDouble=90;    
        System.out.println("toRadians("+myDouble+") = "+Math.toRadians(myDouble));
     }
}

Output:

toRadians(90.0) = 1.5707963267948966

This was just a glimpse among so many methods that are available to be utilized effectively. The readers are expected to explore more methods available with Math class. In the next lecture, we will discuss Java Date and Time.