mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:17:44 +00:00
LibJS: Add extreme value tests for cos and sin
These sometimes produce different NaN patterns which can mess up the value encoding.
This commit is contained in:
parent
27abbfdf09
commit
da8715a07c
4 changed files with 35 additions and 3 deletions
|
@ -183,27 +183,53 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::trunc)
|
|||
// 21.3.2.30 Math.sin ( x ), https://tc39.es/ecma262/#sec-math.sin
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::sin)
|
||||
{
|
||||
// 1. Let n be ? ToNumber(x).
|
||||
auto number = TRY(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
// 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 is +∞𝔽 or n is -∞𝔽, return NaN.
|
||||
if (number.is_infinity())
|
||||
return js_nan();
|
||||
|
||||
// 4. Return an implementation-approximated Number value representing the result of the sine of ℝ(n).
|
||||
return Value(::sin(number.as_double()));
|
||||
}
|
||||
|
||||
// 21.3.2.12 Math.cos ( x ), https://tc39.es/ecma262/#sec-math.cos
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::cos)
|
||||
{
|
||||
// 1. Let n be ? ToNumber(x).
|
||||
auto number = TRY(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
|
||||
// 2. If n is NaN, n is +∞𝔽, or n is -∞𝔽, return NaN.
|
||||
if (number.is_nan() || number.is_infinity())
|
||||
return js_nan();
|
||||
|
||||
// 3. If n is +0𝔽 or n is -0𝔽, return 1𝔽.
|
||||
if (number.is_positive_zero() || number.is_negative_zero())
|
||||
return Value(1);
|
||||
|
||||
// 4. Return an implementation-approximated Number value representing the result of the cosine of ℝ(n).
|
||||
return Value(::cos(number.as_double()));
|
||||
}
|
||||
|
||||
// 21.3.2.33 Math.tan ( x ), https://tc39.es/ecma262/#sec-math.tan
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::tan)
|
||||
{
|
||||
// Let n be ? ToNumber(x).
|
||||
auto number = TRY(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
|
||||
// 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 is +∞𝔽, or n is -∞𝔽, return NaN.
|
||||
if (number.is_infinity())
|
||||
return js_nan();
|
||||
|
||||
// 4. Return an implementation-approximated Number value representing the result of the tangent of ℝ(n).
|
||||
return Value(::tan(number.as_double()));
|
||||
}
|
||||
|
||||
|
|
|
@ -11,4 +11,6 @@ test("basic functionality", () => {
|
|||
expect(Math.cos([1, 2, 3])).toBeNaN();
|
||||
expect(Math.cos({})).toBeNaN();
|
||||
expect(Math.cos("foo")).toBeNaN();
|
||||
expect(Math.cos(-Infinity)).toBeNaN();
|
||||
expect(Math.cos(Infinity)).toBeNaN();
|
||||
});
|
||||
|
|
|
@ -12,4 +12,6 @@ test("basic functionality", () => {
|
|||
expect(Math.sin([1, 2, 3])).toBeNaN();
|
||||
expect(Math.sin({})).toBeNaN();
|
||||
expect(Math.sin("foo")).toBeNaN();
|
||||
expect(Math.sin(Infinity)).toBeNaN();
|
||||
expect(Math.sin(-Infinity)).toBeNaN();
|
||||
});
|
||||
|
|
|
@ -11,4 +11,6 @@ test("basic functionality", () => {
|
|||
expect(Math.tan([1, 2, 3])).toBeNaN();
|
||||
expect(Math.tan({})).toBeNaN();
|
||||
expect(Math.tan("foo")).toBeNaN();
|
||||
expect(Math.tan(Infinity)).toBeNaN();
|
||||
expect(Math.tan(-Infinity)).toBeNaN();
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue