LibJS: Add spec comments and check for edge cases in Math.asin

This commit is contained in:
davidot 2022-11-28 12:00:09 +01:00 committed by Andreas Kling
parent cf4daa3941
commit 6bdf021b0c
Notes: sideshowbarker 2024-07-17 03:59:33 +09:00
2 changed files with 13 additions and 0 deletions

View file

@ -319,9 +319,18 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::acosh)
// 21.3.2.4 Math.asin ( x ), https://tc39.es/ecma262/#sec-math.asin
JS_DEFINE_NATIVE_FUNCTION(MathObject::asin)
{
// 1. Let n be ? ToNumber(x).
auto number = TRY(vm.argument(0).to_number(vm));
// 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 > 1𝔽 or n < -1𝔽, return NaN.
if (number.as_double() > 1 || number.as_double() < -1)
return js_nan();
// 4. Return an implementation-approximated Number value representing the result of the inverse sine of (n).
return Value(::asin(number.as_double()));
}

View file

@ -12,4 +12,8 @@ test("basic functionality", () => {
expect(Math.asin([1, 2, 3])).toBeNaN();
expect(Math.asin({})).toBeNaN();
expect(Math.asin("foo")).toBeNaN();
expect(Math.asin(NaN)).toBe(NaN);
expect(Math.asin(-0.0)).toBe(-0.0);
expect(Math.asin(1.1)).toBe(NaN);
expect(Math.asin(-1.1)).toBe(NaN);
});