fix(math): correct Math.asinh for large finite inputs#5240
fix(math): correct Math.asinh for large finite inputs#5240HiteshShonak wants to merge 2 commits intoboa-dev:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes Math.asinh returning Infinity for very large finite inputs by introducing a large-input fallback formula consistent with common libm/Boost behavior, and adds regression tests to prevent recurrence.
Changes:
- Add a large-input threshold in
Math.asinhto computesign(x) * (ln(|x|) + LN_2)for large finitex, avoiding overflow inf64::asinh(). - Add regression tests for
Math.asinh(1e308),Math.asinh(-1e308), andMath.asinh(Number.MAX_VALUE).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| core/engine/src/builtins/math/mod.rs | Adds a large-input branch to keep Math.asinh finite for extremely large finite numbers. |
| core/engine/src/builtins/math/tests.rs | Adds regression coverage for large finite inputs that previously overflowed to infinities. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Test262 conformance changes
Tested main commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5240 +/- ##
===========================================
+ Coverage 47.24% 59.83% +12.58%
===========================================
Files 476 582 +106
Lines 46892 63459 +16567
===========================================
+ Hits 22154 37968 +15814
- Misses 24738 25491 +753 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
core/engine/src/builtins/math/mod.rs
Outdated
| // 1. Let n be ? ToNumber(x). | ||
| let n = args.get_or_undefined(0).to_number(context)?; | ||
| if n.is_finite() && n.abs() > ASINH_LARGE_INPUT_THRESHOLD { | ||
| return Ok((n.signum() * (n.abs().ln() + std::f64::consts::LN_2)).into()); |
There was a problem hiding this comment.
Do we want to use the better approximation here? (ln |2x| + 1/(4x^2))
We can decompose it to avoid overflowing into infinity:
ln 2x + 1/(4x^2) = ln x + ln 2 + (1/2 * 1/x)(1/2 * 1/x)
There was a problem hiding this comment.
could you check this:
i have made updates as you suggested
#5241
There was a problem hiding this comment.
@Veercodeprog don't try to take over already assigned work, please.
There was a problem hiding this comment.
@jedel1043 updated to use the more accurate approximation ln(|x|) + ln(2) + 1/(4x²) as suggested, thanks
This Pull Request fixes/closes #5239.
It changes the following:
1/√f64::EPSILON(67_108_864.0), fall back ton.signum() * (n.abs().ln() + LN_2)instead of callingf64::asinh()directly, avoiding an internal overflow that producesInfinity. threshold follows the Boost math library convention.Math.asinh(1e308),Math.asinh(-1e308), andMath.asinh(Number.MAX_VALUE).Testing:
cargo test -p boa_engine math -- --nocaptureSpec reference: https://tc39.es/ecma262/#sec-math.asinh