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.
This commit is contained in:
Damien George 2018-09-04 16:57:46 +10:00
parent 8014e7f15f
commit 0b239d458c
1 changed files with 8 additions and 1 deletions

View File

@ -1,5 +1,12 @@
#include <math.h> #include <math.h>
double tanh(double x) { 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;
} }