Can an Integer Be Nan in C++

Can an integer be NaN in C++?

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.

How to use nan and inf in C?

You can test if your implementation has it:

#include <math.h>
#ifdef NAN
/* NAN is supported */
#endif
#ifdef INFINITY
/* INFINITY is supported */
#endif

The existence of INFINITY is guaranteed by C99 (or the latest draft at least), and "expands to a constant expression of type float representing positive or unsigned
infinity, if available; else to a positive constant of type float that overflows at translation time."

NAN may or may not be defined, and "is defined if and only if the implementation supports quiet NaNs for the float type. It expands to a constant expression of type float representing a quiet NaN."

Note that if you're comparing floating point values, and do:

a = NAN;

even then,

a == NAN;

is false. One way to check for NaN would be:

#include <math.h>
if (isnan(a)) { ... }

You can also do: a != a to test if a is NaN.

There is also isfinite(), isinf(), isnormal(), and signbit() macros in math.h in C99.

C99 also has nan functions:

#include <math.h>
double nan(const char *tagp);
float nanf(const char *tagp);
long double nanl(const char *tagp);

(Reference: n1256).

Docs INFINITY
Docs NAN

Problems casting NAN floats to int

The result of a cast of a floating point number to an integer is undefined/unspecified for values not in the range of the integer variable (±1 for truncation).

Clause 6.3.1.4:

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.

If the implementation defines __STDC_IEC_559__, then for conversions from a floating-point type to an integer type other than _BOOL:

if the floating value is infinite or NaN or if the integral part of the floating value exceeds the range of the integer type, then the "invalid" floating-
point exception is raised and the resulting value is unspecified.

(Annex F [normative], point 4.)

If the implementation doesn't define __STDC_IEC_559__, then all bets are off.

What is the result of casting float +INF, -INF, and NAN to integer in C?

As Paul said, it's undefined:

From §6.3.1.4:

6.3.1.4 Real floating and integer

When a finite value of real floating type is
converted to an integer type other
than _Bool, the fractional part is
discarded (i.e., the value is
truncated toward zero). If the value
of the integral part cannot be
represented by the integer type, the
behavior is undefined.50)

Infinity isn't finite, and the integral part can't be represented in an integral type, so it's undefined.

How to produce a NaN float in c?

Using floating point numbers, 0.0 / 0.0 isn't a "divide by zero" error; it results in NaN.

This C program prints -nan:

#include <stdio.h>

int main()
{
float x = 0.0 / 0.0;
printf("%f\n", x);
return 0;
}

In terms what NaN looks like to the computer, two "invalid" numbers are reserved for "signaling" and "quiet" NaN (similar to the two invalid numbers reserved for positive and negative infinity). The Wikipedia entry has more details about how NaN is represented as an IEE floating point number.

Objective-C: int can´t be NAN?

NaN is a value that is specific to the way that floating point numbers (float and double in C) are represented internally (IEEE 754). Is is not available for integer data types, as they may be represented in a completely different manner.

As a workaround, you may see people use a separate boolean flag to indicate if a value is valid, or the extreme values INT_MIN/INT_MAX. Although, nether of these is as good as NaN for ensuring valid values.



Related Topics



Leave a reply



Submit