NaN, Infinity and Negative Zero Explained
Learn what IEEE 754 NaN, infinity and negative zero are, how they arise, why NaN is not equal to itself, and how to test for each safely in C.

Floating-point arithmetic never throws an exception by default. Divide one by zero and you do not crash: you get infinity. Take the square root of minus one and you get a value called NaN, not a number, that is not equal to anything, including itself. And there are two zeros, positive and negative, that compare equal but behave differently in a few operations. These special values are part of IEEE 754 by design, they let long calculations run to the end, and they surprise nearly everyone the first time. This post shows how each arises, how it is encoded, the rules for comparing and testing them, and the compiler flag that silently breaks all of it. The examples are C17 on GCC 14 with glibc 2.40 on x86-64, where double follows IEEE 754 binary64.
How the special values are encoded
The earlier post on the layout of floats described the exponent field. Two of its values are reserved. A stored exponent of all zeros marks zero and the subnormal numbers, and a stored exponent of all ones marks infinity and NaN. For a double, with its 11-bit exponent, those are the exponents 0x000 and 0x7FF.
| Value | Sign | Exponent | Fraction | Bits as hex |
|---|---|---|---|---|
| positive zero | 0 | all zeros | all zeros | 0x0000000000000000 |
| negative zero | 1 | all zeros | all zeros | 0x8000000000000000 |
| positive infinity | 0 | all ones | all zeros | 0x7FF0000000000000 |
| negative infinity | 1 | all ones | all zeros | 0xFFF0000000000000 |
| quiet NaN | 0 or 1 | all ones | top fraction bit set | 0x7FF8000000000000 and similar |
Infinity is the all-ones exponent with a zero fraction. If the fraction is anything other than zero, the value is a NaN. That leaves a huge number of NaN bit patterns, and the extra bits can carry a payload, a small number that some systems use to record where the NaN came from. A quiet NaN has the top fraction bit set and propagates through arithmetic silently. A signalling NaN has it clear, and is meant to raise an invalid-operation exception when used, though it is rarely produced in ordinary code.
Infinity
Infinity is the result of an operation whose true result is too large to represent, or a division of a non-zero number by zero. Rounding an overflowing sum or product gives infinity, not a wrapped value. Dividing by zero with the default settings gives a signed infinity, and the sign follows the usual rules: a positive number over positive zero is positive infinity, and over negative zero is negative infinity.
Infinity behaves sensibly in arithmetic. Adding a finite number to infinity gives infinity. Comparing with infinity works as you would hope: every finite number is less than positive infinity. Infinity minus infinity, zero times infinity and infinity divided by infinity are undefined and produce NaN.
NaN
A NaN is the result of an operation with no meaningful answer, such as zero divided by zero, infinity minus infinity, the square root of a negative number or the logarithm of a negative number. In the default environment these operations raise the invalid-operation flag in the floating-point status register, set the result to a quiet NaN, and carry on.
NaN has one property that defines its behaviour: it is unordered with respect to every value, including itself. Every ordered comparison with a NaN is false: NaN < 1, NaN > 1 and NaN == NaN are all false. The only comparison that is true is !=. This is how you can test for NaN with no library function, using x != x, and also how a naive equality check fails.
Arithmetic propagates NaN: any operation with a NaN operand gives a NaN. That is deliberate. A long computation can run to the end, and the NaN in the result tells you something went wrong, where an exception would have lost the context. It is also why a single bad input value can turn a whole array of results into NaN.
Negative zero
IEEE 754 has two zeros because the sign bit is separate from the magnitude. Negative zero arises from the negation of positive zero, from multiplying a negative number by zero, and from the underflow of a small negative result. It compares equal to positive zero, so -0.0 == 0.0 is true, and in most arithmetic the two are interchangeable. They differ where the sign matters: dividing by negative zero gives negative infinity, and atan2, copysign and a few other functions return different results. Printing negative zero with %f shows -0.000000.
Negative zero is not an oddity to avoid. It preserves information about the sign of a value that underflowed, which some functions rely on, particularly around branch cuts of complex functions.
Seeing all of it
The program below creates each value, tests it with the standard classification macros from math.h, and prints the bit patterns. The divisor zero is marked volatile so the compiler does not fold the divisions at compile time and diagnose them.
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static uint64_t bits(double d)
{
uint64_t u;
memcpy(&u, &d, sizeof u);
return u;
}
int main(void)
{
volatile double zero = 0.0;
double pinf = 1.0 / zero;
double ninf = -1.0 / zero;
double nz = -0.0;
double nan = NAN;
printf("1/0 = %g, -1/0 = %g\n", pinf, ninf);
printf("bits: +inf 0x%016" PRIX64 ", -inf 0x%016" PRIX64 "\n", bits(pinf), bits(ninf));
printf("bits: -0.0 0x%016" PRIX64 ", NAN 0x%016" PRIX64 "\n", bits(nz), bits(nan));
printf("-0.0 == 0.0: %d, signbit(-0.0): %d\n", nz == 0.0, signbit(nz) != 0);
printf("1 / -0.0 = %g\n", 1.0 / nz);
printf("NaN == NaN: %d, NaN != NaN: %d\n", nan == nan, nan != nan);
printf("NaN < 1: %d, NaN > 1: %d\n", nan < 1.0, nan > 1.0);
printf("isnan(NaN): %d, isinf(+inf): %d, isfinite(+inf): %d\n",
isnan(nan) != 0, isinf(pinf) != 0, isfinite(pinf) != 0);
return 0;
}The macro NAN from math.h expands to a quiet NaN of type float, and the conversion to double keeps it quiet. The signbit macro returns a nonzero value for a set sign bit, with no promise about which nonzero value, so the program compares it to zero. The classification macros return nonzero for true, and the comparisons with zero turn them into 0 or 1 for printing.
1/0 = inf, -1/0 = -inf
bits: +inf 0x7FF0000000000000, -inf 0xFFF0000000000000
bits: -0.0 0x8000000000000000, NAN 0x7FF8000000000000
-0.0 == 0.0: 1, signbit(-0.0): 1
1 / -0.0 = -inf
NaN == NaN: 0, NaN != NaN: 1
NaN < 1: 0, NaN > 1: 0
isnan(NaN): 1, isinf(+inf): 1, isfinite(+inf): 0The last line shows that isfinite is false for infinity: the macro is true only for a value that is neither infinite nor NaN. The printed bit patterns confirm the table above, and show that NAN has the quiet bit set and an all-zero payload.
Testing for special values
Use the macros from math.h, not equality, to test for them.
| Test | Macro | True for |
|---|---|---|
| is it NaN | isnan(x) | any NaN |
| is it infinite | isinf(x) | positive or negative infinity |
| is it a normal number or zero | isfinite(x) | everything except infinity and NaN |
| is the sign bit set | signbit(x) | negative numbers, negative zero, and NaN with the sign set |
| classify | fpclassify(x) | one of FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL |
To get a value with a particular sign, use copysign(1.0, x), which returns plus or minus 1 according to the sign bit of x, even for zero and NaN. To tell the two zeros apart, test signbit, never x < 0.0, which is false for negative zero.
NaN in sorting and containers
NaN breaks comparison-based algorithms, because it is not less than, equal to or greater than anything. A sort with the comparison a < b placed NaNs unpredictably, and qsort with a comparator that returns inconsistent results has undefined behaviour. A hash table that uses == to find a key can never find a NaN key. The standard fixes are to filter NaNs out before sorting, or to write a comparator that places them at one end, for instance by testing isnan first. In C++ the same applies to std::sort, and in Rust, f64 implements PartialOrd and not Ord for this reason, with the method total_cmp supplying the IEEE total order that includes NaN.
The fast-math trap
The compiler flag -ffast-math, and its component -ffinite-math-only, tells GCC to assume that no NaN or infinity ever occurs. The compiler may then remove isnan checks as always false, and x != x is folded to false. A program that depended on NaN to signal an error silently stops detecting it, and reports a wrong number instead. If you must use fast-math for speed, do not rely on NaN or infinity, and keep the code that handles special values in a separate file compiled without the flag.
Common mistakes
Testing for NaN with
x == NAN, which is always false. Useisnan(x).Using
x < 0.0to check the sign of a value that could be negative zero.Assuming division by zero in floating point is an error or a crash, when it quietly gives infinity or NaN.
Sorting an array that contains NaN, or using NaN as a hash key.
Enabling
-ffast-mathglobally and losing NaN and infinity handling.


