8000 Merge pull request #6057 from bertrand-l/linspace_nonnegative_num · githubmlai/numpy@0c97cde · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c97cde

Browse files
committed
Merge pull request numpy#6057 from bertrand-l/linspace_nonnegative_num
EHN: raise error for negative 'num' in linspace.
2 parents 461c997 + 43d4aa5 commit 0c97cde

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

numpy/core/function_base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):
2525
evenly spaced samples, so that `stop` is excluded. Note that the step
2626
size changes when `endpoint` is False.
2727
num : int, optional
28-
Number of samples to generate. Default is 50.
28+
Number of samples to generate. Default is 50. Must be non-negative.
2929
endpoint : bool, optional
3030
If True, `stop` is the last sample. Otherwise, it is not included.
3131
Default is True.
@@ -82,6 +82,8 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None):
8282
8383
"""
8484
num = int(num)
85+
if num < 0:
86+
raise ValueError("Number of samples, %s, must be non-negative." % num)
8587
div = (num - 1) if endpoint else num
8688

8789
# Convert float/complex array scalars to float, gh-3504

numpy/core/tests/test_function_base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def test_basic(self):
3434
assert_(y[-1] == 10)
3535
y = linspace(2, 10, endpoint=0)
3636
assert_(y[-1] < 10)
37+
assert_raises(ValueError, linspace, 0, 10, num=-1)
3738

3839
def test_corner(self):
3940
y = list(linspace(0, 1, 1))

0 commit comments

Comments
 (0)
0