瀏覽代碼

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 +∞.
Idan Horowitz 4 年之前
父節點
當前提交
24ffe91b16
共有 1 個文件被更改,包括 5 次插入1 次删除
  1. 5 1
      Userland/Libraries/LibJS/Runtime/MathObject.cpp

+ 5 - 1
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)