2

I always thought there were 2 kinds of NaN's: quiet and signaling.

But then I realized the NAN macro evaluates to neither.

In Visual C++, std::numeric_limits<float>::quiet_NaN() is displayed as 1.#QNAN000.
In Visual C++, std::numeric_limits<float>::signaling_NaN() is displayed as 1.#QNAN000.
Yet also in Visual C++, NAN is displayed as -1.#IND0000... which is neither of the above.

  1. What kind of NaN, then, is the NAN constant? Is it signaling or nonsignaling?

  2. When should I use NAN instead of the others, when should I avoid it, and why?

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 1
    There are more than two NaN values. In particular, there are millions of signaling NaNs and millions of quiet NaNs. [They print in different ways](http://blogs.msdn.com/b/oldnewthing/archive/2013/02/21/10395734.aspx). – Raymond Chen May 26 '14 at 03:20
  • @RaymondChen: I think you missed the phrase *"**kinds** of NaN"* in my question which I repeated 3 times. But thanks for the link, I'll take a look at your blog to see if it's talking about "kinds" of NaNs. – user541686 May 26 '14 at 03:26
  • `quiet_NaN()` returns *a* quiet NaN, but there are others. The `NAN` macro returns a different quiet NaN. Saying that NaN is neither `quiet_NaN()` nor `signaling_NaN()` doesn't mean that it's not either. It's just a quiet NaN that is a different quiet NaN from the one returned by `quiet_NaN`. If you wanted to know whether NAN is signaling or not, [the documentation says it is quiet](http://www.cplusplus.com/reference/cmath/NAN/). – Raymond Chen May 26 '14 at 14:27

1 Answers1

2

You machine does not have a signaling NaN. I presume the Q in #QNAN stands for Quiet. Check numeric_limits::has_signaling_NaN. As for signaling_NaN, the standard says this:

Meaningful for all specializations for which has_signaling_NaN != false. Required in specializations for which is_iec559 != false.

Therefore, it may be required yet meaningless… the natural thing to do is supply a quiet NaN instead.

I haven't found an authoritative resource for QNAN vs IND, but they appear to both be quiet NaNs. There are potentially millions of different NaNs, one for each mantissa value. If I were to guess, IND may be like an error code for an ind‍eterminate value, e.g. 0/0. Not really an appropriate choice for a generic NAN macro, but they might have been stuck with it after defining the C numeric binding that way.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • So they're the same type of NAN, just with a different string representation? +1 thanks – user541686 May 26 '14 at 03:24
  • 1
    @Mehrdad Since there are only two "types", signaling and non-signaling, and your results seem to indicate signaling doesn't exist on your platform, I guess you could say they're the same type. According to [Eric Postpischil](http://stackoverflow.com/a/10400536/153285), you hit the nail on the head: they differ only in string representation. – Potatoswatter May 26 '14 at 03:32