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.
#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;
}
