Skip to content

fix: propagate NaN in axis/arg reductions and Maximum/Minimum like numpy - #7

Merged
tannevaled merged 1 commit into
mainfrom
fix/nan-propagation
Oct 4, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/nan-propagation

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Max/Min already propagate NaN (numpy.max semantics); the rest of the family did not.

call before numpy
MaxAxis(0) over column [9, NaN, 3]-style input 9 NaN
ArgMax([+Inf, NaN]) 0 1
ArgMinAxis with a NaN after the first element index of the finite minimum index of the first NaN
Maximum(NaN, +Inf) / Minimum(NaN, -Inf) ±Inf NaN

Causes: v > dst comparisons in MaxAxis/MinAxis/Arg* skip a NaN that is not first and keep one that is; math.Max/math.Min return the infinity before checking for NaN. The kernels now use the builtin max/min (NaN-propagating, as Max already does) and the arg kernels return the index of the first NaN.

Found with a differential sweep against numpy 2.5.3 (2640 random cases including NaN/Inf/signed zeros and strided/transposed views; the harness is not committed). nan_test.go holds numpy's values for one fixture; each of the four kernel changes was reverted in turn and the test failed each time.

🤖 Generated with Claude Code

Max/Min were already NaN-propagating, but the rest of the family was not:

- MaxAxis/MinAxis used `v > dst`, so a NaN after the first element was
  skipped and a leading NaN stuck: max over [9, NaN, 3] gave 9 (numpy: NaN).
- ArgMax/ArgMin and their axis forms ignored NaN the same way: argmax of
  [inf, NaN] gave 0 (numpy: 1, the index of the first NaN).
- Maximum/Minimum used math.Max/math.Min, which special-case +-Inf ahead of
  NaN: maximum(NaN, +Inf) gave +Inf (numpy: NaN).

Found by a differential sweep against numpy 2.5.3 (2640 random cases with
NaN/Inf/signed zeros, strided and transposed views). The expectations in
nan_test.go are numpy values.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 61968e9 into main Oct 4, 2026
9 checks passed
@tannevaled
tannevaled deleted the fix/nan-propagation branch October 4, 2026 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant