Mai*_*tor 12 javascript ieee-754
In JavaScript, the NaN value can be represented by a wide range of 64-bit doubles internally. Specifically, any double with the following bitwise representation:
x111 1111 1111 xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
Run Code Online (Sandbox Code Playgroud)
Is interpreted as a NaN. My question is: suppose I cast two 32-bit uints to a JS Number using ArrayBuffers, pass it around, then cast it back to two 32-bit uints. Will the recovered bits be the same as the original, or are JS engines allowed to change the bits of a NaN at will? In other words, can JS numbers be used to store 64-bits losslesly?
ECMA-262 第 9 版,2018年6月(JavaScript旨在遵循的标准)在6.1.6“数字类型”中指出:
… 在ECMAScript中,IEEE标准的9007199254740990(即2 53 -2)不同的“非数字”值表示为单个特殊的NaN值。…在某些实现中,外部代码可能能够检测到差异在各种非数字值之间,但是这种行为取决于实现;对于ECMAScript代码,所有NaN值彼此之间是无法区分的。
24.1.17“ NumberToRawBytes(type,value,isLittleEndian)”说:
…如果值为NaN,则可以将rawBytes设置为选择的IEEE 754-2008 binary64格式非数字编码的任何实现。一个实现必须始终为每个实现可区分的NaN值选择相同的编码。…
我看不到有其他提及NaN的文章正在阐明这个问题。一方面,24.1.17有效地告诉我们将NaN转换为原始字节时必须保留NaN的位。但是,似乎没有什么可以告诉我们在其他操作中必须保留这些位。可能会推断出这是目的,因为如果可以通过任何其他操作任意更改这些位,则24.1.17中的这一要求将毫无用处。但是我不会依靠JavaScript实现来实现此目的。
| 归档时间: |
|
| 查看次数: |
252 次 |
| 最近记录: |