numpy
numpy copied to clipboard
numpy.round(i, decimals=d) wrong for np.int64 and d<0
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)]
Caused by np.ndarray.round using floats internally
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)
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.