javascript - javascript 如何以如此准确的方式打印 0.1?

标签 javascript floating-point ieee-754

我已经 heard javascript 数字是 IEEE 754 float ,这解释了原因

> 0.3 - 0.2
0.09999999999999998

但是我不明白

> 0.1
0.1

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

至少有 2 个版本的 IEEE 754 对我没有帮助:1984版本和2008 .听起来后者添加了full support for decimal arithmetic .好像我们没有那个。

最佳答案

JavaScript 使用 IEEE-754 double (2008 年规范中的“binary64”;也就是说,正如您所怀疑的那样,它是 base 2 版本,而不是 2008 base 10 版本)。

你得到数字值 0.1 的字符串 "0.1" 的原因,即使 0.1 不能用 binary64 完美表示,那是——

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).

然后就是下面的注释;点击链接了解全部详情。注 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 (链接并不像看起来那么随意,显然 AMPL 是本文工作的主要动机)。

关于javascript - javascript 如何以如此准确的方式打印 0.1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28494758/

相关文章:

C# - 递增 double 值 (1.212E+25)

c - 如何在c89中定义浮点 double 常量

javascript - AngularJS 表单提交未触发

javascript - 哪个Dom操作比较慢?

javascript - 如何在文档的特定位置使用纯 javascript 在 HTML 文档中添加元素

float 与 C 中的值的比较

objective-c - CS193P - 向 iOS 计算器添加浮点按钮

javascript - 什么位模式代表 double 浮点的最大值?

python - 如何在 Python 中测试负零?

javascript - 你将如何在 JavaScript/HTML 中实现自动大写