Folks
When can a float32 variable show nan
value in a 64 bit system? Can this be caused by overflow?

- 239
- 5
- 10
-
possible duplicate of [How to produce a NaN float in c?](http://stackoverflow.com/questions/7212356/how-to-produce-a-nan-float-in-c) – Isaac Betesh Jan 07 '15 at 20:37
-
1What does the 64bit system have to do with it though? Did the same program not show `nan` on a 32bit system? – harold Jan 07 '15 at 20:38
-
@harold Yes, its only showing up on 64 bit system hence I asked. Else I know how to produce a NaN. – srsci Jan 07 '15 at 20:44
-
Show the code that produces NAN on 64-bit systems and not NAN else-wise. Else we we only be able to speculate as the cause of your results. – chux - Reinstate Monica Jan 07 '15 at 20:45
3 Answers
float f = 0.0f/0.0f; printf("%f", f);
shows the NAN
.
It has nothing to do with 64 bit system, 16 bit system, N bit system or Turing machine.
Overflow would cause infinity.

- 143,097
- 13
- 135
- 256
NaN
- Not a Number - is used to encode non-numeric result of a computation, such as division by 0.
http://en.wikipedia.org/wiki/NaN#Operations_generating_NaN
Your math code probably does something strange, such as multiplying by infinity or doing square root of negative number. This kind of problems must be carefully examined on case-by-case basis. It may be simply an uninitialized variable, accumulation of rounding errors, bad mathematical equation, etc, etc.
Platform bits doesn't matter, as floating point arithmetic is strictly stadarized and should behave the same way everywhere[1].
[1] Exceptions apply, but exotic platforms are not a scope of this question.

- 1,914
- 13
- 15
NO, overflow would cause and infinity value +INF
ot -INF
, NAN
doesn't come that way.
probably your code has a bug.
sqrt(-1) or 0/0

- 11,837
- 2
- 30
- 48