From 1db16c3a1c5e62f439e5460cd22581700160d079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Csaba=20Osztrogon=C3=A1c?= Date: Mon, 16 Dec 2019 10:57:22 +0100 Subject: [PATCH] Fix Math.round(x) for big integer numbers (#3449) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Math.round(x) should be x it x is already integer. (ES5 15.8.2.15) The current implementation calculates with x +/- 0.5, which isn't representable numbers if x >= 2^52. A correct bug fix can be to follow the spec and simply return the number itself if it is already integer. JerryScript-DCO-1.0-Signed-off-by: Csaba Osztrogonác oszi@inf.u-szeged.hu --- .../ecma/builtin-objects/ecma-builtin-math.c | 3 ++- tests/jerry/math-round.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-math.c b/jerry-core/ecma/builtin-objects/ecma-builtin-math.c index b2e61ae64..5475b347d 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-math.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-math.c @@ -359,7 +359,8 @@ ecma_builtin_math_dispatch_routine (uint16_t builtin_routine_id, /**< built-in w { if (ecma_number_is_nan (x) || ecma_number_is_zero (x) - || ecma_number_is_infinity (x)) + || ecma_number_is_infinity (x) + || fmod (x, 1.0) == 0) { /* Do nothing. */ } diff --git a/tests/jerry/math-round.js b/tests/jerry/math-round.js index 36f1fdb33..fe1aa9c01 100644 --- a/tests/jerry/math-round.js +++ b/tests/jerry/math-round.js @@ -35,3 +35,17 @@ assert (Math['round'](-0.7) === -1.0); assert (Math['round'](-1.2) === -1.0); assert (Math['round'](-1.7) === -2.0); assert (Math['round'](-1.5) === -1.0); + +assert (Math['round'](1) === 1); +assert (Math['round'](-1) === -1); + +for (var n = 1; n <= 53; n++) +{ + var x = Math.pow(2, n) + assert (Math['round'](x - 1) === x - 1); + assert (Math['round'](x) === x); + assert (Math['round'](x + 1) === x + 1); + assert (Math['round'](-x - 1) === -x - 1); + assert (Math['round'](-x) === -x); + assert (Math['round'](-x + 1) === -x + 1); +}