如何防止浮点数不精确影响numpy.arange? [英] How to prevent float imprecision from affecting numpy.arange?
问题描述
因为 numpy.arange()使用ceil((stop - start)/step)
确定项目数,小的浮点不精确度(stop = .400000001)
可以在列表中添加意外的值.
Because numpy.arange() uses ceil((stop - start)/step)
to determine the number of items, a small float imprecision (stop = .400000001)
can add an unintended value to the list.
第一种情况不包括停止点(预定)
The first case does not include the stop point (intended)
>>> print(np.arange(.1,.3,.1))
[0.1 0.2]
第二种情况包括停止点(不希望)
The second case includes the stop point (not intended)
>>> print(np.arange(.1,.4,.1))
[0.1 0.2 0.3 0.4]
numpy.linspace()修复了此问题问题,np.linspace(.1,.4-.1,3)
.但需要您知道步骤数. np.linspace(start,stop-step,np.ceil((stop-step)/step))
导致相同的无能.
numpy.linspace() fixes this problem, np.linspace(.1,.4-.1,3)
. but requires you know the number of steps. np.linspace(start,stop-step,np.ceil((stop-step)/step))
leads to the same incosistencies.
如何在不知道范围内元素数量的情况下生成可靠的float
范围?
How can I generate a reliable float
range without knowing the # of elements in the range?
考虑要生成精度未知的浮点索引的情况
Consider the case in which I want generate a float index of unknown precision
np.arange(2.00(...)001,2.00(...)021,.00(...)001)
推荐答案
您的目标是计算如果值是使用精确数学计算出的ceil((stop - start)/step)
.
Your goal is to calculate what ceil((stop - start)/step)
would be if the values had been calculated with exact mathematics.
仅给出 浮点值start
,stop
和step
是可能会发生一些舍入错误的运算结果,因此这是不可能的.四舍五入会删除信息,并且根本无法从信息不足的情况下创建信息.
This is impossible to do given only floating-point values of start
, stop
, and step
that are the results of operations in which some rounding errors may have occurred. Rounding removes information, and there is simply no way to create information from lack of information.
因此,仅当您具有有关start
,stop
和step
的其他信息时,此问题才可以解决.
Therefore, this problem is only solvable if you have additional information about start
, stop
, and step
.
假设step
是精确的,但是start
和stop
积累了一些受e0
和e1
限制的错误.也就是说,您知道start
与理想数学值(在任一方向上)最多为e0
,并且stop
与理想数学值(在任一方向上)最多是e1
.这样,(stop-start)/step
的理想值可能会从其理想值开始在(stop-start-e0-e1)/step
到(stop-start+e0+e1)/step
的范围内.
Suppose step
is exact, but start
and stop
have some accumulated errors bounded by e0
and e1
. That is, you know start
is at most e0
away from its ideal mathematical value (in either direction), and stop
is at most e1
away from its ideal value (in either direction). Then the ideal value of (stop-start)/step
could range from (stop-start-e0-e1)/step
to (stop-start+e0+e1)/step
away from its ideal value.
假设(stop-start-e0-e1)/step
至(stop-start+e0+e1)/step
之间存在整数.然后,仅根据start
,stop
和step
的浮点值以及边界e0
和
Suppose there is an integer between (stop-start-e0-e1)/step
to (stop-start+e0+e1)/step
. Then it is impossible to know whether the ideal ceil
result should be the lesser integer or the greater just from the floating-point values of start
, stop
, and step
and the bounds e0
and e1
.
但是,从您给出的示例来看,理想的(stop-start)/step
可能正好是整数,如(.4-.1)/.1
所示.如果是这样,则任何非零的错误界限都可能导致错误间隔跨越整数,从而使该问题无法从到目前为止的信息中解决.
However, from the examples you have given, the ideal (stop-start)/step
could be exactly an integer, as in (.4-.1)/.1
. If so, any non-zero error bounds could result in the error interval straddling an integer, making the problem impossible to solve from the information we have so far.
因此,为了解决问题,您不仅必须具有关于错误的简单界限,还必须具有更多信息.例如,您必须知道(stop-start)/step
恰好是整数或以其他方式量化.例如,如果您知道理想的步数计算将产生0.1的倍数,例如3.8、3.9、4.0、4.1或4.2,但绝不会是4.05,并且误差足够小,以至于浮动-点计算(stop-start)/step
的最终误差小于.05,则可以将(stop-start)/step
舍入为最接近的合格倍数,然后对其应用ceil
.
Therefore, in order to solve the problem, you must have more information than just simple bounds on the errors. You must know, for example, that (stop-start)/step
is exactly an integer or is otherwise quantized. For example, if you knew that the ideal calculation of the number of steps would produce a multiple of .1, such as 3.8, 3.9, 4.0, 4.1, or 4.2, but never 4.05, and the errors were sufficiently small that the floating-point calculation (stop-start)/step
had a final error less than .05, then it would be possible to round (stop-start)/step
to the nearest qualifying multiple and then to apply ceil
to that.
如果您有这样的信息,则可以使用关于start
,stop
和step
(例如的错误)的已知知识来更新问题.从十进制到浮点的单次转换的结果)和理想(stop-start)/step
的可能值.如果您没有这样的信息,那就没有解决办法.
If you have such information, you can update the question with what you know about the errors in start
, stop
, and step
(e.g., perhaps each of them is the result of a single conversion from decimal to floating-point) and the possible values of the ideal (stop-start)/step
. If you do not have such information, there is no solution.
这篇关于如何防止浮点数不精确影响numpy.arange?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!