什么是JavaScript的最大整数值,数字可以去而不失精度? [英] What is JavaScript's highest integer value that a Number can go to without losing precision?
问题描述
这是由语言定义的吗?是否有定义的最大值?在不同的浏览器中它是不同的吗?
+/- 9007199254740991 b
请注意,其大小不大于2 53 的所有正整数和负整数都可以在Number类型中表示,整数0具有两个表示,+ 0和-0)。
它们是64位浮点值,最大的精确整数值是2 53 或 9007199254740991
。在ES6中,这被定义为 Number.MAX_SAFE_INTEGER 。
请注意,按位运算符和移位运算符对32位int运算,因此在这种情况下,最大安全整数为2 31 -1或2147483647.
测试一下!
var x = 9007199254740992;
var y = -x;
x == x + 1; // true!
y == y - 1; //也是真的!
//算术运算符工作,但按位/移位只对int32操作:
x / 2; // 4503599627370496
x>> 1; // 0
x | 1; // 1
Is this defined by the language? Is there a defined maximum? Is it different in different browsers?
+/- 9007199254740991
Note that all the positive and negative integers whose magnitude is no greater than 253 are representable in the Number type (indeed, the integer 0 has two representations, +0 and −0).
They are 64-bit floating point values, the largest exact integral value is 253-1, or 9007199254740991
. In ES6, this is defined as Number.MAX_SAFE_INTEGER.
Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 231-1, or 2147483647.
Test it out!
var x = 9007199254740992;
var y = -x;
x == x + 1; // true !
y == y - 1; // also true !
// Arithmetic operators work, but bitwise/shifts only operate on int32:
x / 2; // 4503599627370496
x >> 1; // 0
x | 1; // 1
这篇关于什么是JavaScript的最大整数值,数字可以去而不失精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!