Skip to content

astype(int) silently returns wrong answer under Windows #17640

@Rik-de-Kort

Description

@Rik-de-Kort

Casting an array of float64 to int using astype with as argument int will yield the value -2147483648 under windows.

Reproducing code example:

x = 2384351503.0
np.testing.assert_array_equal(np.array([x]).astype(int), np.array([int(x)]))

I expect this test to go through, but it fails with the following message:

Traceback (most recent call last):
  File "C:\Users\koRR\AppData\Local\conda\conda\envs\LRE\lib\site-packages\IPython\core\interactiveshell.py", line 3417, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-10-725c5d2f7abb>", line 1, in <module>
    np.testing.assert_array_equal(np.array([x]).astype(int), np.array([int(x)]))
  File "C:\Users\koRR\AppData\Local\conda\conda\envs\LRE\lib\site-packages\numpy\testing\_private\utils.py", line 931, in assert_array_equal
    verbose=verbose, header='Arrays are not equal')
  File "C:\Users\koRR\AppData\Local\conda\conda\envs\LRE\lib\site-packages\numpy\testing\_private\utils.py", line 840, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Arrays are not equal
Mismatched elements: 1 / 1 (100%)
Max absolute difference: 4531835151
Max relative difference: 1.90065733

Using dtypes np.int64 works fine, already np.int32 returns the wrong answer (although that's 0 and the above result occurs only with the builtin int or np.int).

NumPy/Python version information:

Numpy version: 1.19.1
System version: '3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]'
Bug is taking place under Windows 10. Cannot reproduce on my Arch system with numpy 1.19.2 and python 3.8.5 built using GCC.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions