问题描述
如果你这样做
for(var i = 0; i < 30; i++){console.log(i + " == " + 78764357878563800 + " ? ");console.log((78764357878563790+i) == 78764357878563800);}
您开始比较从 78764357878563790 到 78764357878563790+29 的值,所以...就像您运行它时看到的那样,您会从 i = 8 到 24 得到真值.
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.
所以我不知道我是否不明白,但不应该存在 maxint(我不知道)和 maxint-1 ???然后 max 与 maxint-1 不同?
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?
我想这就像浮点数一样精度......但这不应该只命中浮点数,例如,数字+1总是给出后继?(所以在上面的例子中,如果 i = 78764357878563790 并加上 i++,那么你就进入了无限循环.
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+1 和 i-1 的情况(我一直认为 unsigned maxint + 1 会携带并给出0).
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).
任何建议:
- js 中的最大数和前数是多少.
- 如何处理或了解整数的这种行为何时开始发生.
- 如何处理如此大的数字以及比这更大的数字会很好.
推荐答案
Javascript 中没有整数.
There are no integers in Javascript.
数字是双精度浮点数,精度为 15-16 位.这与您的结果一致.
Numbers are double precision floating point, which gives you a precision of 15-16 digits. This is consistent with your results.
这篇关于javascript 大整数舍入,因为精度?(为什么?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!