在Javascript中快速双曲正切近似

前端之家收集整理的这篇文章主要介绍了在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.f2er.com/js/154061.html

猜你在找的JavaScript相关文章