summaryrefslogtreecommitdiff
path: root/src/math/cos.c
blob: b8b29aa0711692625191da8f70f1c2db2e52eaf2 (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
# define TGSOURCE "cos.c"
#include <errno.h>
#include <math.h>
#include "_tgmath.h"

/** cosine **/

TYPE TGFN(cos)(TYPE x)
{
	int MAXLOOPS = 10;
	int factorial = 1;
	int i;
	TYPE cosine = 1.0;
	TYPE power = 1.0;

	switch (fpclassify(x)) {
	case FP_ZERO:		return 1.0;
	case FP_INFINITE:	feraiseexcept(FE_INVALID); return NAN;
	default:		break;
	}

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

	/* TODO: make sure x is in the right domain */
	/* TODO: tweak number of loops */

	/* cos x = 1 - x^2/2! + x^4/4! -x^6/6! ... */
	for (i = 1; i < MAXLOOPS; i++) {
		power = power * x;
		factorial = factorial * i;

		if (i % 4 == 0) {
			cosine += power / factorial;
		} else if (i % 4 == 2) {
			cosine -= power / factorial;
		}
	}

	/* RETURN_SUCCESS(the cosine of ARGUMENT(x)); */
	return cosine;
}

/***
compute cosine of ARGUMENT(x), in radians.
***/

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