0

I had the problem that uninitialized memory(NaN actually) was used to multiply 0.f then accumulate another value. Finally, I got the NaN. Then I found that

NaN * 0.f = NaN.

in C++. Is this a necessary result that is not related to the compiler?

冯剑龙
  • 569
  • 8
  • 22
  • uninitialized memory is not always nan, operations on nan result in nan – Iłya Bursov Apr 18 '19 at 03:34
  • 1
    Any floating point operation involving a NaN will produce a NaN. That's not a bug. It's what NaNs are supposed to do. – Jerry Coffin Apr 18 '19 at 03:44
  • 2
    Reading the value of an uninitialized variable is undefined behavior. So yes, if that's what you're doing, that's definitely a bug in your code, not in the compiler…apart from that, NaN * 0 is supposed to result in NaN, that's the expected behavior of IEEE 754 floating point, which is almost certainly what floating point numbers are based on on the platform you're targeting… – Michael Kenzel Apr 18 '19 at 03:52

0 Answers0