我正在阅读Eloquent JavaScript,我在这个解释中被“卡住”了,起初我以为我会跳过它,但后来我试图回到它,我无法理解它,我尝试谷歌搜索 64 位反响和类似的东西,但我找不到答案。
我只是没有得到一些东西,例如,10^3?
为什么它限制为三位小数?我试着打字1.99999999999
,它成功了,有什么地方我可以完全理解这句话而不跳过它?我在某处读到 javascript 存在近似问题,2.000000000
可能2.000000000001
会导致问题,但这本书的这一部分似乎与我没有任何相似之处。
任何帮助表示感谢。
这是我从这本书中无法理解的部分
This is what 144 looks like in bits:
0100000001100010000000000000000000000000000000000000000000000000
The number above has 64 bits. Numbers in JavaScript always do. This has one important repercussion: There is a limited amount of different numbers that can be expressed. With three decimal digits, only the numbers 0 to 999 can be written, which is 10^3 = 1000 different numbers. With 64 binary digits, 2^64 different numbers can be written. This is a lot, more than 10^19 (a one with nineteen zeroes).
Not all whole numbers below 10^19 fit in a JavaScript number though. For one, there are also negative numbers, so one of the bits has to be used to store the sign of the number. A bigger issue is that non-whole numbers must also be represented. To do this, 11 bits are used to store the position of the fractional dot within the number.
That leaves 52 bits. Any whole number less than 2^52 (which is more than 10^15) will safely fit in a JavaScript number. In most cases, the numbers we are using stay well below that, so we do not have to concern ourselves with bits at all. Which is good. I have nothing in particular against bits, but you do need a terrible lot of them to get anything done. When at all possible, it is more pleasant to deal with bigger things.