diff options
-rw-r--r-- | numpy/core/code_generators/generate_umath.py | 3 | ||||
-rw-r--r-- | numpy/core/src/multiarray/calculation.c | 4 | ||||
-rw-r--r-- | numpy/core/src/umath/funcs.inc.src | 6 | ||||
-rw-r--r-- | numpy/core/tests/test_umath.py | 8 |
4 files changed, 19 insertions, 2 deletions
diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py index a8fa0debc..4af883fc5 100644 --- a/numpy/core/code_generators/generate_umath.py +++ b/numpy/core/code_generators/generate_umath.py @@ -681,6 +681,9 @@ defdict = { ), } +if sys.version_info[0] >= 3: + defdict['divide'] = defdict['true_divide'] + def indent(st,spaces): indention = ' '*spaces indented = indention + st.replace('\n','\n'+indention) diff --git a/numpy/core/src/multiarray/calculation.c b/numpy/core/src/multiarray/calculation.c index d0e94fa6d..78ae26295 100644 --- a/numpy/core/src/multiarray/calculation.c +++ b/numpy/core/src/multiarray/calculation.c @@ -669,7 +669,11 @@ PyArray_Mean(PyArrayObject *self, int axis, int rtype, PyArrayObject *out) return NULL; } if (!out) { +#if defined(NPY_PY3K) + ret = PyNumber_TrueDivide(obj1, obj2); +#else ret = PyNumber_Divide(obj1, obj2); +#endif } else { ret = PyObject_CallFunction(n_ops.divide, "OOO", out, obj2, out); diff --git a/numpy/core/src/umath/funcs.inc.src b/numpy/core/src/umath/funcs.inc.src index 19a0ed217..9208b92d2 100644 --- a/numpy/core/src/umath/funcs.inc.src +++ b/numpy/core/src/umath/funcs.inc.src @@ -6,6 +6,8 @@ * object functions. */ +#include "npy_3kcompat.h" + /* ***************************************************************************** @@ -34,7 +36,11 @@ Py_reciprocal(PyObject *o) if (!one) { return NULL; } +#if defined(NPY_PY3K) + result = PyNumber_TrueDivide(one, o); +#else result = PyNumber_Divide(one, o); +#endif Py_DECREF(one); return result; } diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 584672f30..95f4ee47b 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -6,9 +6,13 @@ import numpy as np class TestDivision(TestCase): def test_division_int(self): - # int division should return the floor of the result, a la Python + # int division should follow Python x = np.array([5, 10, 90, 100, -5, -10, -90, -100, -120]) - assert_equal(x / 100, [0, 0, 0, 1, -1, -1, -1, -1, -2]) + if 5 / 10 == 0.5: + assert_equal(x / 100, [0.05, 0.1, 0.9, 1, + -0.05, -0.1, -0.9, -1, -1.2]) + else: + assert_equal(x / 100, [0, 0, 0, 1, -1, -1, -1, -1, -2]) assert_equal(x // 100, [0, 0, 0, 1, -1, -1, -1, -1, -2]) assert_equal(x % 100, [5, 10, 90, 0, 95, 90, 10, 0, 80]) |