Can an integer be NaN in C++?
Asked Answered
S

5

42

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

Schutzstaffel answered 16/10, 2010 at 15:16 Comment(0)
K
57

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.

Kp answered 16/10, 2010 at 15:17 Comment(8)
"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.Linkage
@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."Ibanez
@Johannes: I'm sure DerKuchen means to refer to bitpatterns, not values of the object type.Ransdell
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.Rotterdam
@Rotterdam According to link quiet_NaN() will return 0 for int.Nereen
@Nereen That is a wiki; they don't decide anything. For an analysis of what the ISO standard says about integer NaNs, see https://mcmap.net/q/391812/-int-a-a-a-fails .Rotterdam
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)Endothelium
@Rotterdam 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, which only has the notion for floating-point arithmetic and redirects the source to IEC 60559, i.e. IEEE-754.Interpretive
B
6

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.

Blubber answered 16/10, 2010 at 15:18 Comment(1)
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.Interpretive
M
4

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
}
Moneywort answered 22/6, 2019 at 12:57 Comment(0)
A
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.

Amphimixis answered 16/10, 2010 at 15:42 Comment(0)
G
1

No, you cannot set an int to NaN.

Greenlet answered 16/10, 2010 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.