# Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates.

Syntax:

`public static double atan2(double y, double x)`

Parameters:

The parameter ‘y’ represents the ordinate coordinate whereas ‘x’ represents the abscissa coordinate.

Return Value:

The atan2() method returns the angle theta from the conversion of rectangular coordinate to polar coordinate. Special cases of atan2() method are as follows:

• It returns NaN, if either argument is NaN.
• If returns positive zero, if the first argument is positive and finite and the second argument is positive infinity.
• It returns negative zero, if the first argument is negative and finite and the second argument is positive infinity.
• It returns the double value closest to pi, if the first argument is positive and finite and the second argument is negative infinity.
• It returns the double value closest to -pi, if the first argument is negative and finite and the second argument is negative infinity.
• It returns the double value closest to pi/2, if the first argument is positive infinity and the second argument is finite.
• It returns the double value closest to -pi/2, if the first argument is negative infinity and the second argument is finite.
• It returns the double value closest to pi/4, if the arguments are positive infinity.
• It returns the double value closest to -3*pi/4, if the arguments are negative infinity.
• It returns the double value closest to 3*pi/4, if the first argument is positive infinity and the second argument is negative infinity.
• It returns the double value closest to -pi/4, if the first argument is negative infinity and the second argument is positive infinity.

Example 1:

```public class JavaMathAtan2Example1 {
public static void main(String[] args) {
double a=30;
double b=30;
//return the arc tangent value for a
System.out.println("Arc tangent value : "+Math.atan(a));
/*returns an angle theta from the conversion of
rectangular coordinates to polar coordinates*/
System.out.println("Angle theta tangent value : "+Math.atan2(a,b));
}
}```

Output:

```Arc tangent value : 1.5374753309166493
Angle theta tangent value : 0.7853981633974483```

Example 2:

```public class JavaMathAtan2Example2 {
public static void main(String[] args) {
double a=-0.0d/0.0d;
double b=30;
//returns NaN, if either argument is NaN
System.out.println("Angle theta tangent value : "+Math.atan2(a,b));
}
}```

Output:

`Angle theta tangent value : NaN`

Example 3:

```public class JavaMathAtan2Example3 {
public static void main(String[] args) {
double a=-567d;
double b=30d/0.0d;
/*returns a negative zero, first argument is negative and finite
and the second argument is positive infinity. */
System.out.println("Angle theta tangent value : "+Math.atan2(a,b));
}
}```

Output:

`Angle theta tangent value : -0.0`

Example 4:

```public class JavaMathAtan2Example4 {
public static void main(String[] args) {
double a=45/0.0d;
double b=30/0.0d;
System.out.println("pi/4 : "+Math.PI/4);
//returns the double value closest to pi/4, if both arguments are positive infinity.
System.out.println("Angle theta tangent value : "+Math.atan2(a,b));
}
}```

Output:

```pi/4 : 0.7853981633974483
Angle theta tangent value : 0.7853981633974483```

Example 5:

```public class JavaMathAtan2Example5 {
public static void main(String[] args) {
double a=45/0.0d;
double b=-30/0.0d;
System.out.println("3*pi/4 : "+3*Math.PI/4);
/*returns the double value closest to 3*pi/4,
if the first argument is positive infinity and the second argument is negative infinity.*/
System.out.println("Angle theta tangent value : "+Math.atan2(a,b));
}
}```

Output:

```3*pi/4 : 2.356194490192345
Angle theta tangent value : 2.356194490192345```

Example 6:

```public class JavaMathAtan2Example6 {
public static void main(String[] args) {
double a=-45/0.0d;
double b=30/0.0d;
System.out.println("-pi/4 : "+-Math.PI/4);
/*returns the double value closest to -pi/4, if first argument is negative        infinity and the second argument is positive infinity. */
System.out.println("Angle theta tangent value : ">+Math.atan2(a,b));
}
}```

Output:

```-pi/4 : -0.7853981633974483
Angle theta tangent value : -0.7853981633974483```