Great work!

XP to next level

BugEater

Infinity and NaN in Programming

Learning Objectives

By the end of this lesson you will be able to:

  • Explain the IEEE 754 definitions of Infinity and NaN
  • Describe how these special values propagate through arithmetic
  • Identify when Infinity or NaN has leaked into application output

What Are Infinity and NaN?

IEEE 754 defines three special floating-point values:

Positive Infinity (+Inf): Result of 1.0 / 0.0, or when a number exceeds the maximum Double value Negative Infinity (-Inf): Result of -1.0 / 0.0 NaN (Not a Number): Result of undefined operations like 0.0 / 0.0, sqrt(-1), Infinity - Infinity

These are not errors — they are valid values in the IEEE 754 type system. The computation completes. The program continues. The value is just mathematically meaningless.

How NaN Propagates

NaN has a unique property: any arithmetic operation involving NaN returns NaN.

double x = Double.NaN;
double y = x + 5;     // NaN
double z = y * 100;   // NaN
boolean b = x == x;   // false! NaN != NaN

This "poison" behavior means a single NaN introduced early in a calculation chain will propagate to every subsequent calculation — silently, with no exceptions.

How Infinity Propagates

Infinity has similar propagation properties:

double inf = Double.POSITIVE_INFINITY;
double a = inf + 5;       // Infinity
double b = inf - 1000;    // Infinity
double c = inf * -1;      // -Infinity
double d = inf - inf;     // NaN (Infinity - Infinity is undefined!)

Recognizing Leaked Infinity/NaN in Applications

When Infinity or NaN leaks into an application's output:

Symptom Possible Cause
Field shows Infinity or Inf Float overflow or division by zero
Field shows NaN Undefined arithmetic (0/0, sqrt of negative)
Field shows null or empty NaN/Infinity was converted to null during serialization
Field shows -9223372036854775808 (Int64 min) NaN was cast to long

The last two are particularly insidious — the NaN disappears, replaced by something that looks like a valid value.

Testing for Infinity and NaN

For any numeric calculation:

  1. Input that produces division by zero (→ Infinity)
  2. Input that produces 0/0 (→ NaN)
  3. Input that produces sqrt of a negative number (if applicable) (→ NaN)
  4. Input combination that could produce Infinity - Infinity (→ NaN)

Verify that the output field shows a user-friendly error, not Infinity, NaN, null, or a garbage integer.

Pro Tip: In Java, check for NaN with Double.isNaN(value) — not with value == Double.NaN (which is always false because NaN != NaN). A backend that uses == to check for NaN will never detect it.

Key Takeaways

  • Infinity = 1/0; NaN = 0/0 or Infinity - Infinity (IEEE 754)
  • Both propagate silently through arithmetic without throwing exceptions
  • NaN == NaN is false in IEEE 754 — special detection is required
  • Applications must check for and handle these values before displaying results

Quiz

In JavaScript, what is the result of 0 / 0?

Which JavaScript comparison correctly detects a NaN value?

A calculation chain: total = (price - discount) / quantity. If quantity = 0, the result is Infinity. This Infinity is then passed to tax = total * 0.2. What is tax?

A system stores the result of a failed calculation as NaN in the database. What is the MOST likely consequence?