Skip to content

BUG: min_scalar_type returns wrong datatype for high values of float 32 #30342

@julian-belina

Description

@julian-belina

Describe the issue:

The min_scalar_type function returns the incorrect data type for values close to the maximum float32. Using the finfo function, I extracted the maximum values of a float32.

float_32_max = np.finfo("float32").max
print(float_32_max)

3.4028235e38

However when I use it to determine the minmum scalar type float64 is returned

Also reducing the number a bit does not help e.g.

detected_data_type = np.min_scalar_type(3.402e38)
print(detected_data_type)

float64

float_64_max = np.finfo("float64").max
print(float_64_max)
detected_data_type = np.min_scalar_type(1.7976931348623157e308)
print(detected_data_type) 

1.7976931348623157e+308
float64

This works properly for float 64

Reproduce the code example:

import numpy as np

float_32_max = np.finfo("float32").max
print(float_32_max)
detected_data_type = np.min_scalar_type(3.4028235e38)
print(detected_data_type)


float_64_max = np.finfo("float64").max
print(float_64_max)
detected_data_type = np.min_scalar_type(1.7976931348623157e308)
print(detected_data_type)


detected_data_type = np.min_scalar_type(3.402e38)
print(detected_data_type)

Python and NumPy Versions:

2.3.2
3.13.5 | packaged by conda-forge | (main, Jun 16 2025, 08:27:50) [GCC 13.3.0]

Runtime Environment:

[{'numpy_version': '2.3.2',
'python': '3.13.5 | packaged by conda-forge | (main, Jun 16 2025, 08:27:50) '
'[GCC 13.3.0]',
'uname': uname_result(system='Linux', node='iek2012', release='5.14.0-570.42.2.el9_6.x86_64', version='#1 SMP PREEMPT_DYNAMIC Sun Sep 14 13:59:34 UTC 2025', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3', 'SSE41', 'POPCNT', 'SSE42'],
'not_found': ['AVX',
'F16C',
'FMA3',
'AVX2',
'AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL',
'AVX512_SPR']}},
{'architecture': 'Prescott',
'filepath': '/fast/home/j-belina/miniforge3/envs/geokit_env/lib/libopenblasp-r0.3.30.so',
'internal_api': 'openblas',
'num_threads': 8,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.30'}]

Context for the issue:

I am trying to handle overflow errors with GDAL, which relies on C data types. I use GDAL in combination with NumPy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions