微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Javascript中快速双曲正切近似

我在 javascript中进行一些数字信号处理计算,我发现计算双曲正切(tanh)有点太贵了.这是我现在如何近似于tanh:
function tanh (arg) {
    // sinh(number)/cosh(number)
    return (Math.exp(arg) - Math.exp(-arg)) / (Math.exp(arg) + Math.exp(-arg));
}

任何人都知道更快的计算方法吗?

解决方法

here.
function rational_tanh(x)
{
    if( x < -3 )
        return -1;
    else if( x > 3 )
        return 1;
    else
        return x * ( 27 + x * x ) / ( 27 + 9 * x * x );
}

This is a rational function to
approximate a tanh-like soft clipper.
It is based on the pade-approximation
of the tanh function with tweaked
coefficients.

The function is in the range x=-3..3
and outputs the range y=-1..1. Beyond
this range the output must be clamped
to -1..1.

The first to derivatives of the
function vanish at -3 and 3,so the
transition to the hard clipped region
is C2-continuous.

Padé近似值比泰勒扩展值更好.夹紧也可能是一个问题(取决于您的范围).

原文地址:https://www.jb51.cc/js/154061.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐