Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript large integer round because precision? (why?)

If you do

for(var i = 0; i < 30; i++){console.log(i + " == " + 78764357878563800 + " ? ");console.log((78764357878563790+i) == 78764357878563800);}

You start comparing values from 78764357878563790 to 78764357878563790+29, so... like you see if you run it, you get true from i = 8 to 24.

So I don't know if I don't get it, but isn't supposed to exist the maxint (which I dont know) and a maxint-1??? and then max be different than maxint-1?

I suppose this is precision like floating numbers... but that isn't supposed to only hit floating numbers and for example, number+1 always gives the successor? (so in the example above, if i = 78764357878563790 and add i++, then you enter an infinite loop.

I know before hand that this type of spacing between numbers exist for floating points, but never hit a case where also the integers cant represent i+1 and i-1 (I always thinked that unsigned maxint + 1 would carry and give 0).

Any suguestions in:

  • which is the max number and what is the anterior number in js.
  • how to handle or know when this behaviour for integers will start happening.
  • how to handle this large numbers and more big than this would be nice.
like image 762
tyoc213 Avatar asked Jan 19 '23 21:01

tyoc213


2 Answers

There are no integers in Javascript.

Numbers are double precision floating point, which gives you a precision of 15-16 digits. This is consistent with your results.

like image 120
Guffa Avatar answered Jan 31 '23 07:01

Guffa


As I suspected... so there is no integers in JS.

So that is you don't have integers in js like you would spec in other langs (they are thus most like an alias), altought they are confusing if you come from other language that has int and unsigned int and know the behaviour in the back.

So for handle big ints, I suguest to myself something like

if(someInt+1 == someInt || someInt-1 == someInt) { //use big number }

Or something like that, so far I only searched for BigInt libs in js, found one result here for integers

  • Huge Integer JavaScript Library

And for floating point

  • Arbitrary precision Float numbers on JavaScript
  • Java floating point high precision library

So I have my 3 questions ansered.

  • which is the max number and what is the anterior number in js. The same than a Float can give... but you will start getting gaps in some place... that is adding +1 to it will give the same number instead of consecutive.... so you have 2 "max ints" the max int that you can have adding +1 and get the next number and the max integer that is hable to give a 64-bit floating point number.

  • how to handle or know when this behaviour for integers will start happening.

  • how to handle this large numbers and more big than this would be nice.

Use one of the 2 above: a big int library or the check "someInt+1 == someInt || someInt-1 == someInt".

like image 22
tyoc213 Avatar answered Jan 31 '23 05:01

tyoc213