mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-30 00:31:14 +00:00
LibJS: Use more accurate number-to-string method in Number toPrecision
This commit is contained in:
parent
d56205f991
commit
5898db8c0f
Notes:
sideshowbarker
2024-07-17 07:38:17 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/5898db8c0f Pull-request: https://github.com/SerenityOS/serenity/pull/15921 Reviewed-by: https://github.com/linusg
|
@ -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<int>(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)) {
|
||||
|
|
|
@ -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]);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue