numpy icon indicating copy to clipboard operation
numpy copied to clipboard

numpy.round(i, decimals=d) wrong for np.int64 and d<0

Open miccoli opened this issue 7 years ago • 3 comments

numpy.round returns wrong value when decimals are negative and argument is np.int64

Reproducing code example:

>>> import numpy as np
>>> np.round(2**63-1, -3)
-9223372036854775808
>>> round(2**63-1, -3)
9223372036854776000

Numpy/Python version information:

>>> import sys, numpy; print(numpy.__version__, sys.version)
1.15.1 3.6.5 (default, Apr  5 2018, 23:36:52) 
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)]

miccoli avatar Sep 04 '18 21:09 miccoli

Caused by np.ndarray.round using floats internally

eric-wieser avatar Sep 05 '18 07:09 eric-wieser

Re: Closember this issue is still active at

>>> import sys, numpy; print(numpy.__version__, sys.version)
1.23.5 3.10.4 (main, Jun  9 2022, 14:49:07) [Clang 13.0.0 (clang-1300.0.29.30)]
>>> numpy.around([2**63-1], -3)
array([-9223372036854775808])
>>> numpy.array([round(2**63-1, -3)])
array([9223372036854776000], dtype=uint64)

miccoli avatar Nov 23 '22 07:11 miccoli

We had problems when using numpy.round to a numpy.int64 object. Using Python round and pandas.Int64Dtype instead seems to fix the problem for us. Running some benchmark shows the two methods use similar amount of time across some of our test cases.

Taiquan-Liu avatar Feb 16 '24 09:02 Taiquan-Liu