Skip to content

BUG: numpy logarithm behavior does not match CPython 3.14 cmath.log behavior #30470

@cpcloud

Description

@cpcloud

Describe the issue:

In CPython 3.14, the behavior of logarithms of complex numbers changed for some edge case inputs in python/cpython@2cb84b1, causing behavior to differ beyond floating point precision differences between

import cmath

cmath.log(x, base)

and

import numpy as np

np.log(x) / np.log(base)

where

x = 1j
base = -cmath.inf + cmath.nanj

Reproduce the code example:

import cmath
import numpy as np
import warnings


def python(x, base):
    return cmath.log(x, base)


def numpy(x, base):
    return np.log(x) / np.log(base)


def numpy_scimath(x, base):
    return np.emath.logn(base, x)


def main():
    x = 1j
    base = -cmath.inf + cmath.nanj
    funcs = python, numpy, numpy_scimath
    names = list(map(lambda x: x.__name__, funcs))
    padding = max(map(len, names))

    for name, func in zip(names, funcs):
        print(f"{name}".ljust(padding) + ":", func(x, base))


if __name__ == "__main__":
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=RuntimeWarning)
        main()

In 3.13, this produces:

python       : (nan+nanj)
numpy        : (nan+nanj)
numpy_scimath: (nan+nanj)

In 3.14, this produces:

python       : 0j
numpy        : (nan+nanj)
numpy_scimath: (nan+nanj)

Error message:

N/A, there's no error here.

Python and NumPy Versions:

Python versions are 3.13 and 3.14

numpy version is 2.3.4 in both cases

Runtime Environment:

[{'numpy_version': '2.3.4',
  'python': '3.13.9 (main, Oct 14 2025, 13:52:31) [GCC 14.3.0]',
  'uname': uname_result(system='Linux', 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': '/nix/store/xm08aqdd7pxcdhm0ak6aqb1v7hw5q6ri-gcc-14.3.0-lib/lib/libgomp.so.1.0.0',
  'internal_api': 'openmp',
  'num_threads': 32,
  'prefix': 'libgomp',
  'user_api': 'openmp',
  'version': None}]

Context for the issue:

I'm not sure about how to prioritize this, but the change affects numba and numba-cuda, since they depend on numpy's behavior for these functions -- apparently even in the stdlib case, since neither of them calls the stdlib function directly when a user uses these functions.

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