Question Details

No question body available.

Tags

c floating-point bit-manipulation

Answers (2)

February 17, 2026 Score: 6 Rep: 234,966 Quality: High Completeness: 60%

In floating-point arithmetic, “equal” means two numbers are the same. The elements of floating-point arithmetic include both numbers and things that are not numbers, which are called NaNs.

Infinity is a number; floating-point arithmetic, as defined by the IEEE 754 standard, uses a domain consisting of the real numbers plus +∞ and −∞. So +∞ equals +∞ and −∞ equals −∞. Mathematical operations are defined on the infinities; for example, 3/+∞ is +0.

NaN is not a number. So a NaN and another NaN are not equal, because they are not two numbers that are the same. Even if they are identical in their binary representation, they are not numbers that are the same. Mathematical operations are not defined on NaNs.

Generally speaking, a NaN represents something that is outside the normal operation of floating-point arithmetic. It could be a variable that was never initialized to a number or the result of an operation for which no number in the floating-point domain is the result. For example, taking the square root of a negative number produces a NaN.

Consider that we might get the same NaN in different ways. One might come from taking the square root of −16. Another might come from subtracting +∞ from +∞. It would not be correct to assert these two NaNs represent the same number—there is no reason to think the results of these two operations are “equal.”

A program that could get NaN results should be designed to accommodate that. It should either test its operands before operating, test whether a result is a NaN, or enable exceptions to catch invalid operations. (Languages may provide some form of isNaN test, or you can test whether x is a NaN by testing whether x == x is false.) It should not rely on numerical properties of NaNs.

There are times when comparisons with NaNs are needed. For these, we want non-numerical comparisons that give a total ordering on all the elements of floating-point arithmetic. For example, when sorting floating-point data that may contain NaNs, we want a relational operator (like “less than”) that will work on NaNs. Some programming languages provide such operators, so they have separate comparisons numerical operations versus non-numerical operations.

Does C check whether any of the two number is NaN before doing any comparison every time?

C is not one thing. There are many C implementations. There are even multiple C dialects or standards. The ISO C standard does not specify how these things are implemented. Most C implementations use processor instructions that compare floating-point numbers using IEEE-754 semantics, so the treatment of NaNs as not equal is built into the hardware.

February 17, 2026 Score: 1 Rep: 949 Quality: Low Completeness: 80%

Per IEEE 754 standard (which most C implementations follows, but isn't strictly required by the C standard unless STDCIEC60559_BFP is defined):

  • Infinity is a number in the extended real number system. Two infinities with the same sign represent the same mathematical value +∞ == +∞ is true

  • NaN means "Not a Number" : It's a signal that something went wrong(uninitialised variable, sqrt(-1) , ∞ - ∞ , etc). Since NaN doesn't represent any specific value, NaN == NaN is always false by definition, even if their bit patterns are identical. Different operations producing the same NaN doesn't mean they represent the same result

Why the design?

If NaN == NaN were true, code likle this would silently fail:

double x = sqrt(-1);   // NaN
if (x == x){
  // This should NOT execute for invalid data
}

The rule NaN != NaN forces you to explicitly handle errors using isnan() or enable FP exception.

Implementation: Most C compilers using CPU floating-point comparison instructions that follow IEEE 754 semantics directly in hardware, no extra check needed, it's built into the == instruction itself

For bitwise equality, use memcmp() instead of ==