How to deal with big numbers in javascript [duplicate]
Asked Answered
H

3

29

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.

Haleakala answered 26/11, 2010 at 22:3 Comment(3)
See #3072807Ambrosia
You can use npmjs.com/package/big-numbers for converting, formatting, arithmetics and trigonometric operation with big numbersMuchness
Did you ever find a library that handles floating points correctly? Linked answer is integer-only.Hydrogenize
W
11

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
Weitman answered 11/6, 2013 at 15:1 Comment(1)
Does the "BUT" for bignumber.js still hold true - is there an issue about this?Stephaniastephanie
S
2

There's a BigInteger library for JavaScript available here:

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

Stoppage answered 26/11, 2010 at 22:6 Comment(3)
+1 for an answer, but wow that script needs refactoring. Completely unnecessary global symbols all over the place, just asking for collisions.Southward
@TJ I think that one's a more-or-less direct translation of the Java BigDecimal class ... it's been around foreverAndromede
Thanks, it would be great, if it just was a little more easy to understand and extend from.Haleakala
U
0

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

Udine answered 14/12, 2010 at 21:18 Comment(1)
Sorry, but I'was looking for one that manage specially that kind of numbers. :)Haleakala

© 2022 - 2024 — McMap. All rights reserved.