JavaScript阶乘防止无穷大

JavaScript阶乘防止无穷大,javascript,math,numbers,factorial,Javascript,Math,Numbers,Factorial,我一直在使用此函数计算JavaScript中的阶乘数: var f = []; function factorial (n) { if (n == 0 || n == 1) return 1; if (f[n] > 0) return f[n]; return f[n] = factorial(n-1) * n; } 一切似乎都很顺利,直到我尝试了号码500。它返回了无穷大 有没有一种方法可以阻止无限作为答案 谢谢。500!因为没有更好的术语,它是“巨大的”

我一直在使用此函数计算JavaScript中的阶乘数:

var f = [];
function factorial (n) {
  if (n == 0 || n == 1)
    return 1;
  if (f[n] > 0)
    return f[n];
  return f[n] = factorial(n-1) * n;
}
一切似乎都很顺利,直到我尝试了号码
500
。它返回了
无穷大

有没有一种方法可以阻止
无限
作为答案


谢谢。

500!因为没有更好的术语,它是“巨大的”

它远远超出了双精度浮点所能存储的内容,而双精度浮点正是JavaScript用于数字的

除了使用合理的数字外,没有办法防止这种情况发生:p

编辑:为了向您展示它有多大,以下是答案:

500!=1220136825991110068701238785423046926253574342803192842192413588385845373153881997605496447502203281863013616477148203584163378722078177200480785205159329285477907571939330603772960859086270429174547882424912726344305670173270769461062802310452644218878789465754777149863494367781037644274033827365397471386477878495438489595537537990423241061271326984327745715546309977202781014561081188373709531016356324432987029563896628911658974769572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141625359056693423813008856249246891564126775654481886506593847951775360894005745238940335798476363944905313062323749066445048824665075946735862074637925184200459369692981022263971952597190945217823331756934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552363850942885592018727433795173014586357570828355780158735432768888680120399882384702151467605445407663535984174430480128938313896881639487469658817504506926365338175055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


右边是一个1135位的数字。为了比较,双精度浮点可以处理大约15位数的精度。

可以考虑使用任意精度的数值库。不过,这本身就是一个问题。这里有一个相关的问题:。

您确实需要使用大数字。有了它,您可以:

// configure math.js to work with enough precision to do our calculation
math.config({precision: 2000});

// evaluate the factorial using a bignumber value
var value = math.bignumber(500);
var result = math.factorial(value);

// output the results
console.log(math.format(result, {notation: 'fixed'}));
这将输出:

122013682259911100687012387854230469262537434280319284219241358838584537315388199760549644750220328186301361647714820358416337872207817720048075251532928547707577193306037729608508686767270429174788242492634457017327076946106802310452644218878787878946575771463494367787810376765153292857474747878787878787878787874545393953531326984327745715546309977202781014561081188373709531016356324432987029563896628911658974769572087926928871281780070265174507768410719624390394322536422605234945850129918571501248706961568141625359056693423813008856249246891564126775654481886506593847951775360894005745238940335798476363944905313062323749066445048824665075946735862074637925184200459369692981022263971952597190945217823331756934581508552332820762820023402626907898342451712006207714640979456116127629145951237229913340169552363850942885592018727433795173014586357570828355780158735432768888680120399882384702151467605445407663535984174430480128938313896881639487469658817504506926365338175055478128640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


我不知道是否有人已经解决了这个问题。。。 我是一个编码新手,不知道所有方面。但在我自己面对这个阶乘问题后,我来到这里寻找答案。我用另一种方法解决了“无限”显示问题。我不知道它是否很有效。但它确实显示了即使是非常高的整数的结果

对于代码中的冗余或不整洁,我深表歉意

<!DOCTYPE html>

<html>
    <head>
        <title>Factorial</title>
        <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
    </head>

    <body>
        <input type='text' id='number' />
        <input type='button' value='!Factorial!' id='btn' />

        <script>
        var reslt=1;
        var counter=0;  
        var mantissa=0; //stores the seperated matissa 
        var exponent=0; //stores the seperated exponent

            $(document).ready(function (){
                $('#btn').click(function (){
                    var num=parseFloat($('#number').val()); //number input by user

                    for(i=1;i<=num;i++){
                        reslt=reslt*i;
                        //when the result becomes so high that the exponent reaches 306, the number is divided by 1e300
                        if((parseFloat(reslt.toExponential().toString().split("e")[1]))>=300){
                            reslt=reslt/1e300; //the result becomes small again to be able to be iterated without becoming infinity
                            counter+=1; //the number of times that the number is divided in such manner is recorded by counter
                        }
                    }

                    //the mantissa of the final result is seperated first
                    mantissa=parseFloat(reslt.toExponential().toString().split("e")[0]);
                    //the exponent of the final result is obtained by adding the remaining exponent with the previously dropped exponents (1e300)
                    exponent=parseFloat(reslt.toExponential().toString().split("e")[1])+300*counter;

                    alert(mantissa+"e+"+exponent); //displays the result as a string by concatenating

                    //resets the variables and fields for the next input if any
                    $('#number').val('');
                    reslt=1;
                    mantissa=0;
                    exponent=0;
                    counter=0;
                });
            });
        </script>
    </body>
</html>

阶乘的
var reslt=1;
var计数器=0;
var尾数=0//存储单独的matissa
var指数=0//存储分离的指数
$(文档).ready(函数(){
$('#btn')。单击(函数(){
var num=parseFloat($('#number').val();//用户输入的数字
对于(i=1;i=300){
reslt=reslt/1e300;//结果再次变小,以便能够在不变为无穷大的情况下进行迭代
计数器+=1;//计数器记录以这种方式分割的次数
}
}
//最后结果的尾数首先分开
尾数=parseFloat(reslt.toExponential().toString().split(“e”)[0]);
//通过将剩余指数与先前下降的指数(1e300)相加,获得最终结果的指数
exponent=parseFloat(reslt.toExponential().toString().split(“e”)[1])+300*计数器;
警报(尾数+“e+”+指数);//通过串联将结果显示为字符串
//重置下一个输入的变量和字段(如果有)
$('数字').val('');
reslt=1;
尾数=0;
指数=0;
计数器=0;
});
});

Javascript数字只有在变为“无穷大”之前才能变大。如果你想支持更大的数字,你必须使用

示例:

//不带BigInt
console.log(100**1000)//无穷大
//带着BigInt
//(stackOverflow似乎没有打印结果,
//除非我先把它变成字符串)

console.log(String(100n**1000n))//一个非常大的数字
这是因为你有一个巨大的数字。在这里测试检查无穷大,并返回9007199254740992,而不是
if(faciarial(500)=number.POSITIVE_infinity)返回9007199254740992
最大值属性的值约为1.79E+308。大于最大值的值表示为“I