summaryrefslogtreecommitdiff
path: root/src/math/atan2.c
blob: f1cbe879c6806f61e70144550532c9c9993d4bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# define TGSOURCE "atan2.c"
#include <errno.h>
#include <math.h>
#include "_safety.h"
#include "_tgmath.h"

/** arc tangent **/

TYPE TGFN(atan2)(TYPE y, TYPE x)
{
	SIGNAL_SAFE(0);
	int classy = fpclassify(y);
	int classx = fpclassify(x);
	ASSERT_NONZERO(x);

	if (classy == FP_ZERO && classx == 0) {
		if (signbit(x)) {
			return copysign(M_PI, y);
		} else {
			return y;
		}
	}

	if (classy == FP_ZERO) {
		if (x < 0) {
			return copysign(M_PI, y);
		} else if (x > 0) {
			return y;
		}
	}

	if (classx == FP_ZERO) {
		if (y < 0) {
			return - M_PI_2;
		} else if (y > 0) {
			return M_PI_2;
		}
	}

	if (classx == FP_INFINITE) {
		if (classy == FP_INFINITE) {
			if (signbit(x)) {
				return copysign(3.0 * M_PI / 4.0, y);
			} else {
				return copysign(M_PI / 4.0, y);
			}
		} else if (y > 0) {
			if (signbit(x)) {
				return copysign(M_PI, y);
			} else {
				return copysign(0.0, y);
			}
		}
	}

	if (classy == FP_INFINITE && classx != FP_INFINITE) {
		return copysign(M_PI_2, y);
	}

	if (y == 0 && x == 0) {
		errno = EDOM; /* ARGUMENT(y) and ARGUMENT(x) are both LITERAL(0)) */
		return TGHUGE;
	}

	if (0) {
		errno = ERANGE; /* The result cannot be represented */
		/* RETURN_FAILURE(CONSTANT(HUGE_VAL), A range error occurred); */
		return TGHUGE;
	}

	TYPE ret = TGFN(tan)(y / x);
	/* FIXME: quadrant */
	/* RETURN_SUCCESS(a value in range `[-PI(), +PI()]'); */
	return ret;
}

/***
functions compute the principal value of the arc tangent of
ARGUMENT(y)/ARGUMENT(x), using the signs of both to place the return value in
the correct quadrant.
***/

/*
IMPLEMENTATION(The value returned on a domain error, CONSTANT(HUGE_VAL))
LINK(m)
STDC(1)
*/