From fb4b18a5154e9566b54cb0c4b1cdf58a31eca08a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Csaba=20Osztrogon=C3=A1c?= Date: Thu, 12 Dec 2019 10:57:44 +0100 Subject: [PATCH] Fix Math.trunc(x) if -1 < x < 0 (#3445) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Math.trunc(x) should be -0.0 if -1 < x < 0 (ES2015 20.2.2.35). The problem was that -0 isn't -0.0, but +0.0 in C. There were a test case for it, but it was incorrect, because +0.0 === -0.0 in JS. JerryScript-DCO-1.0-Signed-off-by: Csaba Osztrogonác oszi@inf.u-szeged.hu --- jerry-core/ecma/builtin-objects/ecma-builtin-math.c | 4 ++-- tests/jerry/es2015/math-trunc.js | 13 +++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-math.c b/jerry-core/ecma/builtin-objects/ecma-builtin-math.c index e05a2346d..b2e61ae64 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-math.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-math.c @@ -177,12 +177,12 @@ ecma_builtin_math_object_trunc (ecma_number_t arg) if ((arg > 0) && (arg < 1)) { - return (ecma_number_t) 0; + return (ecma_number_t) 0.0; } if ((arg < 0) && (arg > -1)) { - return (ecma_number_t) -0; + return (ecma_number_t) -0.0; } return (ecma_number_t) arg - fmod (arg, 1); diff --git a/tests/jerry/es2015/math-trunc.js b/tests/jerry/es2015/math-trunc.js index 958e83dc9..7a3796f75 100644 --- a/tests/jerry/es2015/math-trunc.js +++ b/tests/jerry/es2015/math-trunc.js @@ -18,13 +18,18 @@ var m_zero = -p_zero; var p_inf = Infinity; var m_inf = -p_inf; +function isSameZero (x, y) +{ + return x === 0 && (1 / x) === (1 / y); +} + assert (isNaN(Math['trunc'](NaN))); -assert (Math['trunc'](p_zero) === p_zero); -assert (Math['trunc'](m_zero) === m_zero); +assert (isSameZero (Math['trunc'](p_zero), p_zero)); +assert (isSameZero (Math['trunc'](m_zero), m_zero)); assert (Math['trunc'](p_inf) === p_inf); assert (Math['trunc'](m_inf) === m_inf); -assert (Math['trunc'](0.5) === p_zero); -assert (Math['trunc'](-0.5) === m_zero); +assert (isSameZero (Math['trunc'](0.5), p_zero)); +assert (isSameZero (Math['trunc'](-0.5), m_zero)); assert (Math['trunc'](1.2) === 1); assert (Math['trunc'](-1.5) === -1); assert (Math['trunc'](65.7) === 65);