1
Fork 0
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.acosh

This commit is contained in:
davidot 2022-11-28 11:58:21 +01:00 committed by Andreas Kling
parent 62a3de0c0a
commit cf4daa3941
2 changed files with 19 additions and 3 deletions

View file

@ -297,10 +297,23 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::acos)
// 21.3.2.3 Math.acosh ( x ), https://tc39.es/ecma262/#sec-math.acosh
JS_DEFINE_NATIVE_FUNCTION(MathObject::acosh)
{
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 or n is +∞𝔽, return n.
if (number.is_nan() || number.is_positive_infinity())
return number;
// 3. If n is 1𝔽, return +0𝔽.
if (number.as_double() == 1.0)
return Value(0.0);
// 4. If n < 1𝔽, return NaN.
if (number.as_double() < 1)
return js_nan();
return Value(::acosh(value));
// 5. Return an implementation-approximated Number value representing the result of the inverse hyperbolic cosine of (n).
return Value(::acosh(number.as_double()));
}
// 21.3.2.4 Math.asin ( x ), https://tc39.es/ecma262/#sec-math.asin

View file

@ -6,4 +6,7 @@ test("basic functionality", () => {
expect(Math.acosh(0.5)).toBeNaN();
expect(Math.acosh(1)).toBeCloseTo(0);
expect(Math.acosh(2)).toBeCloseTo(1.316957);
expect(Math.acosh(NaN)).toBe(NaN);
expect(Math.acosh(Number.POSITIVE_INFINITY)).toBe(Number.POSITIVE_INFINITY);
expect(Math.acosh(1.0)).toBe(0.0);
});