ToString 抽象操作:未唯一确定的最低有效位

ToString abstract operation: least significant digit not uniquely determined

我正在阅读 the ECMAScript abstract operation ToString。第五步(m是我们要转成字符串的数字):

  1. Otherwise, let n, k, and s be integers such that k ≥ 1, 10^(k−1) ≤ s < 10^k, the Number value for s × 10^(nk) is m, and k is as small as possible. Note that k is the number of digits in the decimal representation of s, that s is not divisible by 10, and that the least significant digit of s is not necessarily uniquely determined by these criteria.

我无法弄清楚在什么情况下 s 的最低有效位不会被唯一确定。有例子吗?

答案是 - 与浮点数学一样 - 在可用精度的边缘舍入。

让我们取 s = 7011750883285835k=16 和一些 n(比方说 n=0)。现在确定 m,我们将得到浮点数 0x3FE67006BD248487(大约 0.70117508832858355…)。但是,我们也可以选择 s = 7011750883285836,它也等于 m

重点是,如果我们将双精度转换为十进制表示形式,我们就会得到 0.701175088328583551167128007364。这比必要的要长得多(并且意味着比可用的精度更高),因此 ToString 算法指定用最少的有效数字(“k 尽可能小 ") 仍然解析为我们想要的数字 m。有时,我们可以向上舍入或向下舍入来得到它,这两种方式都是允许的。