1 #include "compat.h"
2 
3 #if ! HAVE_ATAN2
4 # include <stdio.h>
5 # include <math.h>
6 # ifndef M_PI
7 #  define M_PI 3.1415926535898
8 # endif
9 
10 double
atan2(double y,double x)11 atan2(double y, double x)
12 {
13   double result;
14 
15   /* return a value between -PI and PI */
16   if (x*x + y*y == 0) {
17     fprintf(stderr, _("Sorry, the value atan2(0, 0) is indeterminate.\n"));
18     exit(EXIT_FAILURE);
19   }
20   if (x == 0) return (y > 0) ? M_PI / 2: -M_PI / 2;
21 
22   result = atan(y / x);
23   /* if atan() returns a result in the 2nd quadrant, put it into 4th quadrant */
24   if (result > M_PI / 2) result -= M_PI;
25 
26   if (x > 0) return result; /* should be in range -PI/2, PI/2 */
27   else {
28     if (y > 0) return result + M_PI;
29     else return result - M_PI;
30   }
31 }
32 #endif
33