简体   繁体   English

另一个浮点问题

[英]Another floating point question

I have read most of the posts on here regarding floating point, and I understand the basic underlying issue that using IEEE 754 (and just by the nature of storing numbers in binary) certain fractions cannot be represented. 我已经阅读了关于浮点的大部分帖子,我理解使用IEEE 754的基本问题(并且仅仅通过以二进制形式存储数字的性质)某些分数无法表示。 I am trying to figure out the following: If both Python and JavaScript use the IEEE 754 standard, why is it that executing the following in Python 我试图找出以下内容:如果Python和JavaScript都使用IEEE 754标准,为什么在Python中执行以下操作?

.1 + .1 .1 + .1

Results in 0.20000000000000001 (which is to be expected) 结果在0.20000000000000001(这是预期的)

Where as in Javascript (in at least Chrome and Firefox) the answer is .2 在Javascript(至少在Chrome和Firefox中)的答案是.2

However performing 然而表演

.1 + .2 .1 + .2

In both languages results in 0.30000000000000004 在两种语言中结果为0.30000000000000004

In addition, executing var a = 0.3; 另外,执行var a = 0.3; in JavaScript and printing a results in 0.3 在JavaScript中打印结果为0.3

Where as doing a = 0.3 in Python results in 0.29999999999999999 在Python中执行a = 0.3的结果为0.29999999999999999

I would like to understand the reason for this difference in behavior. 我想了解这种行为差异的原因。

In addition, many of the posts on OS link to a JavaScript port of Java's BigDecimal, but the link is dead. 此外,OS上的许多帖子都链接到Java的BigDecimal的JavaScript端口,但链接已经死了。 Does anyone have a copy? 有人有副本吗?

doing a = 0.3 in Python results in 0.29999999999999999 在Python中执行a = 0.3会导致0.29999999999999999

Not quite -- watch: 不完全 - 看:

>>> a = 0.3
>>> print a
0.3
>>> a
0.29999999999999999

As you see, print ing a does show 0.3 -- because by default print rounds to 6 or 7 decimal digits, while typing an expression (here a is a single-variable expression) at the prompt shows the result with over twice as many digits (thus revealing floating point's intrinsic limitations). 正如所看到的, 打印 ING a确实显示出0.3 -因为默认print轮6张或7位十进制数,在打字时的表达式(这里a在提示符是单变量表达式)示出了具有在尽可能多的两次数字的结果(从而揭示浮点的内在局限性)。

Javascript may have slightly different rounding rules about how to display numbers, and the exact details of the rounding are plenty enough to explain the differences you observe. Javascript可能有关于如何显示数字的略微不同的舍入规则,并且舍入的确切细节足以解释您观察到的差异。 Note, for example (on a Chrome javascript console): 请注意,例如(在Chrome javascript控制台上):

> (1 + .1) * 1000000000
  1100000000
> (1 + .1) * 100000000000000
  110000000000000.02

see? 看到? if you manage to see more digits, the anomalies (which inevitably are there) become visible too. 如果你设法看到更多的数字,异常(这不可避免地那里)变得可见了。

and printing. 和印刷。

They might both have the same IEEE 754 underlying representation, but that doesn't mean they're forced to print the same way. 它们可能都具有相同的IEEE 754底层表示,但这并不意味着它们被迫以相同的方式打印。 It looks like Javascript is rounding the output when the difference is small enough. 当差异足够小时,看起来Javascript正在舍入输出。

With floating point numbers, the important part is how the binary data is structured, not what it shows on the screen. 对于浮点数,重要的部分是二进制数据的结构,而不是它在屏幕上显示的结构。

I would like to understand the reason for this difference in behavior. 我想了解这种行为差异的原因。

  1. They're different languages. 他们是不同的语言。

  2. They use different underlying packages. 他们使用不同的底层包。

  3. They have different implementations. 他们有不同的实现。

When you say "Python" -- which implementation are you talking about? 当你说“Python” - 你在谈论哪个实现? C, Jython, IronPython? C,Jython,IronPython? Did you compare each of those? 你有没有比较过这些?

The Javascript folks seem to handle repeating binary fractions differently from the way the Python folks handle repeating binary fractions. Javascript人员似乎处理重复的二进制分数与Python人员处理重复二进制分数的方式不同。

Sometimes Javascript quietly suppresses the error bits at the end. 有时Javascript会在最后悄悄地抑制错误位。 Sometimes it doesn't. 有时却没有。

That's the reason. 这就是原因。

You have the source code for both. 你有两个源代码。 If you want to know more, you can. 如果你想了解更多,你可以。 Knowing the source code doesn't change much, however. 但是,了解源代码并没有太大变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM