Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Java atan2 Function

by suresh

The Java atan2 Function is one of the Java Math Library functions, which is to returns the angle (in radius) from the X-Axiss to the specified point (y, x).

Syntax of a Java atan2 Function

The basic syntax of the Math.atan2 in Java Programming language is as shown below.

static double atan2(double y, double x); //Return Type is Double

// In order to use in program: 
Math.atan2(double y, double x);
  • X: It can be a double value or a valid numerical expression which represent Cartesian X – Coordinate.
  • Y: It can be a double value or a valid numerical expression which represent Cartesian Y – Coordinate.

The Java Math.atan2 function will return the values between -π and π except for special cases that we specified below:

  • If any of the arguments is Not a Number, Java Math.atan2 will return NaN.
  • If the first argument is positive, zero, and the second argument is positive double value, the result is Positive Zero.
  • When the first argument is positive double value, and the second argument is positive infinity, the result is Positive Zero.
  • If the first argument is negative zero, and the second argument is negative double value, the Java atan2 result is Negative Zero.
  • If the first argument is negative double value, and the second argument is positive infinity, the result is Negative Zero.
  • When the first argument is positive, zero, and the second argument is a negative double value. Alternatively, the first argument is positive double value, and the second argument is negative infinity, the result is Closest to PI value.
  • If the first argument is negative, zero, and the second argument is a negative double value. Or the first argument is negative double value, and the second argument is negative infinity, the result is Closest to -PI value.
  • If the first argument is positive double value, and the second argument is positive zero or negative zero. Or the first argument is positive infinity, and the second argument is negative double value, the result is Closest to PI/2 value.
  • When the first argument is negative double value, and the second argument is positive zero or negative zero. Alternatively, the first argument is negative infinity, and the second argument is a finite value, the result will be Closest to -PI/2 value.
  • If both the first argument and second argument are positive infinity, then the result of Java atan2 function will be Closest to PI/4 value.
  • If the first argument is positive infinity, and the second argument is negative infinity, the result will be Closest to 3 * (PI/4) value.
  • When the first argument is negative infinity, and the second argument is positive infinity, the result will be Closest to -PI/4 value.
  • If both the first argument and second argument are Negative infinity, the result is Closest to -3 * (PI/4) value.

Java atan2 Function Example

The Math.atan2 Function in Java Programming returns the angle (in radius) from the X-Axis to the specified point (y, x). In this Java program, We are going to find the same with both positive and negative values and display the output.

TIP: Please refer to the Java tan Function article to understand the Tangent Function. And refer to the Java atan Function article to understand the Arc Tangent Function.

// Java Math.atan2 Function

package TrigonometricFunctions;

public class Atan2Method {
	public static void main(String[] args) {
		
		System.out.println("\nATAN2 result of Zero Value = " + Math.atan2(0, 10));
		System.out.println("ATAN2 result of Zero Value =  " + Math.atan2(10, 0));	
		System.out.println("\nATAN2 result of Zero Value = " + Math.atan2(0, -5));
		System.out.println("ATAN2 result of Zero Value =  " + Math.atan2(-10, 0));
		
		System.out.println("\nATAN2 result of Positive Value =  " + Math.atan2(4, 5));
		System.out.println("ATAN2 result of Positive Value =  " + Math.atan2(6, 10));

		System.out.println("\nATAN2 result of Negative Value =  " + Math.atan2(-2.50, -5.50));
		System.out.println("ATAN2 result of Negative Value =  " + Math.atan2(-6.25, -12.75));
		
		System.out.println("\nATAN2 result of Both Postive & Negative Value =  " + Math.atan2(3.50, -9.50));
		System.out.println("ATAN2 result of Both Postive & Negative Value =  " + Math.atan2(-4.25, 10.75));
		
		double x = Math.toRadians(30), y = Math.toRadians(90);
		System.out.println("\nATAN2 result of Radians = " + Math.atan2(x, y));
	}
}

OUTPUT

Java atan2 Function 1

ANALYSIS

First, We used the Math.atan2 Function with zero as one argument and Positive or negative double values as the second argument.

The following Java atan2 statements will find the angle (in radius) for the corresponding values.

System.out.println("\nATAN2 result of Zero Value = " + Math.atan2(0, 10));
System.out.println("ATAN2 result of Zero Value =  " + Math.atan2(10, 0));	
System.out.println("\nATAN2 result of Zero Value = " + Math.atan2(0, -5));
System.out.println("ATAN2 result of Zero Value =  " + Math.atan2(-10, 0));

Next, we used the Math.atan2 Function directly on Positive double values.

System.out.println("\nATAN2 result of Positive Value =  " + Math.atan2(4, 5));
System.out.println("ATAN2 result of Positive Value =  " + Math.atan2(6, 10));

Next, we used the Java atan2 Function on Negative double values.

System.out.println("\nATAN2 result of Negative Value =  " + Math.atan2(-2.50, -5.50));
System.out.println("ATAN2 result of Negative Value =  " + Math.atan2(-6.25, -12.75));

Here, we used the Java Math.atan2 Function directly on both Positive & Negative double values.

System.out.println("\nATAN2 result of Both Postive & Negative Value =  " + Math.atan2(3.50, -9.50));
System.out.println("ATAN2 result of Both Postive & Negative Value =  " + Math.atan2(-4.25, 10.75));

Next, We declared a variable of type Double and assigned the value. Here, we used the toRadians Math function to convert 30, 90 into equivalent radiant. Lastly, we used System.out.println statement to print the result as output.

double x = Math.toRadians(30), y = Math.toRadians(90);
System.out.println("\nATAN2 result of Radians = " + Math.atan2(x, y));

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy