How to deal with big numbers in javascript [duplicate]

我的梦境 提交于 2019-11-25 23:26:05

问题


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.


回答1:


It looks like this has been solved before:

What is the standard solution in Javascript for handling big numbers (BigNum)?

http://jsfromhell.com/classes/bignumber

http://www-cs-students.stanford.edu/~tjw/jsbn/

If you only need integers you can use BigInteger.js.




回答2:


While looking for an big integer library for an ElGamal crypto implementation I tested several libraries with the following results:

I recommend this one: Tom Wu's 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)

  • Comprehensive set of functions and pretty fast
  • BUT: Negative number representation is buggy!

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

Scheme arithmetic library for JavaScript (https://github.com/jtobey/javascript-bignum)

  • JS-Implementation of Scheme arithmetic functions
  • BUT: No function for y= x^e mod n

I haven't tested this by myself: 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



回答3:


There's a BigInteger library for JavaScript available here:

  • jsbn.js

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




回答4:


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



来源:https://stackoverflow.com/questions/4288821/how-to-deal-with-big-numbers-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!