Java StrictMath - atan2() Method
The java.lang.StrictMath.atan2() method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). The returned value will be in the range -𝜋 through 𝜋. In special cases it returns the following:
- If either argument is NaN, then the result is NaN.
- If both arguments are positive infinity, then the result is the double value closest to 𝜋/4.
- 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 𝜋.
- 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 -𝜋.
- 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 𝜋/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 -𝜋/2.
- If the first argument is positive infinity and the second argument is negative infinity, then the result is the double value closest to 3*𝜋/4.
- If the first argument is negative infinity and the second argument is positive infinity, then the result is the double value closest to -𝜋/4.
- If both arguments are negative infinity, then the result is the double value closest to -3*𝜋/4.
Syntax
public static double atan2(double y, double x)
Parameters
y |
Specify the ordinate coordinate. |
x |
Specify the abscissa coordinate. |
Return Value
Returns theta of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.
Exception
NA.
Example:
In the example below, atan2() method is used to calculate the theta of given points.
import java.lang.*; public class MyClass { public static void main(String[] args) { System.out.println(StrictMath.atan2(10, 10)); System.out.println(StrictMath.atan2(20, 10)); System.out.println(StrictMath.atan2(-20, 10)); } }
The output of the above code will be:
0.7853981633974483 1.1071487177940904 -1.1071487177940904
❮ Java.lang - StrictMath