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

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

How to deal with big numbers in javascript

javascriptfloating-pointdecimalbigintegerbignum

提问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 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
  • 高精度计算函数
  • 但是:由于数字作为字符串的内部表示,据说它很慢

回答by LukeH

There's a BigInteger library for JavaScript available here:

这里有一个适用于 JavaScript 的 BigInteger 库:

(Note that I haven't used this myself. Try it and see what you think.)

(请注意,我自己没有使用过这个。尝试一下,看看你的想法。)

回答by Jér?me Verstrynge

There is also Silent Matt's libraryfor Big Integers. It does not handle decimals.

还有 Silent Matt 的Big Integers。它不处理小数。