What is NaN in C programming

How do you use nan and inf in C?

I have a numeric method that can return nan or inf if an error has occurred, and for testing purposes I want to temporarily force it to return nan or inf to make sure the situation is handled correctly. Is there a reliable compiler-independent Ability to create values ​​for nan and inf in C?

After Googling for about 10 minutes, I was only able to find compiler dependent solutions.


You can test if your implementation has it:

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

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

When comparing floating point values, keep the following in mind and do the following:

even then

is wrong. One way to look for NaN would be:

You can also do the following: To test for NaN.

There are also,,, and macros in in C99.

C99 also has functions:

(Reference: n1256).


There is no compiler-independent method because neither the C (nor the C ++) standards state that the floating point math types must support NAN or INF.

To edit: I just checked the wording of the C ++ standard and it says these functions (members of the numeric_limits template class):

We will return NAN representations "if available". It is not extended, which means "if available", but presumably "if the FP representative of the implementation supports it". There is also a function:

This returns a positive INF representative "if available".

These are both defined in the header - I would guess the C standard has something similar (probably also "if available"), but I don't have a copy of the current C99 standard.

This works for both and:

Edit: As someone said, the old IEEE standard said such values ​​should throw traps. However, the new compilers almost always turn off the traps and return the specified values ​​because the trapping interferes with error handling.

A compiler independent way, but not a processor independent way to get this:

This should work on any processor using the IEEE 754 floating point format (which x86 does).

UPDATE: Tested and updated.


I'm also surprised that these aren't compile-time constants. But I suppose you could create these values ​​easily enough simply by executing a statement that returns such an invalid result. Divide by 0, log of 0, tan of 90, something like that.

I usually use


This works at least in IEEE 754 contexts, since the highest double value that can be represented is approximate. would work as well as would, but three nines are enough and memorable. Since this is either a double literal (in the case) or an actual value, it remains infinite even if you use 128-bit ("long double") floats.

Here's an easy way to define these constants, and I'm pretty sure they're portable:

When I run this code:

I get:

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.