Fix IEEE 754 compliance for real number operations#29
Open
blasphemetheus wants to merge 1 commit intoelixir-nx:mainfrom
Open
Fix IEEE 754 compliance for real number operations#29blasphemetheus wants to merge 1 commit intoelixir-nx:mainfrom
blasphemetheus wants to merge 1 commit intoelixir-nx:mainfrom
Conversation
Erlang's :math module raises ArithmeticError for overflow and domain errors instead of returning IEEE 754 special values. This causes downstream libraries (like Nx) to crash on valid inputs. Fixes: - exp(large): returns :infinity instead of crashing - sinh(large): returns :infinity/:neg_infinity based on sign - cosh(large): returns :infinity - asin/acos outside [-1,1]: returns :nan instead of crashing - acosh below 1: returns :nan - atanh outside (-1,1): returns :nan - atanh(1): returns :infinity, atanh(-1): returns :neg_infinity - sqrt(negative): returns :nan instead of crashing - pow(0, negative): returns :infinity instead of crashing - divide(x, 0.0): returns :infinity/:neg_infinity/:nan per IEEE 754 - divide(x, -0.0): respects negative zero sign (OTP 27+ matching) - cot(0): returns :infinity instead of crashing (1/tan(0) = 1/0) - acot(0): returns pi/2 instead of crashing - acsc(0): returns :infinity instead of crashing - acsc(values < 1): returns :nan for domain errors 34 new tests covering all IEEE 754 edge cases. All 169 tests pass (99 doctests + 70 tests). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
blasphemetheus
added a commit
to blasphemetheus/nx
that referenced
this pull request
Mar 19, 2026
…ter f64 Four independent fixes: 1. window_scatter_max/min on f64: binary size mismatch. Fixed by casting scatter result to output type before to_binary. 2. Nx.slice on scalar tensor: bin_slice crashed calling hd([]) on empty strides list. Added scalar guard clause. 3. Nx.linspace with n=1: divided by zero computing step size. Special-cased to return start value directly. 4. Nx.gather with scalar indices: gave unhelpful Erlang error. Moved shape check before indexed_axes call. IEEE 754 overflow/domain/divzero tests are skipped pending upstream fix in the Complex library (elixir-nx/complex#29). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Erlang's :math module raises ArithmeticError for overflow and domain errors instead of returning IEEE 754 special values. This causes downstream libraries (like Nx: elixir-nx/nx#1707) to crash on valid inputs.
Fixes:
34 new tests covering all IEEE 754 edge cases.
All 169 tests pass (99 doctests + 70 tests).