What uses do floating point NaN payloads have?
Asked Answered
W

3

18

I know that IEEE 754 defines NaNs to have the following bitwise representation:

  • The sign bit can be either 0 or 1
  • 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?

Wasteful answered 28/11, 2015 at 4:47 Comment(3)
I've heard of the payload being used to answer why a NaN was generated in the first place. (0/0, oo-oo, function evaluated at pole, etc.) I have also heard unconfirmed rumours that at least one program uses the (51-bit) sNaN payload as a (48-bit) pointer for a "fallback" of sorts to arbitrary-precision arithmetic.Recommend
the spec also defines when a nan is created, signaling nans vs quiet nans. The idea is so you dont have to check after every operation if there was an overflow or underflow or divide by zero or whatever you can look at the final result and see that the calculation had a problem at some point.Jeanettajeanette
yeah, just checked, it lists several situations where the correct result is a NaN. (no not necessarily those that I listed in the above comment)Jeanettajeanette
P
9

It was thought to be a good idea when IEEE754 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.

Pied answered 28/11, 2015 at 13:28 Comment(2)
It should also be noted that IEEE-754 (2008) does not mandate NaN payloads, it merely recommends them. Use of a single canonical NaN pattern (in both an SNaN and a QNaN flavor) would be standard compliant. See section 6.2 of the standard, in particular (emphasis mine): "To facilitate propagation of diagnostic information contained in NaNs, as much of that information as possible should be preserved in NaN results of operations."Molybdate
As a concrete example, the default on the Apple M1 chip is not to propagate payloads (blog.r-project.org/2020/11/02/will-r-work-on-apple-silicon/…)Conall
C
7

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

Cutcliffe answered 28/11, 2015 at 12:48 Comment(1)
That's pretty fascinating! Could you kindly summarize the information from the link in your answer? Once you do I'll be ready to upvote it.Wasteful
C
2

The statistical environment R uses NaN payloads to distinguish one specific NaN as representing a statistical "missing value", which prints as NA. This allows 'missing' to propagate through numeric calculations -- although when combining a missing value with another NaN it is not predictable which one is propagated.

Conall answered 26/8, 2020 at 0:27 Comment(3)
Described in more detail in the answer to Difference between NA_real_ and NaN in R. Thanks for leading me there! Essentially they put the number 1954 in the payload to indicate "missing" / "not available". We don't know why, though.Coenocyte
We do know why. It's because Ross Ihaka was born in 1954.Conall
Thanks for pointing out. Here is a source for it: "In doubles, an NA is NaN with a special bit pattern (the lowest word is 1954, the year Ross Ihaka was born)" And another mention: "missing values in floating point vectors using a specific NaN payload (1954, which rumour says refers to Ross Ihaka's year of birth)"Coenocyte

© 2022 - 2024 — McMap. All rights reserved.