Can I set an int
to NaN
? If yes, then how can I check if an int
is NaN
or not?

- 7,835
- 7
- 61
- 104

- 26,084
- 47
- 114
- 191
5 Answers
No, NaN is a floating point value.
Every possible value of an int
is a number.
Edit
The standard says:
6.2.6.2 40) Some combinations of padding bits might generate trap representations, for example, if one padding bit is a parity bit. Regardless, no arithmetic operation on valid values can generate a trap representation other than as part of an exceptional condition such as an overflow, and this cannot occur with unsigned types.
So there may be some implementation specific invalid integer values, but there is no defined way to generate them.

- 1,840
- 1
- 15
- 15
-
1"Every possible value of an int is a number." Is that part of the standard, or just something that is always true in practice? For instance, in the C99 standard, integer types can have trap representations. Only `unsigned char` is guaranteed not to have any. – Pascal Cuoq Oct 16 '10 at 15:19
-
1@Pascal trap representations are not regarded values. "Certain object representations need not represent a value of the object type. [...] Such a representation is called a trap representation." – Johannes Schaub - litb Oct 16 '10 at 15:46
-
@Johannes: I'm sure DerKuchen means to refer to bitpatterns, not values of the object type. – Cheers and hth. - Alf Oct 16 '10 at 19:07
-
`numeric_limits
::quiet_NaN()`, of course ;v) . — And seriously, NaN is **not** a floating point value, it is the absence of any value, FP or otherwise. It just happens to be cheaper to add to an FP format than a fixed-point format. – Potatoswatter Oct 19 '10 at 20:49 -
6@Potatoswatter According to [link](http://en.cppreference.com/w/cpp/types/numeric_limits/quiet_NaN) `quiet_NaN()` will return `0` for `int`. – dominikschnitzer Jun 13 '13 at 08:14
-
@dominikschnitzer That is a wiki; they don't decide anything. For an analysis of what the ISO standard says about integer NaNs, see http://stackoverflow.com/a/3972350/153285 . – Potatoswatter Jun 15 '13 at 06:14
-
Since C++20, trap representations are not allowed. ("Padding bits have unspecified value, but cannot cause traps. In contrast, see ISO C 6.2.6.2" - [basic.fundamental](http://eel.is/c++draft/basic.fundamental)) – Lack Aug 28 '20 at 03:25
-
@Potatoswatter Nope. Seriously, (quiet) NaN is a value of some floating-point type, so it **is** a floating-point value. On the contrast, in IEEE-754 parlance, it is a floating-point *datum* which is not a floating-point *number*. Note that C++'s quite NaN is defined as qNaN in [LIA-1](http://www.open-std.org/jtc1/sc22/wg11/docs/n519.pdf), which only has the notion for floating-point arithmetic and redirects the source to IEC 60559, i.e. IEEE-754. – FrankHB Nov 28 '21 at 08:38
Generally (and specifically in the case of C++, to the best of my knowledge): no.
Integer NaN
Most fixed sized integer formats do not have any way of explicitly indicating invalid data.

- 1
- 1

- 249,864
- 45
- 407
- 398
-
Well, sometimes it actually *has to*. For example, ISO C's `ilogb` uses `FP_ILOGB0`, `INT_MAX` and `FP_ILOGBNAN` to encode the invalid results (of `int` type) from the mathematically invalid inputs. These are invalid in the sense of the fact that the function may also raise the `FE_INVALID` floating-point exception in the same conditions. – FrankHB Nov 28 '21 at 08:47
I think the most proper API to handle failures is to return a second integer error code in your api like:
int myfunc(args, int* realReturn);
The returned int is an error code.
The previous output is passed as a pointer in calling code:
int myInt;
if (myFunc(args, &myInt) != 0) {
//handle error
}

- 81
- 3
You don't have any specific int value as Nan. What normally people do is use some large integer to represent this value. IF it is unsigned int then its normally use -1.

- 3,197
- 1
- 21
- 36