Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript使用Math.exp获得Math.sinh结果_Javascript_Math_Precision - Fatal编程技术网

javascript使用Math.exp获得Math.sinh结果

javascript使用Math.exp获得Math.sinh结果,javascript,math,precision,Javascript,Math,Precision,我只是想为数学写polyfil 这是用javascript编写jvm所必需的 但问题是Math.sinh(Double.MIN_值)=4.9E-324的java结果 而在javascript中是0,因为我使用的是polyfil,它需要Math.exp(4.9E-324) 第一个javascript将4.9E-324转换为5E-324,第二个javascript将Math.exp(4.9E-324)或Math.pow(Math.E,4.9E-324)生成1,然后生成(1-1)/2,即0:) JS中

我只是想为数学写polyfil

这是用javascript编写jvm所必需的

但问题是Math.sinh(Double.MIN_值)=4.9E-324的java结果 而在javascript中是0,因为我使用的是polyfil,它需要Math.exp(4.9E-324)

第一个javascript将4.9E-324转换为5E-324,第二个javascript将Math.exp(4.9E-324)或Math.pow(Math.E,4.9E-324)生成1,然后生成(1-1)/2,即0:) JS中的Number.MIN_值也是5E-324,相当于Double.MIN_值中的4.9E-324

是否有任何方法可以避免math.exp或math.pow或处理精度。我看过bigdecimal库,它也不工作 有没有其他方法来处理sig fig
注:我必须通过所有边界测试用例

sinh(x)的泰勒展开式是x+x^3/3+x^5/5+x^7/7!+。这将收敛于x的所有值,但对于接近0的x,收敛速度最快(并给出最佳结果)

function mySinh(x) {
    var returning = x,
        xToN = x,
        factorial = 1,
        index = 1,
        nextTerm = 1;
    while ( nextTerm != 0 ) {
        index++;
        factorial *= index;
        index++;
        factorial *= index;
        xToN *= x*x;
        nextTerm = xToN/factorial;
        returning += nextTerm;
    }
    return returning;
}
对于小于1E-108的
x
nexterm
将立即下溢到0,您只需返回
x

如果您从使用泰勒展开式切换到使用定义的
Math.exp
,最终可能会取决于您的测试用例所看到的内容

function mySinh(x) {
    var returning = x,
        xToN = x,
        factorial = 1,
        index = 1,
        nextTerm = 1;
    while ( nextTerm != 0 ) {
        index++;
        factorial *= index;
        index++;
        factorial *= index;
        xToN *= x*x;
        nextTerm = xToN/factorial;
        returning += nextTerm;
    }
    return returning;
}