google chrome - javascript large integer round because precision? (why?) - Stack Overflow

If you dofor(var i = 0; i < 30; i++){console.log(i + " == " + 78764357878563800 + "

If you do

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

You start paring 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.

If you do

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

You start paring 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.
Share Improve this question edited Aug 12, 2021 at 12:28 Kristie 13210 bronze badges asked Apr 27, 2011 at 23:56 tyoc213tyoc213 1,25318 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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.

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 e 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".

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744182145a4562031.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信