summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-09-04 16:57:46 +1000
committerDamien George <damien.p.george@gmail.com>2018-09-04 16:57:46 +1000
commit0b239d458cb87176089f046756e5deebe24814eb (patch)
tree2fedaa5af8526ca29e1784e2c653afe4f70b198f
parent8014e7f15f0fcc40966fd6e9a7d05a8c5102f66e (diff)
lib/libm_dbl/tanh: Make tanh more efficient and handle large numbers.
Prior to this patch tanh(large number) would return nan due to inf/inf.
-rw-r--r--lib/libm_dbl/tanh.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/lib/libm_dbl/tanh.c b/lib/libm_dbl/tanh.c
index 89743ba90..6bdb7c399 100644
--- a/lib/libm_dbl/tanh.c
+++ b/lib/libm_dbl/tanh.c
@@ -1,5 +1,12 @@
#include <math.h>
double tanh(double x) {
- return sinh(x) / cosh(x);
+ int sign = 0;
+ if (x < 0) {
+ sign = 1;
+ x = -x;
+ }
+ x = expm1(-2 * x);
+ x = x / (x + 2);
+ return sign ? x : -x;
}