mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 18:57:35 +00:00
LibJS: Use more accurate number-to-string method in Number toPrecision
This commit is contained in:
parent
d56205f991
commit
5898db8c0f
2 changed files with 4 additions and 5 deletions
|
@ -386,17 +386,13 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_precision)
|
||||||
}
|
}
|
||||||
// 10. Else,
|
// 10. Else,
|
||||||
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.
|
// 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.
|
// 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)));
|
exponent = static_cast<int>(floor(log10(number)));
|
||||||
number = round(number / pow(10, exponent - precision + 1));
|
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).
|
// 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
|
// c. If e < -6 or e ≥ p, then
|
||||||
if ((exponent < -6) || (exponent >= precision)) {
|
if ((exponent < -6) || (exponent >= precision)) {
|
||||||
|
|
|
@ -87,6 +87,9 @@ describe("correct behavior", () => {
|
||||||
[1, 4, "1.000"],
|
[1, 4, "1.000"],
|
||||||
[123, 4, "123.0"],
|
[123, 4, "123.0"],
|
||||||
[123.45, 4, "123.5"],
|
[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 => {
|
].forEach(test => {
|
||||||
expect(test[0].toPrecision(test[1])).toBe(test[2]);
|
expect(test[0].toPrecision(test[1])).toBe(test[2]);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue