LibJS: Handle negative zero and negative infinity in Math.abs()

As required by the specification:
3. If n is -0, return +0.
4. If n is -∞, return +∞.
This commit is contained in:
Idan Horowitz 2021-06-05 01:55:13 +03:00 committed by Linus Groh
parent 9d2e90d569
commit 24ffe91b16
Notes: sideshowbarker 2024-07-18 16:50:53 +09:00

View file

@ -82,7 +82,11 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::abs)
return {};
if (number.is_nan())
return js_nan();
return Value(number.as_double() >= 0 ? number.as_double() : -number.as_double());
if (number.is_negative_zero())
return Value(0);
if (number.is_negative_infinity())
return js_infinity();
return Value(number.as_double() < 0 ? -number.as_double() : number.as_double());
}
JS_DEFINE_NATIVE_FUNCTION(MathObject::random)