×

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

Tribonacci series in Java

The Fibonacci series and the Tribonacci sequence are linked. The Fibonacci sequence, each element summates the three preceding terms, is expanded into the Tribonacci series in Java. Through specific examples,...

3 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

Contextual keywords in Java

Contextual keywords were earlier known as restricted identifiers and restricted keywords. Context keywords are chosen based on their expected placement in the syntactic grammar. These are the keywords in the code...

3 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

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.

Nonagonal number in Java

Figureate numbers with the form n(7n-5)/2 are known as nonagonal numbers. 7n+3 will be a triangular number if n is a nonagonal number. The nonagon is included in the idea...

4 minutes read.

String Concatenation in Java

In Java, it gathers a new String that combines several strings. Following are the manners to concatenate strings in Java: By + (String concatenation) operatorBy concat() method By + (String concatenation) operator Java...

4 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Coin change problem in dynamic programming

In this tutorial, we will understand a popular problem called the coin changeproblem through dynamic programming. This problem checks the logical and critical thinking ability of the person. Dynamic Programming...

5 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program. Fall through It is a situation where there isn't a break statement in...

3 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.