Skip to content

Handle edge cases in linspace() for num < 2#98

Closed
leofernandezg wants to merge 1 commit intocdelker:masterfrom
leofernandezg:fix/linspace-division-by-zero
Closed

Handle edge cases in linspace() for num < 2#98
leofernandezg wants to merge 1 commit intocdelker:masterfrom
leofernandezg:fix/linspace-division-by-zero

Conversation

@leofernandezg
Copy link
Copy Markdown

Summary

  • Fixes ZeroDivisionError when calling linspace() with num=1

Problem

linspace(start, stop, num=1) computes step = (stop - start) / (num - 1), which divides by zero. This function is used internally by roundcorners() and SegmentArc.get_bbox().

Changes

  • schemdraw/util.py: Return [start] when num=1 and [] when num=0, consistent with numpy.linspace behavior
  • test/test_linspace.py: New test file covering num=0, num=1, num=2, and default cases

Test results

Without fix: test_linspace_num_1 fails with ZeroDivisionError
With fix: all 4 tests pass

Fixes #93

linspace(start, stop, num=1) raised ZeroDivisionError because
step = (stop - start) / (num - 1) divides by zero when num is 1.

Return [start] when num=1 and [] when num=0, consistent with
numpy.linspace behavior. Added test/test_linspace.py.

Fixes cdelker#93
@leofernandezg
Copy link
Copy Markdown
Author

Test suite results

Full test suite (py.test --nbval-lax test/) run on this branch produces the same results as master:

Branch Failed Passed
master (baseline) 38 432
this branch 38 436

The 38 pre-existing failures are in test_parselogic.ipynb and test_timing.ipynb (unrelated to this change).

@leofernandezg
Copy link
Copy Markdown
Author

Update: Re-ran the full test suite with all optional dependencies installed (matplotlib, ziamath, pyparsing).

Branch Passed Failed
master 470 0
fix/linspace-division-by-zero 474 0

No regressions introduced.

@cdelker cdelker closed this Apr 11, 2026
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.

linspace() raises ZeroDivisionError when num=1

2 participants