Docs/Guarantees and recipes
NaN Semantics Cheat Sheet
How each common operation handles NaN, verified against NumPy:
| Operation | Input contains NaN → Result |
|---|---|
add, sub, mul, div |
NaN-in → NaN-out (per-element) |
min, max (reductions) |
NaN propagates (uses nan_min / nan_max, not f64::min / f64::max) |
argmin, argmax |
Returns index of first NaN if any; else index of min/max |
sum, prod, mean, var, std |
NaN propagates (any NaN in → NaN out) |
nansum, nanmean, nanstd, etc. |
NaN-aware: ignores NaN, reduces over remaining |
cumsum, cumprod |
NaN propagates from first NaN forward |
cummin, cummax |
NaN propagates through accumulator from first occurrence |
sort, argsort |
NaN sorts to end (uses explicit nan_last_cmp, not partial_cmp().unwrap_or(Equal)) |
partition, argpartition |
NaN treated as max; ends up in the upper partition |
median, percentile, quantile |
Early-return NaN if any input is NaN |
ptp (peak-to-peak) |
NaN propagates (both UFuncArray and MaskedArray) |
unique |
NaN appears once in output (per NumPy convention; multiple NaNs collapse) |
isin |
NaN matches NaN (numerically, NaN != NaN, but isin uses element equality semantics) |
clip |
NaN propagates from value or bounds |
where(cond, x, y) |
NaN propagates from selected branch |
comparison ops (==, <, >, ...) |
Per IEEE 754: all comparisons with NaN are False |
logical_and, logical_or |
Truthiness of NaN is True (matches NumPy boolean coercion) |
equal_nan=True parameters (in allclose, array_equal) |
NaN compares equal to NaN |
The general rule: arithmetic propagates NaN, reductions propagate NaN unless they have nan* variants, sort puts NaN last, and median early-returns. These are explicit fixture cases in crates/fnp-ufunc/tests/ and were locked in by a correctness sweep documented in the CHANGELOG ("NaN Propagation" section).