Javascript 如何在javascript中处理大数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4288821/
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
How to deal with big numbers in javascript
提问by crsuarezf
I'm looking for a Mathematical solution that deals with really (long, big, huge, storms) numbers. I haven't found anything yet, But I don't wanna think that this problem hasn't be solve at this time. I'm looking for an easy Number solution, like Microsoft Excel Precision (30 decimals), or a BigInteger (Java) solution. in Javascript of course.
我正在寻找一个数学解决方案来处理真正的(长的、大的、巨大的、风暴)数字。我还没有发现任何东西,但我不想认为这个问题此时还没有解决。我正在寻找一个简单的 Number 解决方案,例如 Microsoft Excel Precision(30 位小数)或 BigInteger (Java) 解决方案。当然是在 Javascript 中。
采纳答案by xscott
It looks like this has been solved before:
之前好像已经解决了这个问题:
What is the standard solution in Javascript for handling big numbers (BigNum)?
Javascript 中处理大数 (BigNum) 的标准解决方案是什么?
http://jsfromhell.com/classes/bignumber
http://jsfromhell.com/classes/bignumber
http://www-cs-students.stanford.edu/~tjw/jsbn/
http://www-cs-students.stanford.edu/~tjw/jsbn/
If you only need integers you can use BigInteger.js.
如果你只需要整数,你可以使用BigInteger.js。
回答by Torben
While looking for an big integer library for an ElGamal crypto implementation I tested several libraries with the following results:
在为 ElGamal 加密实现寻找大整数库时,我测试了几个库,结果如下:
I recommend this one:Tom Wu's jsbn.js (http://www-cs-students.stanford.edu/~tjw/jsbn/)
我推荐这个:Tom Wu 的 jsbn.js ( http://www-cs-students.stanford.edu/~tjw/jsbn/)
- Comprehensive set of functions and fast
- 功能齐全,速度快
Leemon Baird's big integer library (http://www.leemon.com/crypto/BigInt.js)
Leemon Baird 的大整数库 ( http://www.leemon.com/crypto/BigInt.js)
- Comprehensive set of functions and pretty fast
- BUT:Negative number representation is buggy!
- 功能齐全,速度相当快
- 但是:负数表示有问题!
bignumber.js (https://github.com/MikeMcl/bignumber.js)
bignumber.js ( https://github.com/MikeMcl/bignumber.js)
- Pretty complete set of functions
- BUT:Converting really big numbers from strings into BigNumber objects result in INFINITY
- 功能齐全
- 但是:将非常大的数字从字符串转换为 BigNumber 对象会导致 INFINITY
Scheme arithmetic library for JavaScript (https://github.com/jtobey/javascript-bignum)
JavaScript 的 Scheme 算法库 ( https://github.com/jtobey/javascript-bignum)
- JS-Implementation of Scheme arithmetic functions
- BUT:No function for y= x^e mod n
- JS-Scheme算术函数的实现
- 但是:y= x^e mod n 没有函数
I haven't tested this by myself:BigNumber (http://jsfromhell.com/classes/bignumber)
我自己还没有测试过这个:BigNumber ( http://jsfromhell.com/classes/bignumber)
- Functions for high precision claculations
- BUT:It's said to be slow due to internal representation of numbers as strings
- 高精度计算函数
- 但是:由于数字作为字符串的内部表示,据说它很慢