From 24ffe91b16cf66da4febd4c909bb0e542b11806a Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sat, 5 Jun 2021 01:55:13 +0300 Subject: [PATCH] LibJS: Handle negative zero and negative infinity in Math.abs() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As required by the specification: 3. If n is -0, return +0. 4. If n is -∞, return +∞. --- Userland/Libraries/LibJS/Runtime/MathObject.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.cpp b/Userland/Libraries/LibJS/Runtime/MathObject.cpp index 907f8f40488..527f3af4d43 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp @@ -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)