diff options
author | davidot <davidot@serenityos.org> | 2022-11-28 12:03:41 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-11-28 13:10:21 +0100 |
commit | d4e5644df88e7e5a9e27d8574a0a84c3c04736e1 (patch) | |
tree | 09604c1483157867e184f1a83abf1c4054b43b49 | |
parent | c565cbd30c3bcec4c74427b3f7f5a94860b75957 (diff) | |
download | serenity-d4e5644df88e7e5a9e27d8574a0a84c3c04736e1.zip |
LibJS: Add spec comments and check for edge cases in Math.atanh
-rw-r--r-- | Userland/Libraries/LibJS/Runtime/MathObject.cpp | 19 | ||||
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/Math/Math.log1p.js | 3 |
2 files changed, 19 insertions, 3 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.cpp b/Userland/Libraries/LibJS/Runtime/MathObject.cpp index bb2da04d66..a45e79128f 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp @@ -390,10 +390,23 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::atanh) // 21.3.2.21 Math.log1p ( x ), https://tc39.es/ecma262/#sec-math.log1p JS_DEFINE_NATIVE_FUNCTION(MathObject::log1p) { - auto value = TRY(vm.argument(0).to_number(vm)).as_double(); - if (value < -1) + // 1. Let n be ? ToNumber(x). + auto number = TRY(vm.argument(0).to_number(vm)); + + // 2. If n is NaN, n is +0𝔽, n is -0𝔽, or n is +∞𝔽, return n. + if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero() || number.is_positive_infinity()) + return number; + + // 3. If n is -1𝔽, return -∞𝔽. + if (number.as_double() == -1.) + return js_negative_infinity(); + + // 4. If n < -1𝔽, return NaN. + if (number.as_double() < -1.) return js_nan(); - return Value(::log1p(value)); + + // 5. Return an implementation-approximated Number value representing the result of the natural logarithm of 1 + ℝ(n). + return Value(::log1p(number.as_double())); } // 21.3.2.9 Math.cbrt ( x ), https://tc39.es/ecma262/#sec-math.cbrt diff --git a/Userland/Libraries/LibJS/Tests/builtins/Math/Math.log1p.js b/Userland/Libraries/LibJS/Tests/builtins/Math/Math.log1p.js index 5d35c1d03e..59ab543a66 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Math/Math.log1p.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Math/Math.log1p.js @@ -5,4 +5,7 @@ test("basic functionality", () => { expect(Math.log1p(-1)).toBe(-Infinity); expect(Math.log1p(0)).toBe(0); expect(Math.log1p(1)).toBeCloseTo(0.693147); + expect(Math.log1p(NaN)).toBe(NaN); + expect(Math.log1p(-0.0)).toBe(-0.0); + expect(Math.log1p(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY); }); |