PatchworkOS
Loading...
Searching...
No Matches
atan2.c
Go to the documentation of this file.
1#include <math.h>
2
3// https://en.cppreference.com/w/c/numeric/math/atan2.html
4double atan2(double y, double x)
5{
6 if (y == 0.0 || y == -0.0)
7 {
8 if (x < 0.0 || x == -0.0)
9 {
10 return (y < 0.0) ? -M_PI : M_PI;
11 }
12 else
13 {
14 return (y < 0.0) ? -0.0 : 0.0;
15 }
16 }
17
18 if (isinf(y))
19 {
20 if (!isinf(x))
21 {
22 return (y < 0.0) ? -M_PI_2 : M_PI_2;
23 }
24 else if (x < 0.0)
25 {
26 return (y < 0.0) ? -3.0 * M_PI_4 : 3.0 * M_PI_4;
27 }
28 else
29 {
30 return (y < 0.0) ? -M_PI_4 : M_PI_4;
31 }
32 }
33
34 if (x == 0.0 || x == -0.0)
35 {
36 return (y < 0.0) ? -M_PI_2 : M_PI_2;
37 }
38
39 if (isinf(x))
40 {
41 if (x < 0.0)
42 {
43 return (y < 0.0) ? -M_PI : M_PI;
44 }
45 else
46 {
47 return (y < 0.0) ? -0.0 : 0.0;
48 }
49 }
50
51 if (isnan(x) || isnan(y))
52 {
53 return NAN;
54 }
55
56 double angle = atan(fabs(y / x));
57
58 if (x > 0.0)
59 {
60 return (y < 0.0) ? -angle : angle;
61 }
62 else
63 {
64 return (y < 0.0) ? -M_PI + angle : M_PI - angle;
65 }
66}
double atan2(double y, double x)
Definition atan2.c:4
double atan(double x)
Definition atan.c:5
#define isinf(x)
Definition math.h:37
#define isnan(x)
Definition math.h:39
#define NAN
Definition math.h:20
double fabs(double x)
Definition fabs.c:3
#define M_PI_2
Definition math.h:12
#define M_PI
Definition math.h:11
#define M_PI_4
Definition math.h:13
int64_t x
Definition main.c:152
int64_t y
Definition main.c:153