English 中文(简体)
点层测量: 可能不安全地依赖具体比较?
原标题:Floating point arithmetics: Possible unsafe reliance on specific comparison?

The following python code calculates the number of iterations to do stuff based on some variables.

  # a - b - c is always a multiple of d.
  i = (a - b - c) / d
  while i:
    # do stuff
    i -= 1

这些变量将一律相同,即只有<代码>ints或floats或以任何方式。 我的关切是,如果数值为<编码>floats<>/code>,它是否会正确发挥作用。 我非常清楚,总是会考虑依赖确切的浮动值的陷阱。 但是,我可以说,上述情况是否危险。 我可以使用<条码>i = (环绕(a-b-c)/ d),但我很想更好地了解浮标。

标题如下:<代码>a - b - c为<代码>d。 因此,我依靠<代码>(a-b-c)/d成为i的数值,即我可以分出<代码>1,从左侧中获取预期的频率,并假定成为事实。 也就是说,能够计算出像这种多样,降幅为1,确切达到0?

我不仅想知道,这是否不安全,而且更重要的是,我需要了解如何解决像这个问题这样的问题? 如果某人决定性地知道这是否安全,是否可以解释如何?

最佳回答

You can use the decimal module to get an idea of what "hides" between a floating point number such as 0.3:

>>> from decimal import Decimal
>>> Decimal(0.3)
Decimal( 0.299999999999999988897769753748434595763683319091796875 )

请注意,A/63/12.7改变了浮动点编号的书写方式(repr(f) work),以便现在显示最短的表述,如果你有float(s),则该示点号可提供相同的浮动点号码。 这意味着repr(0.3) = 0.3 in-030 2.7,但repr(0.3) = 0.2999999999999999999 9999 in original edition. 我提及这一点,因为当你真想看到数字背后的东西时,它可以进一步混淆。

利用精子模块,我们可以在计算浮体时看到错误:

>>> (Decimal(2.0) - Decimal(1.1)) / Decimal(0.3) - Decimal(3) 
Decimal( -1.85037170771E-16 )

在此,我们可能期望<代码>(2.0-1.1) /0.3=3.0,但差异不大。 然而,如果你按照正常的浮动点数进行计算,那么你确实做到零:

>>> (2 - 1.1) / 0.3 - 3
0.0
>>> bool((2 - 1.1) / 0.3 - 3)
False

结果是,自1.85e-16非零以来,在其他地方四舍五入。

>>> bool(-1.85037170771E-16)
True

我不敢肯定,正是在进行这一轮谈判的时候。

关于整个休息时间的终止,有一个小标题I可以提供:53的浮动。

>>> 2.0**53    
9007199254740992.0
>>> 2.0**53 + 1
9007199254740992.0
>>> 2.0**53 + 2
9007199254740994.0

代表号之间的空间为2个<>53至254,如上所示。 但是,如果您的<代码>i是一种低于253>的ger,那么i - 1<> > > 即也是一种可观的ger,您最终将打上0.0<>>>>>>>>>>,该编码在沙尔被视为虚假。

问题回答

我将给你一个语言上的答案(我并不真的知道)。

你的法典存在多种潜在问题。 首先是:

(a - b - c)

If a is (for example) 109, and b and c are both 1, then the answer will be 109, not 109-2 (I m assuming single-precision float here).

之后是:

i = (a - b - c) / d

如果计算器和分母是能够准确代表浮点的数字(如0.3和0.1),那么结果可能不是确切的分类(可能为3.0001而不是3)。 因此,你将永远不会终止。

之后是:

i -= 1

同样,如果<代码>i目前为109,那么这一行动的结果仍将是10<_up>9,因此您的住所永远不会终止。

因此,你应坚决考虑进行所有计算,进行计算。

你有权不赞同零(至少比你打算多变)。 为什么没有测试:while i >= 1。 在此情况下,如与惯犯一样,如果贵重价值低于1,则 lo终将告结束。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...