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
475 views
in Technique[技术] by (71.8m points)

javascript - 一个数字可以在不损失精度的情况下达到的JavaScript的最高整数值是多少?(What is JavaScript's highest integer value that a number can go to without losing precision?)

Is this defined by the language?

(这是由语言定义的吗?)

Is there a defined maximum?

(是否有定义的最大值?)

Is it different in different browsers?

(在不同的浏览器中是否有所不同?)

  ask by TALlama translate from so

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

1 Reply

0 votes
by (71.8m points)

+/- 9007199254740991

(+/- 9007199254740991)

ECMA Section 8.5 - Numbers

(ECMA第8.5节-数字)

Note that all the positive and negative integers whose magnitude is no greater than 2 53 are representable in the Number type (indeed, the integer 0 has two representations, +0 and ?0).

(请注意,所有大小不大于2 53的正整数和负整数都可以在Number类型中表示(实际上,整数0具有+0和-0两种表示形式)。)

They are 64-bit floating point values, the largest exact integral value is 2 53 -1, or 9007199254740991 .

(它们是64位浮点值,最大的精确整数值为2 53 -1或9007199254740991 。)

In ES6, this is defined as Number.MAX_SAFE_INTEGER .

(在ES6中,将其定义为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 2 31 -1, or 2147483647.

(请注意,按位运算符和移位运算符对32位整数进行运算,因此在这种情况下,最大安全整数为2 31 -1或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 

Technical note on the subject of the number 9007199254740992: There is an exact IEEE-754 representation of this value, and you can assign and read this value from a variable, so for very carefully chosen applications in the domain of integers less than or equal to this value, you could treat this as a maximum value.

(关于编号为9007199254740992的主题的技术说明:该值有一个精确的IEEE-754表示形式,您可以从变量中分配和读取该值,因此对于在小于或等于整数的整数域中进行非常仔细选择的应用程序此值,您可以将其视为最大值。)

In the general case, you must treat this IEEE-754 value as inexact, because it is ambiguous whether it is encoding the logical value 9007199254740992 or 9007199254740993.

(在一般情况下,您必须将此IEEE-754值视为不精确,因为它是对逻辑值9007199254740992还是9007199254740993进行编码都是模棱两可的。)


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

...