Oracle ATAN2 Function: A Simple Guide

The ATAN2 function in Oracle SQL is a powerful trigonometric function. It's the "2-argument" version of the arc tangent.

While ATAN(n) calculates the arc tangent of a single number (a ratio), ATAN2(n1, n2) calculates the arc tangent of n1 / n2, but it uses the signs of both arguments to determine the correct quadrant (between -pi and pi) for the resulting angle.

It's most commonly used to find the angle (in radians) of a point (n2, n1) from the origin in a 2D plane.

What is the ATAN2 Function in Oracle?

The ATAN2 function is used to find the full-range arc tangent (from -pi to pi). This is more robust than ATAN, which only returns values from -pi/2 to pi/2.

  • Input Range: The input numbers n1 and n2 can be any number.
  • Output Range: The function returns the angle as a number in radians, which will always be between -pi and pi (approximately -3.14159 to 3.14159).
  • Key Difference: ATAN2(1, 1) and ATAN2(-1, -1) give very different results, even though the ratio 1/1 and -1/-1 is the same. ATAN2 understands they are in different quadrants.

ATAN2 Function Syntax

The syntax for ATAN2 is:

ATAN2(n1, n2)

Let's break that down:

  • n1: This is treated as the 'y' coordinate or the numerator.
  • n2: This is treated as the 'x' coordinate or the denominator.

Oracle ATAN2 Function Examples

Here are two practical examples of how to use ATAN2.

Example 1: Finding the Arc Tangent of Two Positive Numbers

This example calculates the arc tangent for the point (0.2, 0.3), which is in the first quadrant.

Query:

SELECT 
  ATAN2(0.3, 0.2) AS "ArcTangent_2"
FROM DUAL;

Result:

ArcTangent_2
------------
  .982793723

This is the angle (in radians) for the point (0.2, 0.3).

Example 2: Finding an Angle in a Different Quadrant

Let's find the angle for the point (-1, 1). The ATAN function for the ratio 1 / -1 (which is -1) would return -0.785 (or -45 degrees). ATAN2 knows the point is in the second quadrant.

Query:

SELECT 
  ATAN2(1, -1) AS "Quadrant_2_Angle"
FROM DUAL;

Result:

Quadrant_2_Angle
----------------
      2.35619449

This is the angle (in radians) for the point (-1, 1), which is 135 degrees (or 3pi/4), correctly placing it in the second quadrant.*

Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 25+ years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments