Join our newsletter for the latest updates.

Java Math atan()

The Java Math atan() method returns the arc tangent of the specified value.

The arc tangent is the inverse of the tangent function.

The syntax of the atan() method is:

Math.atan(double num)

Here, atan() is a static method. Hence, we are accessing the method using the class name, Math.


atan() Parameters

The atan() method takes a single parameter.

  • num - number whose inverse tangent function is to be returned

atan() Return Value

  • returns the inverse tangent of the specified number
  • returns 0 if the specified value is zero
  • returns NaN if the specified number is NaN

Note: The returned value is an angle between -pi/2 to pi/2.


Example 1: Java Math atan()

import java.lang.Math;

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

    // create variable
    double a = 0.99;
    double b = 2.0;
    double c = 0.0;

    // print the arc tangent value
    System.out.println(Math.atan(a));  // 0.7803730800666359
    System.out.println(Math.atan(b));  // 1.1071487177940904
    System.out.println(Math.atan(c));  // 0.0
  }
}

In the above example, we have imported the java.lang.Math package. This is important if we want to use methods of the Math class. Notice the expression,

Math.atan(a)

Here, we have directly used the class name to call the method. It is because atan() is a static method.


Example 2: Math atan() Returns NaN

import java.lang.Math;

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

    // create variable
    // square root of negative number
    // results in not a number (NaN)
    double a = Math.sqrt(-5);

    // print the arc tangent  value
    System.out.println(Math.atan(a));  // NaN
  }
}

Here, we have created a variable named a.

  • Math.atan(a) - returns NaN because square root of a negative number (-5) is not a number

Note: We have used the Java Math.sqrt() method to compute the square root of a number.