Skip to content

[SPARK-55557][SQL][4.0] Hyperbolic functions should not overflow with large inputs#54804

Closed
mgaido91 wants to merge 1 commit intoapache:branch-4.0from
mgaido91:SPARK-55557_4.0
Closed

[SPARK-55557][SQL][4.0] Hyperbolic functions should not overflow with large inputs#54804
mgaido91 wants to merge 1 commit intoapache:branch-4.0from
mgaido91:SPARK-55557_4.0

Conversation

@mgaido91
Copy link
Contributor

What changes were proposed in this pull request?

As mentioned in https://issues.apache.org/jira/browse/SPARK-55557, for large values (namely, larger than the square root of Double.MAX:_VALUE) the asinh and acosh functions return Infinity, due to overflow. This happens because of the x * x operation in the formula to compute them. However, for such large numbers, the expression can be simplified. Indeed, x +\- 1 for such large numbers has no effect, since the precision is not enough to be sensitive to this operaiton. Hence, log(x + sqrt(x * x +/- 1)) can be written as log(2x) = (log(2) + log(x)) in such cases.

Why are the changes needed?

Current code overflows and returns Infinity for asinh and acosh for large values.

Does this PR introduce any user-facing change?

Yes, SQL with asinh and acosh with large values do not return infinity anymore. For large negative values as input of acosh, NaN is returned.

How was this patch tested?

Added UTs.

Was this patch authored or co-authored using generative AI tooling?

No.

… large inputs

As mentioned in https://issues.apache.org/jira/browse/SPARK-55557, for large values (namely, larger than the square root of `Double.MAX:_VALUE`) the `asinh` and `acosh` functions return Infinity, due to overflow. This happens because of the `x * x` operation in the formula to compute them. However, for such large numbers, the expression can be simplified. Indeed, `x +\- 1` for such large numbers has no effect, since the precision is not enough to be sensitive to this operaiton. Hence, `log(x + sqrt(x * x +/- 1))` can be written as `log(2x) = (log(2) + log(x))` in such cases.

Current code overflows and returns Infinity for asinh and acosh for large values.

Yes, SQL with asinh and acosh with large values do not return infinity anymore. For large negative values as input of acosh, `NaN` is returned.

Added UTs.

No.
@mgaido91
Copy link
Contributor Author

cc @peter-toth

peter-toth pushed a commit that referenced this pull request Mar 16, 2026
… large inputs

### What changes were proposed in this pull request?

As mentioned in https://issues.apache.org/jira/browse/SPARK-55557, for large values (namely, larger than the square root of `Double.MAX:_VALUE`) the `asinh` and `acosh` functions return Infinity, due to overflow. This happens because of the `x * x` operation in the formula to compute them. However, for such large numbers, the expression can be simplified. Indeed, `x +\- 1` for such large numbers has no effect, since the precision is not enough to be sensitive to this operaiton. Hence, `log(x + sqrt(x * x +/- 1))` can be written as `log(2x) = (log(2) + log(x))` in such cases.

### Why are the changes needed?

Current code overflows and returns Infinity for asinh and acosh for large values.

### Does this PR introduce _any_ user-facing change?

Yes, SQL with asinh and acosh with large values do not return infinity anymore. For large negative values as input of acosh, `NaN` is returned.

### How was this patch tested?

Added UTs.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #54804 from mgaido91/SPARK-55557_4.0.

Authored-by: Marco Gaido <mgaido@fbk.eu>
Signed-off-by: Peter Toth <peter.toth@gmail.com>
@peter-toth peter-toth closed this Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants