gpt4 book ai didi

javascript - Javascript 中的快速双曲正切近似

转载 作者:可可西里 更新时间:2023-11-01 02:52:33 25 4
gpt4 key购买 nike

我正在用 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é 近似比泰勒展开好几个数量级。夹紧也可能是一个问题(取决于您的范围)。

关于javascript - Javascript 中的快速双曲正切近似,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6118028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com