-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-56089][SQL] Align asinh/acosh with fdlibm algorithm for cross-engine compatibility #54912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -418,25 +418,36 @@ case class Cosh(child: Expression) extends UnaryMathExpression(math.cosh, "COSH" | |
| since = "3.0.0", | ||
| group = "math_funcs") | ||
| case class Acosh(child: Expression) | ||
| extends UnaryMathExpression((x: Double) => x match { | ||
| // in case of large values, the square would lead to Infinity; also, - 1 would be ignored due | ||
| // to numeric precision. So log(x + sqrt(x * x - 1)) becomes log(2x) = log(2) + log(x) for | ||
| // positive values. | ||
| case x if x >= Math.sqrt(Double.MaxValue) => | ||
| StrictMath.log(2) + StrictMath.log(x) | ||
| case x if x < 1 => | ||
| extends UnaryMathExpression((x: Double) => { | ||
| // fdlibm e_acosh.c algorithm | ||
| if (x < 1.0) { | ||
| Double.NaN | ||
| case _ => StrictMath.log(x + math.sqrt(x * x - 1.0)) }, "ACOSH") { | ||
| } else if (x >= (1 << 28)) { | ||
| StrictMath.log(x) + StrictMath.log(2.0) | ||
| } else if (x == 1.0) { | ||
| 0.0 | ||
| } else if (x > 2.0) { | ||
| StrictMath.log(2.0 * x - 1.0 / (x + math.sqrt(x * x - 1.0))) | ||
| } else { | ||
| val t = x - 1.0 | ||
| StrictMath.log1p(t + math.sqrt(2.0 * t + t * t)) | ||
| } | ||
| }, "ACOSH") { | ||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| nullSafeCodeGen(ctx, ev, c => { | ||
| val sm = "java.lang.StrictMath" | ||
| s""" | ||
| |if ($c >= ${Math.sqrt(Double.MaxValue)}) { | ||
| | ${ev.value} = $sm.log($c) + $sm.log(2); | ||
| |} else if ($c < 1) { | ||
| |if ($c < 1.0) { | ||
| | ${ev.value} = java.lang.Double.NaN; | ||
| |} else if ($c >= ${1 << 28}.0) { | ||
| | ${ev.value} = $sm.log($c) + $sm.log(2.0); | ||
| |} else if ($c == 1.0) { | ||
| | ${ev.value} = 0.0; | ||
| |} else if ($c > 2.0) { | ||
| | ${ev.value} = $sm.log(2.0 * $c - 1.0 / ($c + java.lang.Math.sqrt($c * $c - 1.0))); | ||
| |} else { | ||
| | ${ev.value} = $sm.log($c + java.lang.Math.sqrt($c * $c - 1.0)); | ||
| | double t = $c - 1.0; | ||
| | ${ev.value} = $sm.log1p(t + java.lang.Math.sqrt(2.0 * t + t * t)); | ||
| |} | ||
| |""".stripMargin | ||
| }) | ||
|
|
@@ -865,20 +876,43 @@ case class Sinh(child: Expression) extends UnaryMathExpression(math.sinh, "SINH" | |
| since = "3.0.0", | ||
| group = "math_funcs") | ||
| case class Asinh(child: Expression) | ||
| extends UnaryMathExpression((x: Double) => x match { | ||
| // in case of large values, the square would lead to Infinity; also, + 1 would be ignored due | ||
| // to numeric precision. So log(x + sqrt(x * x + 1)) becomes log(2x) = log(2) + log(x) for | ||
| // positive values. Since the function is symmetric, for large values we can use | ||
| // signum(x) + log(2|x|) | ||
| case x if Math.abs(x) >= Math.sqrt(Double.MaxValue) - 1 => | ||
| Math.signum(x) * (StrictMath.log(2) + StrictMath.log(Math.abs(x))) | ||
| case _ => StrictMath.log(x + math.sqrt(x * x + 1.0)) }, "ASINH") { | ||
| extends UnaryMathExpression((x: Double) => { | ||
| // fdlibm s_asinh.c algorithm | ||
| val ax = Math.abs(x) | ||
| val w = if (ax.isInfinite || ax.isNaN) { | ||
| ax | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in fbmlib they have
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fdlibm, |
||
| } else if (ax < 1.0 / (1 << 28)) { | ||
| ax | ||
| } else if (ax > (1 << 28)) { | ||
| StrictMath.log(ax) + StrictMath.log(2.0) | ||
| } else if (ax > 2.0) { | ||
| StrictMath.log(2.0 * ax + 1.0 / (math.sqrt(x * x + 1.0) + ax)) | ||
| } else { | ||
| val t = x * x | ||
| StrictMath.log1p(ax + t / (1.0 + math.sqrt(1.0 + t))) | ||
| } | ||
| Math.copySign(w, x) | ||
| }, "ASINH") { | ||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| defineCodeGen(ctx, ev, c => { | ||
| nullSafeCodeGen(ctx, ev, c => { | ||
| val sm = "java.lang.StrictMath" | ||
| s"$sm.abs($c) >= ${Math.sqrt(Double.MaxValue) - 1} ? " + | ||
| s"$sm.signum($c) * ($sm.log($sm.abs($c)) + $sm.log(2)) :" + | ||
| s"$sm.log($c + java.lang.Math.sqrt($c * $c + 1.0))" | ||
| s""" | ||
| |double ax = java.lang.Math.abs($c); | ||
| |double w; | ||
| |if (java.lang.Double.isInfinite(ax) || java.lang.Double.isNaN(ax)) { | ||
| | w = ax; | ||
| |} else if (ax < ${1.0 / (1 << 28)}) { | ||
| | w = ax; | ||
| |} else if (ax > ${1 << 28}.0) { | ||
| | w = $sm.log(ax) + $sm.log(2.0); | ||
| |} else if (ax > 2.0) { | ||
| | w = $sm.log(2.0 * ax + 1.0 / (java.lang.Math.sqrt($c * $c + 1.0) + ax)); | ||
| |} else { | ||
| | double t = $c * $c; | ||
| | w = $sm.log1p(ax + t / (1.0 + java.lang.Math.sqrt(1.0 + t))); | ||
| |} | ||
| |${ev.value} = java.lang.Math.copySign(w, $c); | ||
| |""".stripMargin | ||
| }) | ||
| } | ||
| override protected def withNewChildInternal(newChild: Expression): Asinh = copy(child = newChild) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -577,7 +577,7 @@ SELECT asinh(double('1')) | |
| -- !query schema | ||
| struct<ASINH(1):double> | ||
| -- !query output | ||
| 0.8813735870195429 | ||
| 0.881373587019543 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checked with PostgreSQL. |
||
|
|
||
|
|
||
| -- !query | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, as a question, do you know why fdmlib here has
(x - x) / (x - x)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(x - x) / (x - x)is an fdlibm idiom to produce NaN while also raising the IEEE 754 "invalid operation" exception signal. In C, code can detect this via fetestexcept(FE_INVALID). The JVM has no IEEE 754 exception flag mechanism,(x - x) / (x - x)andDouble.NaNare functionally identical in Java/Scala. So we useDouble.NaNhere for clarity.