atan2() in Java

atan2(): This method is available in the Math class of Java.

Syntax:

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

Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (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: 

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

2. 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 (0).

3.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 (0).

4. 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 or (π).

5.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 or (-π).

6.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 or (π/2).

7.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 or (-π/2)

8.If both arguments are positive infinity, then the result is the double value closest to pi/4 or (π/4).

9.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 or (3*π/4).

10.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 or (-π/4).

11.If both arguments are negative infinity, then the result is the double value closest to -3*pi/4 or (-3*π/4).

The computed result must be within 2 ulps of the exact result. Results must be semi-monotonic. 

Parameters: Two parameters are required for this method.

y: the ordinate coordinate.

x:the abscissa coordinate.

Returns: the theta component of the point(r, theta)in polar coordinates that corresponds to the point(x, y) in Cartesian coordinates.

Approach

Java


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

        double y = 10, x = 3;
        System.out.println(Math.atan2(y, x));

    }
}

Output:

1.2793395323170296


No comments:

Post a Comment