Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an integer be NaN in C++?

Tags:

c++

nan

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

like image 395
MBZ Avatar asked Oct 16 '10 15:10

MBZ


People also ask

Does NaN exist in C?

NaN is unordered: it is not equal to, greater than, or less than anything, including itself. x == x is false if the value of x is NaN. You can use this to test whether a value is NaN or not, but the recommended way to test for NaN is with the isnan function (see Floating-Point Number Classification Functions).

What is NaN value in C?

The NaN values are used to identify undefined or non-representable values for floating-point elements, such as the square root of negative numbers or the result of 0/0. In C, this is implemented as a macro that returns an int value. The type of x shall be float , double or long double .

What produces NaN in C?

nan is produced when we program contain value like 0.0/0.0 as said by @Dan Cecile OR sqrt(-1).

Can a float be NaN?

NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis.


2 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.

like image 131
DerKuchen Avatar answered Oct 03 '22 00:10

DerKuchen


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.

like image 36
Mark Rushakoff Avatar answered Oct 02 '22 23:10

Mark Rushakoff