Skip to content

Commit

Permalink
Add expm1 Spark function (facebookincubator#9667)
Browse files Browse the repository at this point in the history
Summary:
Use std::expm1 for the implementation.

Fixes facebookincubator#9716

Pull Request resolved: facebookincubator#9667

Reviewed By: Yuhta

Differential Revision: D57584204

Pulled By: pedroerp

fbshipit-source-id: 2b2983c22f8c9fd6f3d695d69a81f19cffed0d86
  • Loading branch information
Donvi authored and facebook-github-bot committed May 29, 2024
1 parent 38d6563 commit b7bd2a4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions velox/docs/functions/spark/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ Mathematical Functions
Returns Euler's number raised to the power of ``x``.

.. spark:function:: expm1(x) -> double
Returns Euler's number raised to the power of ``x``, minus 1, which is ``exp(x) - 1`` in math. This function expm1(x) is more accurate than ``exp(x) - 1``, when ``x`` is close to zero.
If the argument is NaN, the result is NaN.
If the argument is positive infinity, then the result is positive infinity.
If the argument is negative infinity, then the result is -1.0.
If the argument is zero, then the result is a zero with the same sign as the argument.

.. spark:function:: floor(x) -> [same as x]
Returns ``x`` rounded down to the nearest integer.
Expand Down
11 changes: 11 additions & 0 deletions velox/functions/sparksql/Arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ struct Log1pFunction {
}
};

template <typename T>
struct Expm1Function {
FOLLY_ALWAYS_INLINE void call(double& result, double a) {
// The std::expm1 is more accurate than the expression std::exp(num) - 1.0
// if num is close to zero. This matches Spark's implementation that uses
// java.lang.StrictMath.expm1 as below. Ref:
// https://docs.oracle.com/javase/8/docs/api/java/lang/StrictMath.html#expm1-double-.
result = std::expm1(a);
}
};

template <typename T>
struct CotFunction {
FOLLY_ALWAYS_INLINE void call(double& result, double a) {
Expand Down
1 change: 1 addition & 0 deletions velox/functions/sparksql/RegisterArithmetic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ void registerArithmeticFunctions(const std::string& prefix) {
registerFunction<ToHexVarbinaryFunction, Varchar, Varbinary>(
{prefix + "hex"});
registerFunction<ExpFunction, double, double>({prefix + "exp"});
registerFunction<Expm1Function, double, double>({prefix + "expm1"});
registerBinaryIntegral<PModIntFunction>({prefix + "pmod"});
registerBinaryFloatingPoint<PModFloatFunction>({prefix + "pmod"});
registerFunction<PowerFunction, double, double, double>({prefix + "power"});
Expand Down
24 changes: 24 additions & 0 deletions velox/functions/sparksql/tests/ArithmeticTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,30 @@ TEST_F(ArithmeticTest, log1p) {
EXPECT_TRUE(std::isnan(log1p(kNan).value_or(0)));
}

TEST_F(ArithmeticTest, expm1) {
static const auto expm1 = [&](std::optional<double> a) {
return evaluateOnce<double>("expm1(c0)", a);
};

const double kE = std::exp(1);

// If the argument is NaN, the result is NaN.
// If the argument is positive infinity, then the result is positive infinity.
// If the argument is negative infinity, then the result is -1.0.
// If the argument is zero, then the result is a zero with the same sign as
// the argument.
EXPECT_TRUE(std::isnan(expm1(kNan).value_or(0)));
EXPECT_EQ(expm1(kInf), kInf);
EXPECT_EQ(expm1(-kInf), -1);
EXPECT_EQ(expm1(0), 0);
EXPECT_EQ(expm1(1), kE - 1);
// As this is only for high accuracy of little number, we use a little number
// 1e-12 which can give the difference. If you use std::exp(x) - 1, the value
// may be 1.000009e-12, while the true value should be
// below 1.000000000000005e-12.
EXPECT_LT(expm1(1e-12), 1.00009e-12);
}

class BinTest : public SparkFunctionBaseTest {
protected:
std::optional<std::string> bin(std::optional<std::int64_t> arg) {
Expand Down

0 comments on commit b7bd2a4

Please sign in to comment.