From f9b2d99463650db2ce847918a31163a9e39772e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=20=D0=91=D1=8B=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=BE=D0=B2?= Date: Wed, 18 Nov 2026 12:50:47 +0500 Subject: [PATCH] GH-55636: [C++] Fix logb results for zero input --- cpp/src/arrow/compute/kernels/scalar_arithmetic.cc | 6 +++--- cpp/src/arrow/compute/kernels/scalar_arithmetic_test.cc | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc index 53d4751b8c4f..a6f7217c4acf 100644 --- a/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc +++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic.cc @@ -494,11 +494,11 @@ struct Logb { static_assert(std::is_same::value, ""); static_assert(std::is_same::value, ""); if (x == 0.0) { - if (base == 0.0 || base < 0.0) { + if (base <= 0.0 || !std::isfinite(base)) { return std::numeric_limits::quiet_NaN(); - } else { - return -std::numeric_limits::infinity(); } + const auto infinity = std::numeric_limits::infinity(); + return base < 1.0 ? infinity : -infinity; } else if (x < 0.0) { return std::numeric_limits::quiet_NaN(); } diff --git a/cpp/src/arrow/compute/kernels/scalar_arithmetic_test.cc b/cpp/src/arrow/compute/kernels/scalar_arithmetic_test.cc index 68bb1cb22cba..ce8405536cba 100644 --- a/cpp/src/arrow/compute/kernels/scalar_arithmetic_test.cc +++ b/cpp/src/arrow/compute/kernels/scalar_arithmetic_test.cc @@ -3355,6 +3355,8 @@ TYPED_TEST(TestBinaryArithmeticFloating, Log) { "[NaN, NaN, -Inf, Inf]"); this->AssertBinop(Logb, "[-Inf, -1, 0, Inf]", this->MakeScalar(2.), "[NaN, NaN, -Inf, Inf]"); + this->AssertBinop(Logb, "[0, -0.0, 0, 0, 0, 0, 0]", "[0.5, 0.25, Inf, NaN, 1, 0, -2]", + "[Inf, Inf, NaN, NaN, -Inf, NaN, NaN]"); this->AssertBinop(Logb, "[-Inf, -1, 0, Inf]", "[2, 10, 0, 0]", "[NaN, NaN, NaN, NaN]"); this->AssertBinop(Logb, "[-Inf, -1, 0, Inf]", this->MakeScalar(0.), "[NaN, NaN, NaN, NaN]");