Chapter Contents

Previous

Next
atan2

atan2



Compute the Trigonometric Arc Tangent of a Quotient

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
DIAGNOSTICS
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <math.h>

double atan2(double y, double x);


DESCRIPTION

atan2 computes the angle defined by the positive x axis and a line through the point ( x,y ) to the point (0, 0). The signs of both values x and y are used to determine the quadrant of the result in a Cartesian system. The result is the inverse trigonometric tangent of y/x if x is not 0.


RETURN VALUE

Provided that it is defined and computable, the return value is the angular position of the point x,y . The return value is a double-precision, floating-point number expressed in radians and lies in the half-open interval (-[pi] ,[pi]) . For input values (0.0, y ), the return value will be either [pi]/2 or -[pi]/2 if y does not equal 0.


DIAGNOSTICS

If both x and y are 0, an error message is written to stderr and the function returns 0.0.

If an error occurs in atan2 , the _matherr routine is called. You can supply your own version of _matherr to suppress the diagnostic message or modify the value returned.


EXAMPLE

This example converts rectangular coordinates to polar coordinates:

#include <stdio.h>
#include <lcmath.h>

main()
{
   double x, y;             /* rectangular coordinates (x,y) */
   double r, angle;         /* polar coordinates (r,angle)   */

   puts("Enter the rectangular coordinates please: ");
   scanf("%lf %lf", &x, &y );

   r = sqrt((x*x) + (y*y));

      /* Compute polar coordinates (radians).                */
   angle = atan2(y , x);

      /* Convert radians to degrees.                         */
   angle = (180.0 * angle)/M_PI;
   printf("rect coords(%f,%f) -> polar coords(%f,%f)\n",
          x,y,r,angle);
}


RELATED FUNCTIONS

atan , _matherr


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.