Skip to content

BUG: incorrect promotion for Python integer inputs in ldexp #29182

@raffienficiaud

Description

@raffienficiaud

Describe the issue:

Depending on the input types, the return value of ldexp raises an overflow or returns "infinity", while the same call with python integers just works.

Reproduce the code example:

>>> import numpy as np

# this should work: note the return type should not be float16 in this case.
>>> np.ldexp(1, np.int32(16))
<stdin>:1: RuntimeWarning: overflow encountered in ldexp
np.float16(inf)

# this should work, interestingly I do not see the overflow
>>> np.ldexp(1, np.int64(16))
np.float16(inf)

# ok, this is float
>>> np.ldexp(1., np.int64(16))
np.float64(65536.0)

# this just works fine
>>> np.ldexp(1, int(16))
np.float64(65536.0)

Error message:

See above.

Python and NumPy Versions:

  • python3.10
  • numpy2.2.6, conda hash py310hefbff90_0

Runtime Environment:

>>> import numpy; numpy.show_runtime()
[{'numpy_version': '2.2.6',
  'python': '3.10.18 | packaged by conda-forge | (main, Jun  4 2025, 14:45:41) '
            '[GCC 13.3.0]',
  'uname': uname_result(system='Linux', node='odin', release='6.1.0-32-amd64', version='#1 SMP PREEMPT_DYNAMIC Debian 6.1.129-1 (2025-03-06)', 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',
                                    'AVX512_SPR']}}]

Context for the issue:

Code that used to work with previous versions of python now breaks, with a trivial workaround.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions