diff options
author | Matti Picus <matti.picus@gmail.com> | 2020-08-17 17:05:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-17 17:05:30 +0300 |
commit | adf50b93d76fa55d03ccbc4910602cda23b32c7d (patch) | |
tree | 9e83978cdf6dfa07ab49ee5ec5f34cfcf810e33b /numpy/core | |
parent | cec45adb1a6aae140996d78adbe170fb8328c681 (diff) | |
parent | dd2ddde2a22b80ad8fee815276065f417bdd2177 (diff) | |
download | numpy-adf50b93d76fa55d03ccbc4910602cda23b32c7d.tar.gz |
Merge pull request #16841 from marload/fix-issue-16813
BUG: linspace should round towards -infinity
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/function_base.py | 8 | ||||
-rw-r--r-- | numpy/core/tests/test_function_base.py | 5 |
2 files changed, 13 insertions, 0 deletions
diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index f57e95742..b2f17cfeb 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -34,6 +34,11 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, .. versionchanged:: 1.16.0 Non-scalar `start` and `stop` are now supported. + .. versionchanged:: 1.20.0 + Values are rounded towards ``-inf`` instead of ``0`` when an + integer ``dtype`` is specified. The old behavior can + still be obtained with ``np.linspace(start, stop, num).astype(int)`` + Parameters ---------- start : array_like @@ -160,6 +165,9 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, if axis != 0: y = _nx.moveaxis(y, 0, axis) + + if _nx.issubdtype(dtype, _nx.integer): + _nx.floor(y, out=y) if retstep: return y.astype(dtype, copy=False), step diff --git a/numpy/core/tests/test_function_base.py b/numpy/core/tests/test_function_base.py index 62a9772c8..dad7a5883 100644 --- a/numpy/core/tests/test_function_base.py +++ b/numpy/core/tests/test_function_base.py @@ -402,3 +402,8 @@ class TestLinspace: stop = array(2, dtype='O') y = linspace(start, stop, 3) assert_array_equal(y, array([1., 1.5, 2.])) + + def test_round_negative(self): + y = linspace(-1, 3, num=8, dtype=int) + t = array([-1, -1, 0, 0, 1, 1, 2, 3], dtype=int) + assert_array_equal(y, t) |