How to set a number to NaN or infinity?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.