在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é近似值比泰勒扩展值更好.夹紧也可能是一个问题(取决于您的范围).

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...