I know that IEEE 754 defines NaNs to have the following bitwise representation:
- The sign bit can be either
0
or1
- The exponent field contains all
1
bits - Some bits of the mantissa are used to specify whether it's a quiet NaN or signalling NaN
- The mantissa cannot be all
0
bits because that bit pattern is reserved for representing infinity - The remaining bits of the mantissa form a payload
The payload is propagated (as is the NaN as a whole) to the result of a floating point calculation when the input of the calculation is NaN, though I have no knowledge of the details of this propagation or whether the standard specifies how this is done. Who sets the original payload? What happens if I add two NaNs with different payloads?
But most importantly: I've never seen NaN payloads used before. What uses does this payload field have?
It was thought to be a good idea whenIEEE754 and NaN's were developed. I have actually seen it used to store the reason why a NaN was created.
Today, I wouldn't use it in portable code for several reasons. How sure are you that this payload will survive for example an assignment? If you assign x = y, how sure are you that x has the same NaN payload as y? And how sure are you that it will survive arithmetic? If a or b is an NaN, then a op b is supposed to be the one NaN, or one of the two NaNs if they are both NaN. Sure that this is the case? I wouldn't be willing to bet on it.
https://softwareengineering.stackexchange.com/questions/185406/what-is-the-purpose-of-nan-boxing
Take a look at that link for an explanation of how js engines use nan boxing
来源:https://stackoverflow.com/questions/33967804/what-uses-do-floating-point-nan-payloads-have