Skip to content

BUG: loss of precision in longdouble min (nightly build) #20863

@effigies

Description

@effigies

Describe the issue:

My pre-release tests have begun breaking, and I've tracked it down to a small issue. See the minimal reproduction below.

The use case is rescaling an array into a smaller dtype, which requires finding the bounds of the array and target dtype. The specific failing use case is when squeezing float128 into int32, but I provide below a more thorough regression test that demonstrates that this occurs in a number of cases, including float16 -> int16, float32 -> int32, and float128 -> int64.

Reproduce the code example:

import numpy as np
np.min(np.float128([np.inf, 2147483647]))
# Result: 2147483648.0

# More thorough test
for ft in np.sctypes["float"]:
    for it in np.sctypes["int"]:
        fmax = np.finfo(ft).max
        imax = np.iinfo(it).max
        shared_max = np.min(ft([fmax, imax]))
        check = shared_max <= imax
        print(f"{it} {ft} {check} {fmax} {imax} {shared_max}")

Error message:

<class 'numpy.int8'> <class 'numpy.float16'> True 65504.0 127 127.0
<class 'numpy.int16'> <class 'numpy.float16'> False 65504.0 32767 32768.0
<class 'numpy.int32'> <class 'numpy.float16'> True 65504.0 2147483647 65504.0
<class 'numpy.int64'> <class 'numpy.float16'> True 65504.0 9223372036854775807 65504.0
<class 'numpy.int8'> <class 'numpy.float32'> True 3.4028234663852886e+38 127 127.0
<class 'numpy.int16'> <class 'numpy.float32'> True 3.4028234663852886e+38 32767 32767.0
<class 'numpy.int32'> <class 'numpy.float32'> False 3.4028234663852886e+38 2147483647 2147483648.0
<class 'numpy.int64'> <class 'numpy.float32'> True 3.4028234663852886e+38 9223372036854775807 9.223372036854776e+18
<class 'numpy.int8'> <class 'numpy.float64'> True 1.7976931348623157e+308 127 127.0
<class 'numpy.int16'> <class 'numpy.float64'> True 1.7976931348623157e+308 32767 32767.0
<class 'numpy.int32'> <class 'numpy.float64'> True 1.7976931348623157e+308 2147483647 2147483647.0
<class 'numpy.int64'> <class 'numpy.float64'> True 1.7976931348623157e+308 9223372036854775807 9.223372036854776e+18
<class 'numpy.int8'> <class 'numpy.float128'> True inf 127 127.0
<class 'numpy.int16'> <class 'numpy.float128'> True inf 32767 32767.0
<class 'numpy.int32'> <class 'numpy.float128'> False inf 2147483647 2147483648.0
<class 'numpy.int64'> <class 'numpy.float128'> False inf 9223372036854775807 9.223372036854776e+18

NumPy/Python version information:

1.23.0.dev0+437.g4b985e2b8 3.8.12 (default, Oct 12 2021, 13:49:34)
[GCC 7.5.0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions