javascript 是如何准确地打印 0.1 的?

How does javascript print 0.1 with such accuracy?

我有 heard that javascript Numbers are IEEE 754 个浮点数,这就解释了为什么

> 0.3 - 0.2
0.09999999999999998

但我不明白

> 0.1
0.1

我认为 0.1 不能准确地存储为以 2 为底的浮点数,但它会立即打印出来,就像它一直是 0.1 一样。是什么赋予了?解释器在打印之前是否进行了舍入?

至少有 2 个版本的 IEEE 754 对我没有帮助:1984 edition and 2008. It sounds like the latter added full support for decimal arithmetic。好像我们没有那个。

JavaScript 使用 IEEE-754 双精度数("binary64" 正如 2008 规范所说的那样;也就是说,正如您所怀疑的那样,它是 base 2 版本,而不是 2008 base 10 版本).

尽管 0.1 无法用 binary64 完美表示,但您得到数字值 0.1 的字符串 "0.1" 的原因是——

TL;DR:该字符串不是数字的精确版本,它只是足以将其与相邻的不完全区分确切数字

—该规范定义了将数字转换为字符串的复杂规则,以解决精度不足的问题。它们包含在 §9.8.1 - ToString Applied to the Number Type:

  1. If m is NaN, return the String "NaN".
  2. If m is +0 or −0, return the String "0".
  3. If m is less than zero, return the String concatenation of the String "-" and ToString(−m).
  4. If m is infinity, return the String "Infinity".
  5. Otherwise, let n, k, and s be integers such that k ≥ 1, 10k−1 ≤ s < 10k, the Number value for s × 10n−k 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.
  6. If kn ≤ 21, return the String consisting of the k digits of the decimal representation of s (in order, with no leading zeroes), followed by n−k occurrences of the character ‘0’.
  7. If 0 < n ≤ 21, return the String consisting of the most significant n digits of the decimal representation of s, followed by a decimal point ‘.’, followed by the remaining k−n digits of the decimal representation of s.
  8. If −6 < n ≤ 0, return the String consisting of the character ‘0’, followed by a decimal point ‘.’, followed by −n occurrences of the character ‘0’, followed by the k digits of the decimal representation of s.
  9. Otherwise, if k = 1, return the String consisting of the single digit of s, followed by lowercase character ‘e’, followed by a plus sign ‘+’ or minus sign ‘−’ according to whether n−1 is positive or negative, followed by the decimal representation of the integer abs(n−1) (with no leading zeroes).
  10. Return the String consisting of the most significant digit of the decimal representation of s, followed by a decimal point ‘.’, followed by the remaining k−1 digits of the decimal representation of s, followed by the lowercase character ‘e’, followed by a plus sign ‘+’ or minus sign ‘−’ according to whether n−1 is positive or negative, followed by the decimal representation of the integer abs(n−1) (with no leading zeroes).

然后有以下注意事项;请按照 link 了解完整详情。注 3 可能是最相关的:

NOTE 3

Implementers of ECMAScript may find useful the paper and code written by David M. Gay for binary-to-decimal conversion of floating-point numbers:

Gay, David M. Correctly Rounded Binary-Decimal and Decimal-Binary Conversions. Numerical Analysis, Manuscript 90-10. AT&T Bell Laboratories (Murray Hill, New Jersey). November 30, 1990. Available as http://cm.bell-labs.com/cm/cs/doc/90/4-10.ps.gz. Associated code available as http://cm.bell-labs.com/netlib/fp/dtoa.c.gz and as http://cm.bell-labs.com/netlib/fp/g_fmt.c.gz and may also be found at the various netlib mirror sites.

对我来说,4-10.ps.gz 文件似乎已损坏(无法阅读第 6-8 页),但我在这里找到了一个 PDF:http://ampl.com/REFS/rounding.pdf (not as random a link as it may seem, apparently AMPL 是我的主要动机在论文中工作)。