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

/** exponential function **/

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

	switch (fpclassify(x)) {
	case FP_ZERO:		return 1.0;
	case FP_INFINITE:	return signbit(x) ? 0.0 : x;
	default:		break;
	}

	if (0) {
		errno = ERANGE; /* The magnitude of ARGUMENT(x) is too large */
		/* RETURN_FAILURE(CONSTANT(HUGE_VAL), A range error occurred); */
		return TGHUGE;
	}

	/* TODO: tweak max loops */

	/* exp x = 1 + x + x^2/2! + x^3/3! + x^4/4! ... */
	for (i = 1; i < MAXLOOPS; i++) {
		power *= x;
		factorial *= i;
		exponent += power / factorial;
	}

	/* RETURN_SUCCESS(the exponential function of ARGUMENT(x)); */
	return exponent;
}

/***
compute the exponential function of ARGUMENT(x).
***/

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