From 818c9cd0b0195570e43a3681a726d372fa99de8e Mon Sep 17 00:00:00 2001 From: Robert Sipka Date: Thu, 16 Mar 2017 08:59:48 +0100 Subject: [PATCH] Fix issue #742 (#1650) It is a workaround fix. The problem comes from the inaccuracy of the double rounding. JerryScript-DCO-1.0-Signed-off-by: Robert Sipka rsipka.uszeged@partner.samsung.com --- .../ecma/builtin-objects/ecma-builtin-number-prototype.c | 9 +++++++-- tests/jerry/number-prototype-to-string.js | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-number-prototype.c b/jerry-core/ecma/builtin-objects/ecma-builtin-number-prototype.c index bc5a0726d..1b1851577 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-number-prototype.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-number-prototype.c @@ -253,10 +253,15 @@ ecma_builtin_number_prototype_object_to_string (ecma_value_t this_arg, /**< this scale = -scale; } - int buff_size; + int buff_size = 1; if (is_scale_negative) { - buff_size = (int) floor (log (this_arg_number) / log (radix)) + 1; + double counter = this_arg_number; + while (counter > radix) + { + counter /= radix; + buff_size++; + } } else { diff --git a/tests/jerry/number-prototype-to-string.js b/tests/jerry/number-prototype-to-string.js index 6a3a8a985..8322480a0 100644 --- a/tests/jerry/number-prototype-to-string.js +++ b/tests/jerry/number-prototype-to-string.js @@ -80,6 +80,12 @@ assert((123400).toString(34) === "34pe"); assert((123400).toString(35) === "2upp"); assert((123400).toString(36) === "2n7s"); +assert ((1152921504606846600).toString([1,2,3,4].slice(1,2)) === "111111111111111111111111111111111111111111111111111010000000"); +assert ((-1152921504606846600).toString(2) === "-111111111111111111111111111111111111111111111111111010000000"); + +assert ((0x100000000000061).toString(2) === "100000000000000000000000000000000000000000000000001100000") +assert ((-0x100000000000061).toString(16) === "-100000000000060"); + assert((123400).toString(new Number(16)) === "1e208"); var digit_chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',