Java - Math.atan2() Method



Description

The Java Math atan2(double y,double x) converts rectangular coordinates (x, y) to polar (r, theta). This method computes the phase theta by computing an arc tangent of y/x in the range of -pi to pi. Special cases −

  • If either argument is NaN, then the result is NaN.

  • If the first argument is positive zero and the second argument is positive, or the first argument is positive and finite and the second argument is positive infinity, then the result is positive zero.

  • If the first argument is negative zero and the second argument is positive, or the first argument is negative and finite and the second argument is positive infinity, then the result is negative zero.

  • If the first argument is positive zero and the second argument is negative, or the first argument is positive and finite and the second argument is negative infinity, then the result is the double value closest to pi.

  • If the first argument is negative zero and the second argument is negative, or the first argument is negative and finite and the second argument is negative infinity, then the result is the double value closest to -pi.

  • If the first argument is positive and the second argument is positive zero or negative zero, or the first argument is positive infinity and the second argument is finite, then the result is the double value closest to pi/2.

  • If the first argument is negative and the second argument is positive zero or negative zero, or the first argument is negative infinity and the second argument is finite, then the result is the double value closest to -pi/2.

  • If both arguments are positive infinity, then the result is the double value closest to pi/4.

  • If the first argument is positive infinity and the second argument is negative infinity, then the result is the double value closest to 3*pi/4.

  • If the first argument is negative infinity and the second argument is positive infinity, then the result is the double value closest to -pi/4.

  • If both arguments are negative infinity, then the result is the double value closest to -3*pi/4.

A result must be within 2 ulps of the correctly rounded result. Results must be semi-monotonic.

Syntax

public static double atan2(double y, double x)

Parameters

Here is the detail of parameters −

  • X − X co-ordinate in double data type.

  • Y − Y co-ordinate in double data type.

Return Value

  • This method returns theta from polar coordinate (r, theta).

Example 1

In this example, we're showing the usage of Math.atan2() method to get the atan2 based on two double numbers. We've created two double variables x and y and initialized them with a given co-ordinates. Then using Math.atan2() method we've printed the theta value.

public class Test { 
   public static void main(String args[]) {
      double x = 45.0;
      double y = 30.0;

      System.out.println( Math.atan2(x, y) );
   }
}

This will produce the following result −

Output

0.982793723247329

Example 2

In this example, we're showing the usage of Math.atan2() method to get the atan2 based on two float numbers. We've created two double variables x and y and initialized them with a given co-ordinates. Then using Math.atan2() method we've printed the theta value.

public class Test { 
   public static void main(String args[]) {
      float x = (float)45.0;
      float y = (float)30.0;

      System.out.println( Math.atan2(x, y) );
   }
}

This will produce the following result −

Output

0.982793723247329

Example 3

In this example, we're showing the usage of Math.atan2() method to get the atan2 based on two zero values. We've created two double variables x and y and initialized them with a given co-ordinates. Then using Math.atan2() method we've printed the theta value.

public class Test { 
   public static void main(String args[]) {
      double x = 0.0;
      double y = 0.0;

      System.out.println( Math.atan2(x, y) );
   }
}

This will produce the following result −

Output

0.0
java_numbers.htm
Advertisements