From 5898db8c0f7cb3e2b42bd0d9a4abb37dc2c3303b Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 4 Nov 2022 11:16:58 -0400 Subject: [PATCH] LibJS: Use more accurate number-to-string method in Number toPrecision --- Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp | 6 +----- .../Tests/builtins/Number/Number.prototype.toPrecision.js | 3 +++ 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp b/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp index 1e0a8b5212..b4285a7e7b 100644 --- a/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp @@ -386,17 +386,13 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_precision) } // 10. Else, else { - // FIXME: The computations below fall apart for large values of 'p'. A double typically has 52 mantissa bits, which gives us - // up to 2^52 before loss of precision. However, the largest value of 'p' may be 100, resulting in numbers on the order - // of 10^100, thus we lose precision in these computations. - // a. Let e and n be integers such that 10^(p-1) ≤ n < 10^p and for which n × 10^(e-p+1) - x is as close to zero as possible. // If there are two such sets of e and n, pick the e and n for which n × 10^(e-p+1) is larger. exponent = static_cast(floor(log10(number))); number = round(number / pow(10, exponent - precision + 1)); // b. Let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes). - number_string = decimal_digits_to_string(number); + number_string = number_to_string(number, NumberToStringMode::WithoutExponent); // c. If e < -6 or e ≥ p, then if ((exponent < -6) || (exponent >= precision)) { diff --git a/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toPrecision.js b/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toPrecision.js index 6b3203576b..c2be588062 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toPrecision.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Number/Number.prototype.toPrecision.js @@ -87,6 +87,9 @@ describe("correct behavior", () => { [1, 4, "1.000"], [123, 4, "123.0"], [123.45, 4, "123.5"], + + // Disabled for now due to: https://github.com/SerenityOS/serenity/issues/15924 + // [3, 100, "3." + "0".repeat(99)], ].forEach(test => { expect(test[0].toPrecision(test[1])).toBe(test[2]); });