Skip to content

BUG: numpy 2.0 dividing np.uint8 by python int leads to OverflowError #25639

Closed
@MatthewFlamm

Description

@MatthewFlamm

Describe the issue:

When preparing for upgrading to numpy 2.0, I'm seeing differences in handling of division of numpy integer types with python integer types.

Reproduce the code example:

import numpy as np

a = np.uint8(15)

# results in a np.float64 in both versions (not shown explicitly in code)
print(a / 100)

# Gives the following error only in numpy 2.0
#   OverflowError: Python integer 256 out of bounds for uint8
print(a / 256)

Error message:

OverflowError: Python integer 256 out of bounds for uint8

Python and NumPy Versions:

2.0.0.dev0+git20240113.d2f60ff
3.10.13 (main, Jan 10 2024, 19:45:45) [GCC 9.4.0]

Runtime Environment:

[{'numpy_version': '2.0.0.dev0+git20240113.d2f60ff',
  'python': '3.10.13 (main, Jan 10 2024, 19:45:45) [GCC 9.4.0]',
  'uname': uname_result(system='Linux', node='codespaces-c4dd1c', release='6.2.0-1018-azure', version='#18~22.04.1-Ubuntu SMP Tue Nov 21 19:25:02 UTC 2023', machine='x86_64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2'],
                      'not_found': ['AVX512F',
                                    'AVX512CD',
                                    'AVX512_KNL',
                                    'AVX512_KNM',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL']}},
 {'architecture': 'Zen',
  'filepath': '/workspaces/dotfiles/numpy_2/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-0cf96a72.3.23.dev.so',
  'internal_api': 'openblas',
  'num_threads': 2,
  'prefix': 'libopenblas',
  'threading_layer': 'pthreads',
  'user_api': 'blas',
  'version': '0.3.23.dev'}]

Context for the issue:

Is this a planned change for numpy 2.0? I could not find any indication in the release notes or migration guide. If so, would the recommended workaround be to first cast a, in the code example, to float64 or a uint type that can handle larger values? Or perhaps convert the python int into a suitable numpy integer type?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions