How to set a number to NaN or infinity?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
NaN (Not-a-Number) and infinity are special floating-point values defined by the IEEE 754 standard. NaN represents undefined or unrepresentable results (like 0/0), while infinity represents values that exceed the maximum representable number. Every major language provides constants or functions to create and check for these values. Understanding their behavior is essential because NaN has unusual comparison properties — it is not equal to itself — and arithmetic with infinity follows specific rules.
Python
NumPy
JavaScript
Java
C / C++
C# / .NET
Arithmetic Rules
Practical Uses
Common Pitfalls
- Checking NaN with
==:NaN == NaNisFalsein every language — this is defined by IEEE 754. Always usemath.isnan()(Python),Number.isNaN()(JavaScript),Double.isNaN()(Java), orstd::isnan()(C++) to check for NaN. - Using JavaScript's global
isNaN()instead ofNumber.isNaN(): The globalisNaN("hello")returnstruebecause it coerces the string to a number first.Number.isNaN("hello")correctly returnsfalse. Always useNumber.isNaN()for accurate NaN checks. - Integer division producing infinity in some languages: In Python,
1 / 0raisesZeroDivisionError, not infinity. Only floating-point division1.0 / 0.0produces infinity in C/Java. In JavaScript,1 / 0givesInfinitybecause all numbers are floats. - NaN in sorting and comparisons: NaN is not less than, greater than, or equal to any value (including itself). Sorting arrays containing NaN produces unpredictable results. Filter out NaN values before sorting.
- Serializing NaN and infinity to JSON: JSON does not support
NaN,Infinity, or-Infinity. Python'sjson.dumps(float('nan'))raisesValueErrorwithallow_nan=False, or produces non-standard output. Replace special values before serialization.
Summary
- Use
float('nan')/math.nan(Python),NaN(JavaScript),Double.NaN(Java/C#) to create NaN - Use
float('inf')/math.inf(Python),Infinity(JavaScript),Double.POSITIVE_INFINITY(Java/C#) for infinity - Always use language-specific functions to check for NaN — never use
==because NaN is not equal to itself - NaN propagates through all arithmetic operations
- Infinity follows algebraic rules except
inf - inf = NaNandinf * 0 = NaN - Filter NaN values before sorting, aggregating, or serializing to JSON
Related reading
- How to set credentials in AWS SDK v3 JavaScript?
- How to set focus on an input field after rendering?
- How to set initialValues based on async source such as an ajax call with redux-form
- How to set iOS status bar background color in React Native?
- How to setup Node environment variable in Dockerfile for running node.js application?
- How to shard across a cluster of nodes based on the value of String?
- How to skip over an element in .map()?
- How to sleep the thread in node.js without affecting other threads?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.