Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / math / atan2.c
1 /*
2  * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  *
5  * Author: Ceriel J.H. Jacobs
6  */
7 /* $Id: atan2.c,v 1.3 1994/06/24 11:43:15 ceriel Exp $ */
8
9 #include        <math.h>
10 #include        <errno.h>
11 #include        "localmath.h"
12
13 double
14 atan2(double y, double x)
15 {
16         double absx, absy, val;
17
18         if (x == 0 && y == 0) {
19                 errno = EDOM;
20                 return 0;
21         }
22         absy = y < 0 ? -y : y;
23         absx = x < 0 ? -x : x;
24         if (absy - absx == absy) {
25                 /* x negligible compared to y */
26                 return y < 0 ? -M_PI_2 : M_PI_2;
27         }
28         if (absx - absy == absx) {
29                 /* y negligible compared to x */
30                 val = 0.0;
31         }
32         else    val = atan(y/x);
33         if (x > 0) {
34                 /* first or fourth quadrant; already correct */
35                 return val;
36         }
37         if (y < 0) {
38                 /* third quadrant */
39                 return val - M_PI;
40         }
41         return val + M_PI;
42 }