diff options
author | davidot <davidot@serenityos.org> | 2022-11-28 12:08:01 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-11-28 13:10:21 +0100 |
commit | 8de8742b7c88a19584bb4560dd4383c3e2e2fbaf (patch) | |
tree | 61237d9b8298b2a9842a1c75c40cf13ac0b0b145 | |
parent | 4306462a95ee1851de67c8729114fe6a7d653740 (diff) | |
download | serenity-8de8742b7c88a19584bb4560dd4383c3e2e2fbaf.zip |
LibJS: Add spec comments and check for edge cases in Math.sinh
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/MathObject.cpp | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/Math/Math.sinh.js | 4 |
2 files changed, 11 insertions, 2 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.cpp b/Userland/Libraries/LibJS/Runtime/MathObject.cpp index 9baec14c3b..6fd42dc545 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp @@ -600,9 +600,14 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::log10) // 21.3.2.31 Math.sinh ( x ), https://tc39.es/ecma262/#sec-math.sinh JS_DEFINE_NATIVE_FUNCTION(MathObject::sinh) { + // 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 not finite or n is either +0𝔽 or -0𝔽, return n. + if (!number.is_finite_number() || number.is_positive_zero() || number.is_negative_zero()) + return number; + + // 3. Return an implementation-approximated Number value representing the result of the hyperbolic sine of ℝ(n). return Value(::sinh(number.as_double())); } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Math/Math.sinh.js b/Userland/Libraries/LibJS/Tests/builtins/Math/Math.sinh.js index 6ea3049c4c..1602382b73 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Math/Math.sinh.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Math/Math.sinh.js @@ -3,4 +3,8 @@ test("basic functionality", () => { expect(Math.sinh(0)).toBe(0); expect(Math.sinh(1)).toBeCloseTo(1.1752011936438014); + expect(Math.sinh(NaN)).toBe(NaN); + expect(Math.sinh(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY); + expect(Math.sinh(Number.NEGATIVE_INFINITY)).toBe(Number.NEGATIVE_INFINITY); + expect(Math.sinh(-0.0)).toBe(-0.0); }); |