summaryrefslogtreecommitdiffstatshomepage
path: root/lib
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-09-04 16:59:08 +1000
committerDamien George <damien.p.george@gmail.com>2018-09-04 17:00:30 +1000
commitafc7ddca311e9c5d785537bbadb86cabf8634bea (patch)
tree2e9ea4c8cb67f4d13f92403b9bb35487ff78bed7 /lib
parent0b239d458cb87176089f046756e5deebe24814eb (diff)
downloadmicropython-afc7ddca311e9c5d785537bbadb86cabf8634bea.tar.gz
micropython-afc7ddca311e9c5d785537bbadb86cabf8634bea.zip
lib/libm/math: Make tanhf more efficient and handle large numbers.
Prior to this patch tanhf(large number) would return nan due to inf/inf.
Diffstat (limited to 'lib')
-rw-r--r--lib/libm/math.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/libm/math.c b/lib/libm/math.c
index 6b65202cf0..d2c3949957 100644
--- a/lib/libm/math.c
+++ b/lib/libm/math.c
@@ -52,10 +52,14 @@ static const float _M_LN10 = 2.30258509299404; // 0x40135d8e
float log10f(float x) { return logf(x) / (float)_M_LN10; }
float tanhf(float x) {
- if (isinf(x)) {
- return copysignf(1, x);
+ int sign = 0;
+ if (x < 0) {
+ sign = 1;
+ x = -x;
}
- return sinhf(x) / coshf(x);
+ x = expm1f(-2 * x);
+ x = x / (x + 2);
+ return sign ? x : -x;
}
/*****************************************************************************/