Ian Ozsvald <[email protected]> added the comment: np.sum does sum correctly if I force a more appropriate dtype. The numpy doc notes: http://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html "dtype : dtype, optional The type of the returned array and of the accumulator in which the elements are summed. By default, the dtype of a is used. An exception is when a has an integer type with less precision than the default platform integer. In that case, the default platform integer is used instead."
If I force a longer dtype then it sums correctly: >>>> np.sum(np.array([True]*128)) -128 >>>> np.sum(np.array([True]*128), dtype=np.int64) 128 >>>> np.sum(np.array([True]*65536)) 0 >>>> np.sum(np.array([True]*65536), dtype=np.int16) # not long enough 0 >>>> np.sum(np.array([True]*65536), dtype=np.int32) # long enough 65536 >>>> np.sum(np.array([True]*65536), dtype=np.int64) # long enough 65536 Maybe np.sum's behaviour should be to use np.int_ if the dtype of the array to be summed is shorter than np.int_? It looks as though int summation works correctly: >>>> print "%e" % np.sum(np.array([int(1e12)] * int(1e6))) 1.000000e+18 >>>> print "%e" % np.sum(np.array([int(1e12)] * int(1e7))) # an int64 overflows as expected (same result as CPython numpy) -8.446744e+18 >>>> print "%e" % sys.maxint 9.223372e+18 Floats from numpy preserve their precision (this mirrors CPython numpy's behaviour): >>>> type(np.sum(np.array([np.float16(0.1)]))) <type 'numpy.float16'> >>>> type(np.sum(np.array([np.float32(0.1)]))) <type 'numpy.float32'> >>>> type(np.sum(np.array([np.float64(0.1)]))) <type 'numpy.float64'> Python Floats are summed using a float64 (this mirrors CPython numpy's behaviour): >>>> type(np.sum(np.array([float(1)]))) <type 'numpy.float64'> >>>> type(np.sum(np.array([float(1e70)] * int(1e6)))) <type 'numpy.float64'> Versions: PyPy pypy-c-jit-68443-d5e489e07679-linux64 with numpy 1.8.0.dev-6b8df83 CPython 2.7.3 with numpy 1.8.0 ________________________________________ PyPy bug tracker <[email protected]> <https://bugs.pypy.org/issue1663> ________________________________________ _______________________________________________ pypy-issue mailing list [email protected] https://mail.python.org/mailman/listinfo/pypy-issue
