Fix Nx.linspace crash with n=1, NaN as well#1710
Open
blasphemetheus wants to merge 1 commit intoelixir-nx:mainfrom
Open
Fix Nx.linspace crash with n=1, NaN as well#1710blasphemetheus wants to merge 1 commit intoelixir-nx:mainfrom
blasphemetheus wants to merge 1 commit intoelixir-nx:mainfrom
Conversation
josevalim
reviewed
Mar 20, 2026
nx/test/nx/linspace_n1_test.exs
Outdated
| result = Nx.linspace(0, 1, n: 5) | ||
| assert Nx.shape(result) == {5} | ||
| end | ||
| end |
Contributor
There was a problem hiding this comment.
Same as the other, let's add those tests directly to test/nx_test.exs. There may be already an existing describe block we could use!
When n=1 and endpoint=true (default), the divisor is n-1=0, causing a divide-by-zero. Special-case n=1 to return start value directly. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
8d9de86 to
5f80016
Compare
polvalente
reviewed
Mar 20, 2026
| iota | ||
| |> multiply(step) | ||
| |> add(start) | ||
| |> as_type(opts[:type]) |
Contributor
There was a problem hiding this comment.
How about we extract the else into a defp?
polvalente
reviewed
Mar 20, 2026
Comment on lines
+3076
to
+3083
| result = Nx.linspace(0, 10, n: 1) | ||
| assert Nx.shape(result) == {1} | ||
| assert Nx.to_flat_list(result) == [0.0] | ||
| end | ||
|
|
||
| test "n=1 with same start/stop" do | ||
| result = Nx.linspace(5, 5, n: 1) | ||
| assert Nx.to_flat_list(result) == [5.0] |
Contributor
There was a problem hiding this comment.
You could use assert_equal here
polvalente
approved these changes
Mar 20, 2026
Contributor
polvalente
left a comment
There was a problem hiding this comment.
Just a few stylistic comments!
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.
linspace generates evenly-spaced numbers between a start and stop value. Linear Spacing.
When n=1 and endpoint=true (default), the divisor is
n-1=0, causing a divide-by-zero. The fix is to special-case n=1 to return start value directly. There's also a divide 0 by 0 possible which produces NaN currently.I'm not sure precisely why one might want to call this function with n=1, but I think it should return sensible results
In the case of the current ArithmeticError: (for any positive int j)
in case of the current NaN: (for any positive int k)
running LinspaceN1Space test file on main
These are edge cases, but the current result in presumably unintended behavior.
originally part of the closed fuzz test edge case PR #1707