×

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates.

Syntax:

public static double atan2(double y, double x)

Parameters:

The parameter ‘y’ represents the ordinate coordinate whereas ‘x’ represents the abscissa coordinate.

Return Value:

The atan2() method returns the angle theta from the conversion of rectangular coordinate to polar coordinate. Special cases of atan2() method are as follows:

  • It returns NaN, if either argument is NaN.
  • If returns positive zero, if the first argument is positive and finite and the second argument is positive infinity.
  • It returns negative zero, if the first argument is negative and finite and the second argument is positive infinity.
  • It returns the double value closest to pi, if the first argument is positive and finite and the second argument is negative infinity.
  • It returns the double value closest to -pi, if the first argument is negative and finite and the second argument is negative infinity.
  • It returns the double value closest to pi/2, if the first argument is positive infinity and the second argument is finite.
  • It returns the double value closest to -pi/2, if the first argument is negative infinity and the second argument is finite.
  • It returns the double value closest to pi/4, if the arguments are positive infinity.
  • It returns the double value closest to -3*pi/4, if the arguments are negative infinity.
  • It returns the double value closest to 3*pi/4, if the first argument is positive infinity and the second argument is negative infinity.
  • It returns the double value closest to -pi/4, if the first argument is negative infinity and the second argument is positive infinity.

Example 1:

public class JavaMathAtan2Example1 {
    public static void main(String[] args) {
        double a=30;
        double b=30;
        //return the arc tangent value for a
        System.out.println("Arc tangent value : "+Math.atan(a));
        /*returns an angle theta from the conversion of
        rectangular coordinates to polar coordinates*/
        System.out.println("Angle theta tangent value : "+Math.atan2(a,b));
    }
}

Output:

Arc tangent value : 1.5374753309166493
Angle theta tangent value : 0.7853981633974483

Example 2:

public class JavaMathAtan2Example2 {
    public static void main(String[] args) {
        double a=-0.0d/0.0d;
        double b=30;
        //returns NaN, if either argument is NaN
        System.out.println("Angle theta tangent value : "+Math.atan2(a,b));
    }
}

Output:

Angle theta tangent value : NaN

Example 3:

public class JavaMathAtan2Example3 {
    public static void main(String[] args) {
        double a=-567d;
        double b=30d/0.0d;
        /*returns a negative zero, first argument is negative and finite
         and the second argument is positive infinity. */
        System.out.println("Angle theta tangent value : "+Math.atan2(a,b));
    }
}

Output:

Angle theta tangent value : -0.0

Example 4:

public class JavaMathAtan2Example4 {
    public static void main(String[] args) {
        double a=45/0.0d;
        double b=30/0.0d;
        System.out.println("pi/4 : "+Math.PI/4);
        //returns the double value closest to pi/4, if both arguments are positive infinity.
        System.out.println("Angle theta tangent value : "+Math.atan2(a,b));
    }
}

Output:

pi/4 : 0.7853981633974483
Angle theta tangent value : 0.7853981633974483

Example 5:

public class JavaMathAtan2Example5 {
    public static void main(String[] args) {
        double a=45/0.0d;
        double b=-30/0.0d;
        System.out.println("3*pi/4 : "+3*Math.PI/4);
        /*returns the double value closest to 3*pi/4,
        if the first argument is positive infinity and the second argument is negative infinity.*/
        System.out.println("Angle theta tangent value : "+Math.atan2(a,b));
    }
}

Output:

3*pi/4 : 2.356194490192345
Angle theta tangent value : 2.356194490192345

Example 6:

public class JavaMathAtan2Example6 {
    public static void main(String[] args) {
        double a=-45/0.0d;
        double b=30/0.0d;
        System.out.println("-pi/4 : "+-Math.PI/4);
        /*returns the double value closest to -pi/4, if first argument is negative        infinity and the second argument is positive infinity. */
        System.out.println("Angle theta tangent value : ">+Math.atan2(a,b));
    }
}

Output:

-pi/4 : -0.7853981633974483
Angle theta tangent value : -0.7853981633974483

Related Topics

Zigzag Traversal of Binary Tree in Java

In this article, you will be acknowledged about the zigzag traversal of binary tree in java and the approaches or ways in which the zigzag traversal can be done. Zigzag Traversal A...

10 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Session Tracking in Java

When a series of requests from the same User (i.e., requests coming from the same browser) occurs over an extended period of time, servlets employ a mechanism known as session...

3 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

Java Pop

The array, linked list, stack, queue, and other data structures are supported by Java programming. The insertion, deletion, and element searching operations are available for every data structure. And Java...

4 minutes read.

StringBuilder in Java

StringBuilder in Java Java StringBuilder class is introduced since JDK 1.5. The StringBuilder class is mainly used to create modifiable or mutable strings. Note that the StringBuilder class is not synchronized....

7 minutes read.

Java Math max() Method

The max() method of Math class returns the greater of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double max(double a, double b)public...

2 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

How to resolve Illegal state exceptions in Java

What is IllegalStateException? When a method is called at the incorrect time, a runtime exception called an IllegalStateException is raised in Java. This exception is utilized to show that a method...

3 minutes read.

Java program to count the occurrences of each character

The count of each character is the frequency of the characters. For example, the given String is “Programming”. In the given string, the count of the characters ‘P’ -1, ’r’-2,...

6 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.