Skip to content

[Python][C++][Compute] logb(0, base) returns -inf for every base, including 0 < base < 1 and NaN #55636

Open

Description

@Chetansahney

Describe the bug, including details regarding any error messages, version, and platform.

Logb in cpp/src/arrow/compute/kernels/scalar_arithmetic.cc special-cases x == 0.0 and returns -inf for every base that is not zero or negative:

if (x == 0.0) {
  if (base == 0.0 || base < 0.0) {
    return std::numeric_limits<T>::quiet_NaN();
  } else {
    return -std::numeric_limits<T>::infinity();
  }
}

That is only right for base > 1. For 0 < base < 1, log(base) is negative, so log_base(0) is +inf. A NaN base should give NaN, and an infinite base gives -inf / inf, which is NaN.

import numpy as np
import pyarrow as pa
import pyarrow.compute as pc

base = pa.array([0.5, 0.1, np.inf, np.nan, 2.0])
x = pa.array([0.0] * len(base))
with np.errstate(divide="ignore", invalid="ignore"):
    expected = np.log(x.to_numpy()) / np.log(base.to_numpy())
print("logb    ", pc.logb(x, base).to_pylist())
print("expected", expected.tolist())

Output with pyarrow 26.0.1 (Windows, Python 3.12):

logb     [-inf, -inf, -inf, -inf, -inf]
expected [inf, inf, nan, nan, -inf]

Without the special case, std::log(x) / std::log(base) already gives the right values here, since std::log(0.0) is -inf. The x < 0.0 branch is also covered by std::log returning NaN. So one fix is to drop the x == 0.0 branch, or make it return -inf / std::log(base). logb_checked raises for x == 0 and is not affected.

Component(s)

C++, Python

Activity

  1. changed the title [-][C++][Compute] logb(0, base) returns -inf for every base, including 0 < base < 1 and NaN[/-] [+][Python][C++][Compute] logb(0, base) returns -inf for every base, including 0 < base < 1 and NaN[/+] on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions