- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Math atan2(double, double) Method
Description
The Java Math atan2(double y,double x) method 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.
Declaration
Following is the declaration for java.lang.Math.atan2() method
public static double atan2(double y, double x)
Parameters
y − the ordinate coordinate
x − the abscissa coordinate
Return Value
This method returns the theta component of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.
Exception
NA
Getting Arc Tangent with Positive Value Example
The following example shows the usage of Math atan2() method with both parameters having positive values.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get a variable x which is equal to PI/2 double x = Math.PI / 2; // get a variable y which is equal to PI/3 double y = Math.PI / 3; // convert x and y to degrees x = Math.toDegrees(x); y = Math.toDegrees(y); // get the polar coordinates System.out.println("Math.atan2(" + x + "," + y + ")=" + Math.atan2(x, y)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.atan2(90.0,59.99999999999999)=0.9827937232473292
Getting Arc Tangent with Negative Value Example
The following example shows the usage of Math atan2() method with both parameters having negative values.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get a variable x which is equal to -PI/2 double x = -(Math.PI / 2); // get a variable y which is equal to -PI/3 double y = -(Math.PI / 3); // convert x and y to degrees x = Math.toDegrees(x); y = Math.toDegrees(y); // get the polar coordinates System.out.println("Math.atan2(" + x + "," + y + ")=" + Math.atan2(x, y)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.atan2(-90.0,-59.99999999999999)=-2.158798930342464
Getting Arc Tangent with First Positive and Second Negative Value Example
The following example shows the usage of Math atan2() method with first parameter is positive and second is having negative value.
package com.tutorialspoint; public class MathDemo { public static void main(String[] args) { // get a variable x which is equal to PI/2 double x = Math.PI / 2; // get a variable y which is equal to -PI/3 double y = -(Math.PI / 3); // convert x and y to degrees x = Math.toDegrees(x); y = Math.toDegrees(y); // get the polar coordinates System.out.println("Math.atan2(" + x + "," + y + ")=" + Math.atan2(x, y)); } }
Output
Let us compile and run the above program, this will produce the following result −
Math.atan2(90.0,-59.99999999999999)=2.158798930342464
To Continue Learning Please Login
Login with Google