tags:

views:

159

answers:

4

Hi,

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

+8  A: 

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.

DerKuchen
"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
@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
@Johannes: I'm sure DerKuchen means to refer to bitpatterns, not values of the object type.
Alf P. Steinbach
`numeric_limits<int>::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
A: 

No, you cannot set an int to NaN.

codaddict
+2  A: 

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.

Mark Rushakoff
A: 

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.

Manoj R