Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

google chrome - 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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...