Skip to content

atan2: propagate NaN instead of returning 0/pi at x == +/-0#35

Merged
gokulkrishna98 merged 2 commits into
apple:mainfrom
gokulkrishna98:fix/atan2-nan-propagation
Jul 8, 2026
Merged

atan2: propagate NaN instead of returning 0/pi at x == +/-0#35
gokulkrishna98 merged 2 commits into
apple:mainfrom
gokulkrishna98:fix/atan2-nan-propagation

Conversation

@gokulkrishna98

@gokulkrishna98 gokulkrishna98 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Description

The atan2 converter's x=0 branch classifies inputs purely via comparisons (y > 0, 0 > y, etc.), which are all False when NaN is involved. This meant atan2(NaN, +0.0) incorrectly returned 0 and atan2(NaN, -0.0) incorrectly returned pi, instead of NaN as required by IEEE-754 and matched by torch.atan2.

This adds an explicit NaN check (y != y or x != x) that takes priority over every other branch in the decomposition, so NaN now always propagates through correctly.

Testing

  • Added test_nan_propagation to TestAtan2 in tests/ops/test_ops.py, covering NaN in y, x, and both, including at x = +/-0.
  • Updated the three TestAtan2IR FileCheck patterns in tests/ops/test_ops_ir.py to match the new IR shape (extra NaN constant + final NaN-select where).
  • Full tests/ops/ suite passes locally.

atan2's x=0 branch classified purely via comparisons, which are all
False for NaN and misclassified atan2(NaN, +0) as 0 and
atan2(NaN, -0) as pi instead of NaN. Add an explicit NaN check
(y != y or x != x) that takes priority over every other branch, plus
a regression test and updated IR FileCheck patterns.
@gokulkrishna98
gokulkrishna98 marked this pull request as ready for review July 8, 2026 21:01
@gokulkrishna98
gokulkrishna98 merged commit 1b3cb3b into apple:main Jul 8, 2026
2 checks passed
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.

2 participants