8000 BUG: Numpy full returns wrong value when casting from integer to float · Issue #27274 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Numpy full returns wrong value when casting from integer to float #27274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BrunoBelucci opened this issue Aug 23, 2024 · 2 comments
Closed
Labels
33 - Question Question about NumPy usage or development

Comments

@BrunoBelucci
Copy link

Describe the issue:

For some cases, when we use np.full with a fill_value that is an integer and with dtype=np.float32, we get a result off by 1.

Reproduce the code example:

import numpy as np
np.full(10, 19211613, np.float32)

Error message:

No response

Python and NumPy Versions:

1.26.4
3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0]

Runtime Environment:

[{'numpy_version': '1.26.4',
'python': '3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) '
'[GCC 12.3.0]',
'uname': uname_result(system='Linux', node='bbelucci-gs75', release='6.8.0-40-generic', version='#40~22.04.3-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 30 17:30:19 UTC 2', 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']}},
{'filepath': '.../lib/libmkl_rt.so.2',
'internal_api': 'mkl',
'num_threads': 6,
'prefix': 'libmkl_rt',
'threading_layer': 'intel',
'user_api': 'blas',
'version': '2022.1-Product'},
{'filepath': '.../lib/libomp.so',
'internal_api': 'openmp',
'num_threads': 12,
'prefix': 'libomp',
'user_api': 'openmp',
'version': None}]

Context for the issue:

The code returns
array([19211612., 19211612., 19211612., 19211612., 19211612., 19211612., 19211612., 19211612., 19211612., 19211612.], dtype=float32)

while I would expect it to return
array([19211613., 19211613., 19211613., 19211613., 19211613., 19211613., 19211613., 19211613., 19211613., 19211613.], dtype=float32)

@seberg seberg added 33 - Question Question about NumPy usage or development and removed 00 - Bug labels Aug 23, 2024
@WarrenWeckesser
Copy link
Member

The integer 19211613 cannot be represented exactly as a single precision (32 bit) floating point value. At that magnitude, the spacing between representable values is 2.

In [16]: np.float32(19211613)
Out[16]: np.float32(19211612.0)

In [17]: x = np.float32(19211613)

In [18]: np.spacing(x)
Out[18]: np.float32(2.0)
2.0

In [19]: np.nextafter(x, np.float32(np.inf))
Out[19]: np.float32(19211614.0)

@WarrenWeckesser WarrenWeckesser closed this as not planned Won't fix, can't repro, duplicate, stale Aug 23, 2024
@BrunoBelucci
Copy link
Author

I was expecting something about the machine precision, but I could not find an answer and was just verifying that the value was below the maximum value we could represent. Thank you very much for the clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
33 - Question Question about NumPy usage or development
Projects
None yet
Development

No branches or pull requests

3 participants
0