# Java – Find Angle Theta – atan2() Method – Examples & Explanation

`java.lang.Math.atan2()` method is used to find the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) of a double value for the given input (`x` – parameter) in Java.
This method computes the phase theta by computing an arc tangent of y/x in the range of -pi to pi.
The computed result must be within 2 ulps of the exact result. Results must be semi-monotonic.

## Package

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

## Method Declaration of `atan2()` Method

The declaration of `atan2()` method is:

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

## Syntax of `atan2()` Method

The syntax of `atan2()` method in Java is:

```

java.lang.Math.atan2(double x, double y)
```

Example

```/*
*Find the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) 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 angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) of x to result variable
result = java.lang.Math.atan2(x);
//Printing result to the console
System.out.println(result);
}
}
```

Output:

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

```/*
*Find the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) 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 angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) of x to result variable
result = Math.atan2(x);

//Printing result to the console
System.out.println(result);
}
}
```

Output:

## Special Cases of `atan2()` Method in Java

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.
Example

```/*
*This example shows some special cases of atan2() 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.atan2(positiveZero);
System.out.println(result);

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

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

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

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

}

}
```

Output:

## Parameters of `atan2()` Method in Java

`x`, `y` – Where `x` and `y` are any valid double input values. These parameters are required.
y – the ordinate coordinate
x – the abscissa coordinate

## Error Handling

If `x` and `y` parameters are not a number `atan2()` method will return an `error`.
If there are no arguments(`x` and `y` – input value) pass 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.atan2(x);
System.out.println(result);

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

}

}
```

Output:
`Error`

## Return Value of `atan2()` Method in Java

`atan2()` method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) of a double value for the given inputs (`x`, `y` – parameters).
the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.

## Java `atan2()` 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("angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) of a Negative Value: "+Math.atan2(negativeValue));
System.out.println("angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) of a Positive Value: "+Math.atan2(positiveValue));
System.out.println("angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) of Zero: "+Math.atan2(zero));

}
}
```

Output:

## Java `atan2()` 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 angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) of each element of the array
for (int i=0; i < test.length; i++) {
System.out.println(  Math.atan2(test[i])  );
}

}
}
```

Output: