is_nan

is_nan(float $num): bool

Checks whether a value is NaN (not a number).

C C23

C23
#include <math.h>
#include <stdio.h>

int main(void) {
    double nan = NAN; /* from math.h */
    printf("%d %d\n", isnan(nan), isnan(1.0)); /* macros return int 0/1 */
    printf("%d\n", nan == nan); /* the famous trap: NaN is not equal to itself */
    return 0;
}
Output
1 0
0

C++ C++26

C++26
#include <cmath>
#include <iostream>
#include <limits>

int main() {
    double nan = std::numeric_limits<double>::quiet_NaN();
    std::cout << std::boolalpha;
    std::cout << std::isnan(nan) << " " << std::isnan(1.0) << "\n";
    std::cout << (nan == nan) << "\n"; // NaN != NaN, per IEEE 754
    return 0;
}
Output
true false
false

C# v11.0.100

v11.0.100
double nan = double.NaN;
Console.WriteLine($"{double.IsNaN(nan)} {double.IsNaN(1.0)}");
Console.WriteLine(nan == nan); // false: NaN is never equal to anything
Output
True False
False

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math"
)

func main() {
	nan := math.NaN()
	fmt.Println(math.IsNaN(nan), math.IsNaN(1.0))
	fmt.Println(nan == nan) // false: even Go keeps the IEEE 754 rule
}
Output
true false
false

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        double nan = Double.NaN;
        System.out.println(Double.isNaN(nan) + " " + Double.isNaN(1.0));
        System.out.println(nan == nan); // false: even Java keeps the IEEE 754 rule
    }
}
Output
true false
false

JavaScript v26.13.1

v26.13.1
const nan = NaN;
// Number.isNaN never coerces; the legacy global isNaN("abc") would coerce and return true
console.log(Number.isNaN(nan), Number.isNaN(1.0));
console.log(nan === nan); // the famous trap: NaN is not equal to itself
Output
true false
false

PHP v8.6.0

v8.6.0
var_dump(is_nan(NAN));
var_dump(is_nan(1.0));
var_dump(NAN == NAN);  // the famous trap: NaN is not equal to itself
var_dump(NAN === NAN); // even a strict comparison fails
Output
bool(true)
bool(false)
bool(false)
bool(false)

Python v3.15.2

v3.15.2
import math

nan = float("nan")
print(math.isnan(nan), math.isnan(1.0))
print(nan == nan)  # the famous trap: NaN is not equal to itself
Output
True False
False

Rust v1.99.1

v1.99.1
fn main() {
    let nan = f64::NAN;
    println!("{} {}", nan.is_nan(), 1.0f64.is_nan());
    println!("{}", nan == nan); // Rust keeps the IEEE 754 rule too: NaN != NaN
}
Output
true false
false

TypeScript v26.13.1

v26.13.1
const nan: number = NaN;
console.log(`${Number.isNaN(nan)} ${Number.isNaN(1.0)}`);
console.log(nan === nan); // the famous trap: NaN is not equal to itself
Output
true false
false

Language differences

Where NaN comes from: IEEE 754 reserves it for invalid operations (0/0, INF-INF, log(-1)) — it equals nothing, not even itself, and every language shares that behavior (line 2 of every example prints false alike).

C: isnan is a C99 macro (example) returning int 0/1 rather than bool; the NaN literal is the NAN macro from math.h; note that compiler -ffast-math breaks NaN semantics (this site's builds do not enable it).

C++: std::isnan (example) plus quiet_NaN() to produce NaN; there is also signaling_NaN (the variant that raises FP exceptions) — daily use is the quiet one.

C#: double.IsNaN (example); x != x also works as a quick NaN test in C#, and the compiler will not optimize the comparison away.

Go: math.IsNaN (example); NaN propagation through == / map keys / slice comparisons is notoriously sneaky — a NaN key in a map can never be found (hash hits are impossible) and sort results become undefined.

Java: Double.isNaN (example); the classic gotcha is Double.compare(nan, nan) returning 0 (compare deliberately treats NaN as equal and greater than +INF) — == and compare have opposite semantics.

JavaScript: the biggest trap is the global isNaN coercing its argument first (isNaN("abc") === true), while Number.isNaN (example) never coerces and is the real test; x !== x is the veteran NaN idiom; Object.is(nan, nan) is the only comparison that returns true.

PHP: is_nan (example) takes floats only — ints are converted to float, strings raise a TypeError (PHP 8 onward); NaN compares false with every operator (==/===/</>), and sort functions behave undefined when NaN is present.

Python: math.isnan (example); float("nan") equals math.nan; beyond the shared trap, Python's quirk is that nan == nan is False while nan in [nan] is True (membership uses an identity check first).

Rust: f64::is_nan (example); Rust guarantees no fast-math reordering, so nan == nan is stably false; for ordering use partial_cmp/total_cmp to handle the NaN branch.

TypeScript: identical to JS (example); extra advice — prefer Number.isNaN over the global and consider banning the latter via eslint's no-restricted-globals.