Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I get big number libraries for javascript to work?

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?

like image 444
Vier Avatar asked Feb 16 '23 15:02

Vier


1 Answers

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.

like image 106
AakashM Avatar answered Feb 23 '23 01:02

AakashM