logo

The Difference Between Inf and NaN When Dividing by Zero in Julia 📂Julia

The Difference Between Inf and NaN When Dividing by Zero in Julia

Overview

Explains how 0/0 and 1/0 are different in Julia.

Code

julia> 0/0
NaN

julia> 1/0
Inf

At least in programming, infinity Inf can be quite useful in terms of initialization or comparison of sizes, making division by 0 not so strange after all. The problem is ${{ 0 } \over { 0 }} \ne \infty$, and in Julia, it is treated as NaN. In other words, while dividing by 0 might not pose a problem, there’s a risk of unexpected behavior if the numerator is 0, contrary to the anticipated Inf.

Environment

  • OS: Windows
  • julia: v1.8.3