summaryrefslogtreecommitdiff
path: root/src/math/exp.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-02-08 18:42:39 -0500
committerJakob Kaivo <jkk@ung.org>2019-02-08 18:42:39 -0500
commit7ef8a7379f7f7d09e71ccae2a0b688c3cd80423f (patch)
tree092ab0aed1769117fd7b28b8592f6f96b0e0d5af /src/math/exp.c
parent6acf19370e8adff79cd83b257d3f04aeaf2a59dd (diff)
merge sources into single tree
Diffstat (limited to 'src/math/exp.c')
-rw-r--r--src/math/exp.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/math/exp.c b/src/math/exp.c
new file mode 100644
index 00000000..b63ecf4f
--- /dev/null
+++ b/src/math/exp.c
@@ -0,0 +1,44 @@
+# define TGSOURCE "exp.c"
+#include <math.h>
+#include "nonstd/tgmath.h"
+#include "errno.h"
+
+/** exponential function **/
+TYPE TGFN(exp)(TYPE x)
+{
+ int MAXLOOPS = 10;
+ int i;
+ int factorial = 1;
+ TYPE exponent = 1.0;
+ TYPE power = 1.0;
+
+ 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)
+*/