diff options
author | Jaime <jaime.frio@gmail.com> | 2015-07-09 00:01:59 -0700 |
---|---|---|
committer | Jaime <jaime.frio@gmail.com> | 2015-07-09 00:01:59 -0700 |
commit | 0c97cdeb1b29776f927fea4830c802e0339ac8dd (patch) | |
tree | 364ef491b8642c2becd7c9714c0c526fe03c4875 | |
parent | 461c997075723cc8812692458d7b2ea9c839336e (diff) | |
parent | 43d4aa5de8a991fa40c9976280cef5b4b080a806 (diff) | |
download | numpy-0c97cdeb1b29776f927fea4830c802e0339ac8dd.tar.gz |
Merge pull request #6057 from bertrand-l/linspace_nonnegative_num
EHN: raise error for negative 'num' in linspace.
-rw-r--r-- | numpy/core/function_base.py | 4 | ||||
-rw-r--r-- | numpy/core/tests/test_function_base.py | 1 |
2 files changed, 4 insertions, 1 deletions
diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index e50e1a505..532ef2950 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -25,7 +25,7 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): evenly spaced samples, so that `stop` is excluded. Note that the step size changes when `endpoint` is False. num : int, optional - Number of samples to generate. Default is 50. + Number of samples to generate. Default is 50. Must be non-negative. endpoint : bool, optional If True, `stop` is the last sample. Otherwise, it is not included. Default is True. @@ -82,6 +82,8 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): """ num = int(num) + if num < 0: + raise ValueError("Number of samples, %s, must be non-negative." % num) div = (num - 1) if endpoint else num # Convert float/complex array scalars to float, gh-3504 diff --git a/numpy/core/tests/test_function_base.py b/numpy/core/tests/test_function_base.py index a64d44473..aba030f3d 100644 --- a/numpy/core/tests/test_function_base.py +++ b/numpy/core/tests/test_function_base.py @@ -34,6 +34,7 @@ class TestLinspace(TestCase): assert_(y[-1] == 10) y = linspace(2, 10, endpoint=0) assert_(y[-1] < 10) + assert_raises(ValueError, linspace, 0, 10, num=-1) def test_corner(self): y = list(linspace(0, 1, 1)) |