diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.cpp b/Userland/Libraries/LibJS/Runtime/MathObject.cpp index 139ef6a0d4..16489c7c19 100644 --- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp @@ -157,7 +157,8 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::max) auto cur = vm.argument(i).to_number(global_object); if (vm.exception()) return {}; - max = Value(cur.as_double() > max.as_double() ? cur : max); + if ((max.is_negative_zero() && cur.is_positive_zero()) || cur.as_double() > max.as_double()) + max = cur; } return max; } @@ -174,7 +175,8 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::min) auto cur = vm.argument(i).to_number(global_object); if (vm.exception()) return {}; - min = Value(cur.as_double() < min.as_double() ? cur : min); + if ((min.is_positive_zero() && cur.is_negative_zero()) || cur.as_double() < min.as_double()) + min = cur; } return min; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Math/Math.max.js b/Userland/Libraries/LibJS/Tests/builtins/Math/Math.max.js index f99a39cc9c..47c268721d 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Math/Math.max.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Math/Math.max.js @@ -5,6 +5,8 @@ test("basic functionality", () => { expect(Math.max(1)).toBe(1); expect(Math.max(2, 1)).toBe(2); expect(Math.max(1, 2, 3)).toBe(3); + expect(Math.max(-0, 0)).toBe(0); + expect(Math.max(0, -0)).toBe(0); expect(Math.max(NaN)).toBeNaN(); expect(Math.max("String", 1)).toBeNaN(); }); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Math/Math.min.js b/Userland/Libraries/LibJS/Tests/builtins/Math/Math.min.js index fedfcabe27..05d0807b47 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Math/Math.min.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Math/Math.min.js @@ -4,6 +4,8 @@ test("basic functionality", () => { expect(Math.min(1)).toBe(1); expect(Math.min(2, 1)).toBe(1); expect(Math.min(1, 2, 3)).toBe(1); + expect(Math.min(-0, 0)).toBe(-0); + expect(Math.min(0, -0)).toBe(-0); expect(Math.min(NaN)).toBeNaN(); expect(Math.min("String", 1)).toBeNaN(); });