Java Math atan2() Method

java math atan2() method

Java Math Class

Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), abs() etc.
The java.lang.Math class contains various methods for performing basic numeric operations

Here, in the page we will discuss about the atan2() Method in java.

Java Math atan2() Method :

Returns the angle theta resulting from the transformation of polar coordinates (r, theta) into rectangular coordinates (x, y). By calculating an arc tangent of y/x in the range of -pi to pi, this technique calculates the phase theta.

Special Case :

  • The result is NaN if either of the arguments is NaN.
  • If both the first and second arguments are true, then the result is positive. the outcome is a positive zero.
  • The outcome is negative zero if the first argument is negative zero and the second argument is positive.
  • The result is the double value that is closest to pi if the first argument is positive zero and the second argument is negative.
  • The result is the double value that is closest to -pi if both the first and second arguments are negative.
  • The result is the double value that is closest to pi/2 if the first argument is positive and the second argument is positive zero or negative zero.
  • The result is the double value that is closest to -pi/2 if the first argument is negative and the second argument is either positive zero or negative zero.
  • The double value that is closest to pi/4 is the result if both arguments are positive infinity.
  • The result is the double value that is closest to 3*pi/4 if the first argument is positive infinity and the second argument is negative infinity.
  • The result is the double value that is closest to -pi/4 if the first argument is negative infinity and the second argument is positive infinity.
  • The outcome is the double value that is closest to -3*pi/4 if either or both arguments are negative infinity.

Syntax  :

public static double atan2(double y,double x)

clear() Function in ArrayList

clone() Function in ArrayList

add() Function in ArrayList

addall() Function in ArrayList

Example 1 for Positive Numbers :  

Run
import java.util.*;
public class Main {


	public static void main(String args[])
	{

		//coordinates of X and Y axis.
		double x = 90.0;
		double y = 45.0;

		//calculating the angle
		double theta = Math.atan2(y, x);
        
        //outputing the angle
		System.out.println(theta);
	}
}

Output :

0.4636476090008061

Example 2 for Negative Numbers :  

Run
import java.util.*;
public class Main {


	public static void main(String args[])
	{

		//coordinates of X and Y axis.
		double x = -90.0;
		double y = -45.0;

		//calculating the angle
		double theta = Math.atan2(y, x);
        
                //outputing the angle
		System.out.println(theta);
	}
}

Output :

-2.677945044588987

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription