In JavaScript, how is integer represented in storage layout?

淺唱寂寞╮ 提交于 2019-12-22 12:56:15

问题


JavaScript comes with 64bit float numbers for all numeric literals, and the storage layout and range is regulated under ITEE 754.

On the other hand, I learned that the float has a range of ± ~10^-323.3 to ~10^308.3 and an as-reliable-as-possible precision. Integer has a range of -2^53 - 2^53 and a reliable precision.

ITEE 754 explains the behavior of float numbers but I get confused about the integer in JS. How is the range of precision generated from the 64bit data format?

[Solved] The value is stored in the fraction position. 1 is (1+0)*2^0, 2 is (1 + 0) * 2^1, 3 is (1 + 2^-1)*2^1... Any number between -2^53 and 2^53 could be expressed precisely. As there is a leading 1 for all fraction, the range is -2^53 to 2^53


回答1:


Actually it is -2^52 to 2^52. What happens is that integer is actually stored in floating point format. At yout URL you can see that from 64 bits 52 bits are used for fraction. That is where number is actually stored.




回答2:


As reliable as possible means precisely the 2^52 range. While you can represent numbers larger than that you start skiping some integers after this point.




回答3:


[Solved myself] The value is stored in the fraction position. 1 is (1+0)*2^0, 2 is (1 + 0) * 2^1, 3 is (1 + 2^-1)*2^1... Any number between -2^53 and 2^53 could be expressed precisely. As there is a leading 1 for all fraction, the range is -2^53 to 2^53



来源:https://stackoverflow.com/questions/7675222/in-javascript-how-is-integer-represented-in-storage-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!