JavaScript大整数轮因为精度? (为什么?) [英] javascript large integer round because precision? (why?)

查看:117
本文介绍了JavaScript大整数轮因为精度? (为什么?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在chrome或safari中输入78764357878563812,那么您可以获得

如果您这样做



<$ p $ (i = 0; i <30; i ++){console.log(i +==+ 78764357878563800 +?); console.log((78764357878563790 + i) == 78764357878563800);}

您将78764357878563790与78764357878563790 + 29进行比较,所以...就像你看到你是否运行它一样,你从i = 8变为24。



所以我不知道我是否得到它,但不应该存在maxint(我不知道)和maxint-1?然后最大不同于maxint-1?



我认为这是像浮动数字一样的精确度......但是这不应该只是浮动数字,例如数字+ 1总是给继任者? (所以在上面的例子中,如果我= 78764357878563790并添加了i ++,那么你输入了一个无限循环。



我之前知道这种类型的数字间隔存在浮点数,但从来没有遇到过整数不能代表i + 1和i-1的情况(我一直认为无符号maxint + 1会携带并给出0)。

<

$ b


  • 这是最大数字,js中的前数字是什么。

  • 如何处理或知道整数行为何时开始发生。

  • 如何处理这个大数字,并且比这更大。


解决方案

Javascript中没有整数。

双精度浮点数,它提供15-16位数的精度,这与您的结果一致。


if you type 78764357878563812 in chrome or in safari, then you get

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 dont know if I dont get it, but isn't supposed to exist the maxint (which I dont know) and a maxint-1??? and then max be diferent than maxint-1?

I suppose this is precision like floating numbers... but that isnt supposed to only hit floating numbers and for example number+1 always give the succesor? (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.

解决方案

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.

这篇关于JavaScript大整数轮因为精度? (为什么?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆