Open In App

Java atan() Method

Last Updated : 27 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Math.atan() method in Java is used to calculate the arc tangent of a given value. This method is part of the java.lang.Math package. In this article, we are going to discuss the working of this method with regular values and for special cases such as infinity and NaN. This method is very useful when we have to work with angles.

Special Cases:

  • If the argument is NaN, then the result will be NaN.
  • If the argument is +0.0, the result will be +0.0
  • If the argument is -0.0, the result will be -0.0
  • If the argument is positive infinity, the result will be π/2.
  • If the argument is negative infinity, the result will be -π/2.

Syntax of atan() Method

The syntax of the atan() method is:

public static double atan(double a)

  • Parameter: This method takes a single parameter "a", of type double, for which we are calculating the arc tangent.
  • Return Type: This method returns the arc tangent of the argument.

Examples of Java atan() Method

Now, we are going to discuss some examples for better understanding.

Example 1: In this example, we will see the basic usage of the Math.atan() method.

Java
// Java Program to demonstrate the working of 
// Math.atan() method with regular values
import java.lang.Math;

public class Geeks {
    
    public static void main(String[] args) {
        
        double a = Math.PI;
        System.out.println("atan(pi) = " + Math.atan(a)); 

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

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

        double e = -0.0;
        System.out.println("atan(-0.0) = " + Math.atan(e));

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

        System.out.println("atan(NaN) = " + Math.atan(Double.NaN)); 
        System.out.println("atan(+Infinity) = " + Math.atan(Double.POSITIVE_INFINITY));
        System.out.println("atan(-Infinity) = " + Math.atan(Double.NEGATIVE_INFINITY)); 
    }
}

Output:

Output

Explanation: In the above example, we are calculating the atan for different types of values.


Example 2: In this example, we will use atan() method to handle infinity and NaN.

Java
// Java program to demonstrate 
// handling NaN and Infinity
import java.lang.Math;

public class Geeks {
    
    public static void main(String[] args) {
        
        // Positive Infinity
        double p = Double.POSITIVE_INFINITY;
        System.out.println("atan(+Infinity) = " + Math.atan(p)); 

        // Negative Infinity
        double n = Double.NEGATIVE_INFINITY;
        System.out.println("atan(-Infinity) = " + Math.atan(n));

        // NaN (Not a Number)
        double n1 = Double.NaN;
        System.out.println("atan(NaN) = " + Math.atan(n1)); 

        // NaN generated from 0.0 / 0.0
        double n2 = 0.0 / 0.0;  
        System.out.println("atan(0.0 / 0.0) = " + Math.atan(n2)); 
    }
}

Output
atan(+Infinity) = 1.5707963267948966
atan(-Infinity) = -1.5707963267948966
atan(NaN) = NaN
atan(0.0 / 0.0) = NaN

Explanation: In the above example, we are handling the cases where the input value is positive infinity and negative infinity and NaN.


Next Article

Similar Reads