mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:17:34 +00:00
LibJS: Add spec comments and check for edge cases in Math.atanh
This commit is contained in:
parent
c565cbd30c
commit
d4e5644df8
2 changed files with 19 additions and 3 deletions
|
@ -390,10 +390,23 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::atanh)
|
||||||
// 21.3.2.21 Math.log1p ( x ), https://tc39.es/ecma262/#sec-math.log1p
|
// 21.3.2.21 Math.log1p ( x ), https://tc39.es/ecma262/#sec-math.log1p
|
||||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::log1p)
|
JS_DEFINE_NATIVE_FUNCTION(MathObject::log1p)
|
||||||
{
|
{
|
||||||
auto value = TRY(vm.argument(0).to_number(vm)).as_double();
|
// 1. Let n be ? ToNumber(x).
|
||||||
if (value < -1)
|
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 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
|
// 21.3.2.9 Math.cbrt ( x ), https://tc39.es/ecma262/#sec-math.cbrt
|
||||||
|
|
|
@ -5,4 +5,7 @@ test("basic functionality", () => {
|
||||||
expect(Math.log1p(-1)).toBe(-Infinity);
|
expect(Math.log1p(-1)).toBe(-Infinity);
|
||||||
expect(Math.log1p(0)).toBe(0);
|
expect(Math.log1p(0)).toBe(0);
|
||||||
expect(Math.log1p(1)).toBeCloseTo(0.693147);
|
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);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue