Repository navigation
Return infinities for overflowing real arithmetic - #31
Merged
polvalente merged 1 commit intoOct 8, 2026
Merged
Conversation
BEAM floats raise ArithmeticError when a result overflows, so
Complex.add, subtract, multiply, divide and pow on real numbers crashed
where IEEE 754 calls for an infinity. pow already rescued the error but
answered :nan for every case except 0 to a negative power, which hid
overflow behind a domain-error answer.
Rescue the overflow in the five real-number clauses and pick the sign
from the operands. pow keeps :nan for a negative base with a
non-integer exponent and returns :neg_infinity for a negative base with
an odd exponent. The %Complex{} clauses compute component-wise through
these functions, so complex values pick up the same behaviour.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
blasphemetheus
commented
Oct 7, 2026
blasphemetheus
commented
Oct 7, 2026
blasphemetheus
marked this pull request as ready for review
October 7, 2026 06:55
polvalente
reviewed
Oct 8, 2026
Comment on lines
-409
to
+413
| def add(left, right) when is_number(left) and is_number(right), do: left + right | ||
| def add(left, right) when is_number(left) and is_number(right) do | ||
| left + right | ||
| rescue | ||
| ArithmeticError -> if left > 0, do: :infinity, else: :neg_infinity | ||
| end |
Member
There was a problem hiding this comment.
I think this should be a contribution to :complex yet again
Member
There was a problem hiding this comment.
Ugh, I changed tabs too much. This is :complex hahhaha
polvalente
approved these changes
Oct 8, 2026
Comment on lines
-409
to
+413
| def add(left, right) when is_number(left) and is_number(right), do: left + right | ||
| def add(left, right) when is_number(left) and is_number(right) do | ||
| left + right | ||
| rescue | ||
| ArithmeticError -> if left > 0, do: :infinity, else: :neg_infinity | ||
| end |
Member
There was a problem hiding this comment.
Ugh, I changed tabs too much. This is :complex hahhaha
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.
Follow-up to the IEEE 754 work in ab2149e, which covered the unary and domain-error cases. Binary arithmetic on real numbers still crashes when the exact result overflows a float:
IEEE 754 wants
:infinity,:infinityand:infinity.powalready rescued the error but answered:nanfor everything except zero to a negative power, so an overflow looked like a domain error.This rescues the overflow in the real-number clauses of
add,subtract,multiply,divideandpowand picks the sign from the operands.powkeeps:nanfor a negative base with a non-integer exponent and returns:neg_infinityfor a negative base with an odd exponent. The%Complex{}clauses compute component-wise through these same functions, soComplex.add(Complex.new(max, 1.0), Complex.new(max, 1.0))now givesComplex.new(:infinity, 2.0).Downstream, Nx's
BinaryBackendroutes its element-wise arithmetic through these functions. With this changeNx.add,Nx.multiply,Nx.divideandNx.powon overflowing f64 inputs return infinities instead of raising, andNx.productover 600 f16 values of 1e3 returns infinity instead of raising from its accumulator. All five were verified against the Nx test build with this branch swapped in as the dependency.The new tests fail on
mainand pass here. The existing suite passes.🤖 Generated with Claude Code