Skip to content

BUG: np.log(arr, where=(arr > 0.0)) ignoring where optional parameter. #29561

@J0wsper

Description

@J0wsper

Describe the issue:

When using the np.log function and setting the where optional parameter to only include values greater than 0.0 on a 1-dimensional array, the function can simply ignore it. This can either result in less-than-zero values being squashed to 0.0 or having their sign flipped. Interestingly, the presence of print statements seem to influence this behavior. Additionally, printing unrelated variables can influence this behavior.

Reproduce the code example:

import numpy as np

# Flipping sign
vals = np.empty((5,))
vals.fill(-np.finfo("float32").max)
print(vals)
log_vals = np.log(vals, where=(vals > 0.0))
print(log_vals)
# Expected output: [-3.40282347e+38 -3.40282347e+38 -3.40282347e+38 -3.40282347e+38 -3.40282347e+38]
# Actual output: [3.40282347e+38 3.40282347e+38 3.40282347e+38 3.40282347e+38 3.40282347e+38]

# Squashing to 0.0.
# NOTE: This will only squash to 0 if you run this script independently of the previous script.
vals2 = np.empty((5,))
vals2.fill(-np.finfo("float32").max)
log_vals2 = np.log(vals2, where=(vals2 > 0.0))
print(log_vals2)
# Expected output: [-3.40282347e+38 -3.40282347e+38 -3.40282347e+38 -3.40282347e+38 -3.40282347e+38]
# Actual output: non-deterministic small values near 0.0

Error message:

Python and NumPy Versions:

python: 3.12.11
numpy: 2.2.6

Runtime Environment:

[{'numpy_version': '2.2.6',
  'python': '3.12.11 (main, Jul 11 2025, 22:43:48) [Clang 20.1.4 ]',
  'uname': uname_result(system='Linux', node='spacetee', release='6.15.6-1-default', version='#1 SMP PREEMPT_DYNAMIC Fri Jul 11 05:06:35 UTC 2025 (a69e09e)', 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': 'Haswell',
  'filepath': '/home/brams/Documents/oppie/.venv/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so',
  'internal_api': 'openblas',
  'num_threads': 4,
  'prefix': 'libscipy_openblas',
  'threading_layer': 'pthreads',
  'user_api': 'blas',
  'version': '0.3.29'}]

Context for the issue:

Being able to ignore less-than-zero values in np.log with where should be essential to avoid runtime errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions