4

Looking for a library to work with large numbers on javascript (bigger than 2^53) I checked a couple of questions (JavaScript large number library? and Is there a bignum library for JavaScript?) and then tinkered a little bit with javascript-bignum.js and big.js, but the thing is that with I am unable to represent odd numbers, since both

Big(9007199254740995);

and

SchemeNumber.fn["string->number"](9007199254740995);

return

9007199254740996

rather than

9007199254740995

as I would expect.

So, is it that I am doing something wrong? Or there's no way to represent large odd numbers?

Community
  • 1
  • 1
Vier
  • 718
  • 2
  • 6
  • 19
  • 10
    *Usually* when handling big number libraries, you need to avoid the native number literals, in order not to "poison" your big number values with "pre-broken" values. Try if `Big("9007199254740995");` works any better. – Joachim Sauer May 02 '13 at 14:07
  • 3
    Some examples, using a string input and a computed result using small numbers: http://jsfiddle.net/LrtrL/3/ – apsillers May 02 '13 at 14:15

1 Answers1

8

When you say this

Big(9007199254740995)

you are not giving the bignum library a chance! Your numeric literal is first parsed by pure JS, in which that number isn't exactly representable. You can see this simply with

window.alert(9007199254740995);

which alerts 9007199254740996.

In order to let your chosen bignum library successfully represent this number, you will need to pass it as a string, for example:

Big('9007199254740995')

should get you this exact number, as a bignum.

AakashM
  • 62,551
  • 17
  • 151
  • 186