开发者

How to deal with big numbers in javascript [duplicate]

开发者 https://www.devze.com 2023-01-27 07:33 出处:网络
This question already has an answer here: 开发者_开发技巧 What is the standard solution in JavaScript for handling big numbers (BigNum)?
This question already has an answer here: 开发者_开发技巧 What is the standard solution in JavaScript for handling big numbers (BigNum)? (1 answer) Closed 8 years ago.

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.


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


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.)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消