Java – Find Arc Tan – atan() Method – Examples & Explanation

java.lang.Math.atan() method is used to find the arc tangent of a double value for the given input (x – parameter) in Java.
he returned angle is in the range -pi/2 through pi/2.
The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Package

atan() method exists in Math class of java.lang package.
Since: JDK1.0

 

Method Declaration of atan() Method

The declaration of atan() method is:

public static double atan(double x)

 

Syntax of atan() Method

The syntax of atan() method in Java is:

 
java.lang.Math.atan(double x)

Example

/*
*Find the arc tangent of given input(double) without importing the 
java.lang Package.
*/
public class JavaMathExample {
    public static void main(String[] args) {
        //Declaring variables
        double x = -4876.1874;
        double result;
        //Assign arc tangent of x to result variable
        result = java.lang.Math.atan(x);
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

Example
By importing java.lang package the syntax of atan() method can be used as:

/*
*Find the arc tangent of given input(double) by importing the java.lang Package.
*/
import java.lang.*;  //importing package

public class JavaMathExample {
    public static void main(String[] args) {
        
        //Declaring variables
        double x = -68.97;
        double result;
        
        //Assign arc tangent of x to result variable
        result = Math.atan(x);
        
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

 

Special Cases of atan() Method in Java

If the argument is NaN, then the result is NaN.
If the argument is zero, then the result is a zero with the same sign as the argument.
Example

/*
*This example shows some special cases of atan() method in Java
*/
import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {

        //Declaring variables
        double positiveZero = 0;
        double negativeZero = -0;
        double positiveInfinity = Double.POSITIVE_INFINITY;
        double negativeInfinity = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double result;

        //Here argument is positive zero, output will also be positive zero
        result = Math.atan(positiveZero);
        System.out.println(result);

        //Here argument is negative zero, output will be positive zero
        result = Math.atan(negativeZero);
        System.out.println(result);

        //Here argument is positive infinity, output will also be positive infinity
        result = Math.atan(positiveInfinity);
        System.out.println(result);

        //Here argument is negative infinity, output will be negative infinity
        result = Math.atan(negativeInfinity);
        System.out.println(result);

        //Here argument is NaN, output will be NaN
        result = Math.atan(nan);
        System.out.println(result);


    }

}

Output:

 

Parameters of atan() Method in Java

x – Where x is any valid double input value. This parameter is required.

 

Error Handling

If the x parameter is not a number atan() method returns an error.
If there is no argument (x – input value) passes to the method, the compiler will produce an error.Example

/*
*This example returns a compile time error
*/
import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {

        //Declaring variables
        String x = "Not a number";
        double result;

        //argument is not a number
        result = Math.atan(x);
        System.out.println(result);

        //Here no argument is used
        result = Math.atan();
        System.out.println(result);


    }

}

Output:
Error

 

Return Value of atan() Method in Java

atan() method returns the arc tangent of a double value for the given input (x – parameter).

 

Java atan() Method Example 1

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        double negativeValue = -18.869;
        double positiveValue = 27.78639;
        double zero = 0;
        System.out.println("arc tangent of a Negative Value: "+Math.atan(negativeValue));
        System.out.println("arc tangent of a Positive Value: "+Math.atan(positiveValue));
        System.out.println("arc tangent of Zero: "+Math.atan(zero));

    }
}

Output:

 

Java atan() Method Example 2

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        //Declare and initialize an array containing double values
        double test [] = { Double.MAX_VALUE, Double.MIN_VALUE, 
		Double.NaN, Double.MIN_EXPONENT, 42.867e-12, 16.852, 0, -2.58, -78.8569e7 };

        //Use for loop to print arc tangent of each element of the array
        for (int i=0; i < test.length; i++) {
            System.out.println(  Math.atan(test[i])  );
        }

    }
}

Output: