Examine Division By Zero Behavior Across Programming Languages
Back to Blog

Examine Division By Zero Behavior Across Programming Languages

Dividing by zero behaves differently depending on the language and whether the numbers are integers or floating-point. In some cases, the program throws an error; in others, it produces special values like +Infinity, -Infinity, or NaN.

Here’s a snapshot of how several popular languages handle it:

Language float / 0 int / 0 float 0 / 0 int 0 / 0
PHP 7.x +INF / -INF +INF / -INF NaN NaN
PHP 8+ ❌ DivisionByZeroError ❌ DivisionByZeroError ❌ DivisionByZeroError ❌ DivisionByZeroError
Python +inf / -inf ❌ ZeroDivisionError NaN ❌ ZeroDivisionError
JavaScript / TypeScript Infinity /
-Infinity
Infinity /
-Infinity
NaN NaN
C# +Infinity /
-Infinity
❌ Exception NaN ❌ Exception
Java Infinity /
-Infinity
❌ Exception NaN ❌ Exception
Go +Inf / -Inf ❌ Panic NaN ❌ Panic
Rust inf / -inf ❌ Panic NaN ❌ Panic

Integer vs Float: the Big Split

Looking at this, a clear pattern emerges:

  • Integers: most languages treat division by zero as an error. There isn’t a defined "integer infinity," so the operation fails.
  • Floats: many languages follow IEEE 754, which defines 1.0 / 0.0 → +Infinity, -1.0 / 0.0 → -Infinity, and 0.0 / 0.0 → NaN. This allows calculations to continue and produces values that can be reasoned about mathematically.

Why IEEE 754 recommends this

IEEE 754 defines floating-point behavior to make calculations more consistent:

  1. Continuity of calculations: As a denominator approaches zero, the result grows very large. Using Infinity reflects this behavior instead of stopping the program.
  2. Propagation of undefined values: 0/0 is indeterminate. Representing it as NaN allows it to propagate in later calculations, which can be detected or handled without crashing.
  3. Mathematical consistency: Infinity and NaN correspond to limits and indeterminate forms in calculus, making numeric reasoning more predictable.

How Infinity and NaN Are Represented

Floats are stored as bit patterns in memory. IEEE 754 defines three main components:

  • Sign bit → indicates positive or negative (+Inf vs -Inf)
  • Exponent → scales the number
  • Mantissa (fraction) → precise digits of the number

Special cases:

  • Infinity: exponent all 1s, mantissa all 0s. The sign bit sets +∞ or -∞.
  • NaN: exponent all 1s, mantissa non-zero.

For example, a 32-bit float for +Infinity can be broken down like this:

Sign | Exponent    | Mantissa
 0   | 11111111    | 00000000000000000000000
  • Sign bit = 0 → positive
  • Exponent = 11111111 → all 1s
  • Mantissa = 00000000000000000000000 → all 0s

A simple NaN example:

Sign | Exponent    | Mantissa
 0   | 11111111    | 10000000000000000000000
  • Sign bit = 0 → positive (can vary)
  • Exponent = 11111111 → all 1s
  • Mantissa = 10000000000000000000000 → non-zero → signals NaN

These bit patterns explain why floats can "handle" division by zero: the hardware produces a recognizable pattern instead of generating an exception. Integers don’t have such encodings, so division by zero triggers an error.