二分查找和 eps 比较
我在二分搜索中进行了两次比较,但我无法在两个底层之间做出准确的偏好。我在下面的两个样本之间振荡:
for (int step = 0; step < 100; ++step) {
double middle = (left + right) / 2;
if (f(middle) > 0) right = middle; else left = middle;
}
f
for (int step = 0; step < 100; ++step) {
double middle = (left + right) / 2;
if (f(middle) > eps) right = middle; else left = middle;
}
是一个单调递增函数,因为即使 eps 很小,二分搜索参数中的相应误差也存在更大的危险。另一方面,即使由于舍入误差而导致我们对相等值的比较不正确,二分搜索仍将正确收敛,因为相等值可能只出现在一个点中,并且在非常接近它的点中所有内容都将是正确的。我想对此有一个想法。
I have 2 comparisons inside binary search, but I can't make an exact preference in between two underlain. I oscillate between in two samples below:
for (int step = 0; step < 100; ++step) {
double middle = (left + right) / 2;
if (f(middle) > 0) right = middle; else left = middle;
}
and
for (int step = 0; step < 100; ++step) {
double middle = (left + right) / 2;
if (f(middle) > eps) right = middle; else left = middle;
}
f is a monotonically increasing function, because even with small eps, there's a danger that the corresponding error in the binary search parameter will be much bigger. On the other hand, even if our comparison is incorrect for equal values due to rounding errors, the binary search will still converge correctly since equal values may only appear in one point and everything will be correct in points very close to it. I want to have an idea about that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的代码来看,您正在尝试决定函数何时具有零值。第一种方法已经足够好了,因为它符合你的意图。看来没有必要用第二种方法了。
Judging from your code, you are trying to decide when the function will have a zero value. The first method is already good enough, for it is consistent with your intention. It seems that there is no need to use the second method.