mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-23 08:00:20 +00:00
LibJS: Add spec comments and check for edge cases in Math.tanh
This commit is contained in:
parent
8de8742b7c
commit
bf1b2d63c6
Notes:
sideshowbarker
2024-07-17 03:59:03 +09:00
Author: https://github.com/davidot Commit: https://github.com/SerenityOS/serenity/commit/bf1b2d63c6 Pull-request: https://github.com/SerenityOS/serenity/pull/16234
2 changed files with 13 additions and 2 deletions
|
@ -636,13 +636,22 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::cosh)
|
|||
// 21.3.2.34 Math.tanh ( x ), https://tc39.es/ecma262/#sec-math.tanh
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::tanh)
|
||||
{
|
||||
// 1. Let n be ? ToNumber(x).
|
||||
auto number = TRY(vm.argument(0).to_number(vm));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
|
||||
// 2. If n is NaN, n is +0𝔽, or n is -0𝔽, return n.
|
||||
if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
|
||||
return number;
|
||||
|
||||
// 3. If n is +∞𝔽, return 1𝔽.
|
||||
if (number.is_positive_infinity())
|
||||
return Value(1);
|
||||
|
||||
// 4. If n is -∞𝔽, return -1𝔽.
|
||||
if (number.is_negative_infinity())
|
||||
return Value(-1);
|
||||
|
||||
// 5. Return an implementation-approximated Number value representing the result of the hyperbolic tangent of ℝ(n).
|
||||
return Value(::tanh(number.as_double()));
|
||||
}
|
||||
|
||||
|
|
|
@ -5,4 +5,6 @@ test("basic functionality", () => {
|
|||
expect(Math.tanh(Infinity)).toBe(1);
|
||||
expect(Math.tanh(-Infinity)).toBe(-1);
|
||||
expect(Math.tanh(1)).toBeCloseTo(0.7615941559557649);
|
||||
expect(Math.tanh(NaN)).toBe(NaN);
|
||||
expect(Math.tanh(-0.0)).toBe(-0.0);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue