Understanding Java Math Class And its different methods

Understanding Java Math Class And its different methods

You can’t learn Java without mathematics. It haunts you everywhere but if you want to learn to program then you must understand Java Maths Class. The Java Math class is useful to perform mathematical problems like min(), max(), sin(), cos(), tan(), abs() etc.  Numerous methods in the Java Math class enable you to apply mathematics to numbers. 

What is Java Math Class with Examples?

The Maths class in Java is useful to perform mathematical problems like min(), max(), sin(), cos(), tan(), abs() etc. All implementations of the comparable Math class function cannot be defined to deliver results that are exactly the same bit for bit, unlike some of the StrictMath class numeric methods. Where rigorous repeatability is not essential, this relaxation allows implementation with improved performance.

The methods addExact(), subtractExact(), multiplyExact(), and toIntExact() throw an ArithmeticException if the size is int or long and the results exceed the range of values.

When a specific minimum or maximum value is present, overflow occurs for other mathematical operations like increment, decrement, division, absolute value, and negation. It should be compared to the proper maximum and minimum values.

Example

public class Main
{
  public static void main (String[]args)
  {
    double x = 24d;
    double y = 6d;
    
    // returns the logarithm of given value when base is 10      
    System.out.println("log10 of x is:"+ Math.log10(x));
    
    // returns the log of x + 1  
    System.out.println("log1p of x is:"+ Math.log1p(x));
    
    // returns the power of 2    
    System.out.println("exp of a is:"+ Math.exp(x));
  }
}

Also read, Learn about Java Datatypes and how we use them

Different types of Java Math Methods

Numerous methods for carrying out fundamental mathematical operations, including the logarithm, cube root, and trigonometric functions, are included in the java.lang.Math class. The following are the different Java math techniques:

Basic Math Method

Math.abs()

Math.abs() returns the absolute value of a given argument. If the given argument is positive, the same argument is returned; if the given argument is negative, then the negation of the argument is returned.

public class Main
{
  public static void main (String args[])
  {
    int x = 48;
    int y = -48;
      
    System.out.println (Math.abs (x)); // 48
    System.out.println (Math.abs (y)); // 48
  }
}

Math.max()

When two or more values are given Math.max() method finds out the maximum value among them and prints the result.

public class Main  {  
    public static void main(String args[]) {  
        int x = 20;  
        int y = 50;    
        System.out.println(Math.max(x, y)); // 50 
    }  
}

Math.min()

When two or more values are given Math.min() method finds out the minimum value among them and prints the result.

public class Main  {  
    public static void main(String args[]) {  
        int x = 20;  
        int y = 50;    
        System.out.println(Math.min(x, y)); // 20 
    }  
}

Math.round()

When a value is given in the decimal form the Math.round() method is used to find out the round figure of the given value.

public class Main {  
    public static void main(String[] args) {  
        double x = 79.52; 
        System.out.println(Math.round(x)); // 80 
    }  
}

Math.sqrt()

The Math.sqrt() is used to find the square root of a given value. It even finds the square root of a decimal value. Ex square root of 81.0 is 9.

public class Main {  
    public static void main(String[] args)  {  
        double x = 81.0; 
        System.out.println(Math.sqrt(x));  // 9.0
    }  
}

Math.cbrt()

When we need to return the values in the form of cube root we use Math.cbrt(). Ex. cube root of 729 is 9.

public class Main  {  
    public static void main(String[] args)  {   
        double x = 729;  
        System.out.println(Math.cbrt(x)); // 9.0
    }  
}

Math.hypot()

Math.pypot() is used to find the hypotenuse of two given values as in most cases it is used in triangles square root of (a*a + b*b).

public class Main  {  
    public static void main(String[] args) {  
        double a = 8;  
        double b = 6;  
        System.out.println(Math.hypot(a, b)); // 10.0
    }  
}

Math.random()

Math.random() is used to find out the random double value that is between 0.0 to 1.0 (greater than 0.0 and less than 1.0 that is 0.9049556422519468 (any double value number).

public class Main  {  
    public static void main(String[] args) {
        double a = Math.random();  
        double b = Math.random();
        
        System.out.println(a);  // 0.7842494800508415
        System.out.println(b); // 0.720642318947557
    }  
}

Also read, Simple explanation of Java Operators

Logarithmic Maths Method

Math.logo()

Math.logo() return logarithmic values of base e.

public class Main
{
  public static void main (String[]args)
  {
    double x = 38.9;
    
    System.out.println (Math.log (x)); // 3.6609942506244004
  }

}

Math.log10()

Natural logarithm of a double value is returned when the base is 10.

public class Main
{
  public static void main(String[] args)  {  
        double x = 38.9;  
        System.out.println(Math.log10(x));  // 1.5899496013257077
    }

}

Math.log1p()

It gives back the natural logarithm of the argument and number one added together.

public class Main
{
  public static void main(String[] args)  {  
        double x = 1.0/0;  
          System.out.println(Math.log1p(x));  // Infinity
    }  
}

Math.exp()

This is used to return the value of the Euler’s number, e, increased to double power. Euler’s number e, in this case, is roughly equivalent to 2.718281828459045.

public class Main
{
  public static void main(String[] args)  {  
        double x = 1.0/0;  
          System.out.println(Math.log1p(x)); // Infinity
    } 
}

Math.exmp1()

The Euler’s number e raised to the power of a double value is returned using this, and one is subtracted from it. Euler’s number e, in this case, is roughly equivalent to 2.718281828459045.

public class Main
{
  public static void main(String[] args)  {  
        double a = -7.0;  
        System.out.println(Math.expm1(a));  // -0.9990881180344455
    }  
}

Also read, Automatically Format Code On File Save in Visual Studio Code Using Prettier

Trigonometric Maths Method

Math.toRadian()

Math.toRadian() is used to convert a given angle that is measured in degrees to an approximately equivalent measured degree to radians.

public class Main
{
  public static void main(String[] args)  {  
        double a = 60;  
        System.out.println(Math.toRadians(a));  // 1.0471975511965976
    }  
}

Math.sin()

  1. When we need to find a sin value of a given decimal number (double) is returned as we use sin for trigonometric questions.
public class Main
{
  public static void main(String[] args) {  
        double a = 60;
        double b = Math.toRadians(a);  
        System.out.println(Math.sin(b)); //  0.8660254037844386
    } 
}

Math.cos()

Math.cos() is used to find out the base and height or cosine value of the given double measured in degrees which are first converted to radian using Math.toRadian() then the cosine value is returned.

public class Main
{
  public static void main(String[] args)  {  
        double a = 60;  
        double b = Math.toRadians(a);  
        System.out.println(Math.cos(b)); // 0.5000000000000001
    }  
}

Math.tan()

Math.tan() is used to find the tangent of a given double value which is first converted to radians then the tangent is returned.

public class Main
{
  public static void main(String[] args)  {  
        double a = 60;  
        double b = Math.toRadians(a);  
        System.out.println(Math.tan(b));  // 1.7320508075688767
    }  
}

Also read, Understanding Java String methods with examples

Final Words

The Maths class in Java is useful to perform basic mathematical problems. Numerous methods in the Java Math class enable you to apply mathematics to numbers. All implementations of the comparable Math class function cannot be defined to deliver results that are exactly the same bit for bit, unlike some of the StrictMath class numeric methods. We have now reached the end of this tutorial. I hope the information above added to your understanding of Java. We’ll continue learning about Java in the next blogs.

Table of Contents