Skip to content

Commit

Permalink
Fix power function Inf handling (#11295)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #11295

Fix bug introduced in D64145275 that returned NaN whenever the expononent was infinity.

Reviewed By: spershin

Differential Revision: D64573431

fbshipit-source-id: 7346b79732e5850579e5f048d0c85c6772a8cd42
  • Loading branch information
Daniel Hunte authored and facebook-github-bot committed Oct 18, 2024
1 parent d42ec70 commit 0c7fbff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 2 additions & 3 deletions velox/functions/prestosql/Arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ struct PowerFunction {
template <typename TInput>
FOLLY_ALWAYS_INLINE void
call(double& result, const TInput& a, const TInput& b) {
result = (std::isnan(a) && b != 0) || std::isnan(b) || std::isinf(b)
? std::numeric_limits<double>::quiet_NaN()
: pow(a, b);
result =
std::isnan(b) ? std::numeric_limits<double>::quiet_NaN() : pow(a, b);
}
};

Expand Down
15 changes: 12 additions & 3 deletions velox/functions/prestosql/tests/ArithmeticTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,18 @@ TEST_F(ArithmeticTest, powerNan) {
}

TEST_F(ArithmeticTest, powerInf) {
std::vector<double> baseDouble = {1, kInf, kInf, kInf};
std::vector<double> exponentDouble = {kInf, 1, kNan, 0};
std::vector<double> expectedDouble = {kNan, kInf, kNan, 1};
// base | exp | result
//----------------------
// 1 Inf 1
// 1 -Inf 1
// 2 Inf Inf
// 2 -Inf 0
// Inf 1 Inf
// Inf 0 1
// Inf NaN NaN
std::vector<double> baseDouble = {1, 1, 2, 2, kInf, kInf, kInf};
std::vector<double> exponentDouble = {kInf, -kInf, kInf, -kInf, 1, 0, kNan};
std::vector<double> expectedDouble = {1, 1, kInf, 0, kInf, 1, kNan};

// Check using function name and alias.
assertExpression<double>(
Expand Down

0 comments on commit 0c7fbff

Please sign in to comment.