Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions core/engine/src/builtins/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,21 @@ impl Math {
/// [spec]: https://tc39.es/ecma262/#sec-math.asinh
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/asinh
pub(crate) fn asinh(_: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
Ok(args
.get_or_undefined(0)
// 1. Let n be ? ToNumber(x).
.to_number(context)?
// 2. If n is NaN, n is +0𝔽, n is -0𝔽, n is +∞𝔽, or n is -∞𝔽, return n.
// 3. Return an implementation-approximated value representing the result of the inverse hyperbolic sine of ℝ(n).
.asinh()
.into())
// 1/√f64::EPSILON, as established by the Boost math library.
const ASINH_LARGE_INPUT_THRESHOLD: f64 = 67_108_864.0;

// 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 {
let r = n.recip();
return Ok(
(n.signum() * (n.abs().ln() + std::f64::consts::LN_2 + 0.25 * r * r)).into(),
);
}

// 2. If n is NaN, n is +0𝔽, n is -0𝔽, n is +∞𝔽, or n is -∞𝔽, return n.
// 3. Return an implementation-approximated value representing the result of the inverse hyperbolic sine of ℝ(n).
Ok(n.asinh().into())
}

/// Get the arctangent of a number.
Expand Down
3 changes: 3 additions & 0 deletions core/engine/src/builtins/math/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ fn asinh() {
run_test_actions([
TestAction::assert_eq("Math.asinh(1)", 0.881_373_587_019_543),
TestAction::assert_eq("Math.asinh(0)", 0.0),
TestAction::assert_eq("Math.asinh(1e308)", 709.889_355_822_726_f64),
TestAction::assert_eq("Math.asinh(-1e308)", -709.889_355_822_726_f64),
TestAction::assert_eq("Math.asinh(Number.MAX_VALUE)", 710.475_860_073_943_9_f64),
Comment thread
HiteshShonak marked this conversation as resolved.
]);
}

Expand Down