javascript 中的非常大的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30678303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:34:50  来源:igfitidea点击:

Extremely large numbers in javascript

javascripttypesnumbersintegerbiginteger

提问by JEJoll

I'm working on the Project Euler problems (currently question 13).

我正在处理 Project Euler 问题(当前问题 13)。

For this question I have to find the first 10 digits of the sum of 100 numbers all of a size similar to this:

对于这个问题,我必须找到大小与此类似的 100 个数字之和的前 10 位数字:

91,942,213,363,574,161,572,522,430,563,301,811,072,406,154,908,250

I think I could use something like Java's BigInteger, but I started solving the problems in JavaScript (I'm trying to boost my js abilities for work), and I would like to continue using it, even to solve this problem.

我想我可以使用 Java 的 BigInteger 之类的东西,但是我开始用 JavaScript 解决问题(我正在努力提高我的 js 工作能力),我想继续使用它,甚至解决这个问题。

I'd like to stick to pure JS if possible.

如果可能的话,我想坚持使用纯 JS。

回答by bhspencer

You are going to need a javascript based BigInteger library. There are many to choose from. Here is one https://github.com/peterolson/BigInteger.js

您将需要一个基于 javascript 的 BigInteger 库。有很多可供选择。这是一个https://github.com/peterolson/BigInteger.js

You can use it like this

你可以像这样使用它

var n = bigInt("91942213363574161572522430563301811072406154908250")
    .plus("91942213363574161572522430563301811072406154908250");

回答by Qi Fan

Javascript recently got a new primitive data type BigInt(stage 4 proposal as of January 2020). https://github.com/tc39/proposal-bigint

Javascript 最近获得了一种新的原始数据类型BigInt(截至 2020 年 1 月的第 4 阶段提案)。 https://github.com/tc39/proposal-bigint

Chrome, Firefox and few other browsers have started supporting this in newer versions (check compatibility here), while other browsers are still implementing it.

Chrome、Firefox 和少数其他浏览器已开始在较新版本中支持此功能(在此处检查兼容性),而其他浏览器仍在实现它。

https://developers.google.com/web/updates/2018/05/bigint

https://developers.google.com/web/updates/2018/05/bigint

Basically it can be declared using either literals like

基本上它可以使用任何文字来声明,例如

var a = 1n;

var a = 1n;

or

或者

var b = BigInt('22222222222222222222222222222222');

var b = BigInt('22222222222222222222222222222222');

Math operators don't do auto conversion between BigInt and Number, so

数学运算符不会在 BigInt 和 Number 之间进行自动转换,因此

1 + 1n

will throw an error.

会抛出错误。

回答by Alex McMillan

You could always convert your sum to a string, rip out the .and grab the result - something like this:

你总是可以将你的总和转换为 a string,撕掉.并获取结果 - 像这样:

var sum = 2384762348723648237462348;
sum = sum.toString(); // "2.3847623487236483e+24"

// Rip out the "."
sum = sum.substr(0, 1) + sum.substr(2);

// Grab the first 10 characters
var firstTen = sum.substr(0, 10);

回答by JEJoll

Surprisingly, sticking all the values in an array and adding them all together and just taking the first 10 digits worked. I must have had a typo somewhere in my code when it didn't work before.

令人惊讶的是,将所有值粘贴在一个数组中并将它们加在一起,然后只取前 10 位数字就可以了。当我的代码之前不起作用时,我一定在某处输入了拼写错误。

I'm sure that doing something this simple wouldn't work in all cases (like those @AlexMcmillan and @zerkms have been debating about). I think the safest bet is the BigInteger library mentioned by @bhspencer, but it seems like adding the first x significant digits with y digits as a buffer might also be worth a shot in some cases.

我敢肯定,做这么简单的事情不会在所有情况下都有效(就像@AlexMcmillan 和 @zerkms 一直在争论的那样)。我认为最安全的选择是@bhspencer 提到的 BigInteger 库,但似乎在某些情况下,将前 x 位有效数字与 y 位数字添加为缓冲区也可能值得一试。

回答by cheesehead

I did this using an array and updating all entries with a function.

我使用一个数组并用一个函数更新所有条目。

function f(a) {
  for (let i = 0; i < a.length - 1; i++) {
      a[i + 1] = a[i + 1] + parseInt(a[i] / 10);
      a[i] = a[i] % 10;
  }
  return a;
}
// remember to init the array with enough elements for all digits
var a = Array(200);
a.fill(0);
a[0] = 1;

Hereis a JSFiddle with the code for problem 20.

是一个带有问题 20 代码的 JSFiddle。