From 7241ff3967223edd63ab6df96b095ed94f73fd60 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 22 Mar 2021 07:49:08 +0100 Subject: [PATCH] LibJS: *Actually* check for negative zero in JS::Value(double) As @nico pointed out, 0.0 == -0.0 in C++, even though they are not bitwise identical. Use the same trick as Value::is_negative_zero() to really check for it. This allows JS::Value(0.0) to correctly become an Int32-backed 0 value. --- Userland/Libraries/LibJS/Runtime/Value.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index fddd33cf81..81ea3298cb 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -109,7 +109,8 @@ public: explicit Value(double value) { - if (value >= NumericLimits::min() && value <= NumericLimits::max() && trunc(value) == value && value != -0.0) { + bool is_negative_zero = value == 0.0 && (1.0 / value == -INFINITY); + if (value >= NumericLimits::min() && value <= NumericLimits::max() && trunc(value) == value && !is_negative_zero) { m_type = Type::Int32; m_value.as_i32 = static_cast(value); } else {