summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-04-23 20:47:53 +0200
committerLinus Groh <mail@linusgroh.de>2021-04-23 20:51:48 +0200
commit0053816e9dd372664e8c58fde4855ba9f5e2b300 (patch)
tree5fbadb49ee48a697eeed86cc830fa293797070be
parent883e8683b2b67570672958adc626bed9e6176985 (diff)
downloadserenity-0053816e9dd372664e8c58fde4855ba9f5e2b300.zip
LibJS: Correctly handle mixing +0 and -0 in Math.{min,max}()
The native C++ < and > operators won't handle this correctly, so the result was different depending on the order of arguments. This is now fixed by explicitly checking for positive and negative zero values. Fixes #6589.
-rw-r--r--Userland/Libraries/LibJS/Runtime/MathObject.cpp6
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Math/Math.max.js2
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Math/Math.min.js2
3 files changed, 8 insertions, 2 deletions
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();
});