二分查找和 eps 比较

发布于 2024-12-25 01:46:02 字数 548 浏览 2 评论 0原文

我在二分搜索中进行了两次比较,但我无法在两个底层之间做出准确的偏好。我在下面的两个样本之间振荡:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟的代价 2025-01-01 01:46:02

从您的代码来看,您正在尝试决定函数何时具有零值。第一种方法已经足够好了,因为它符合你的意图。看来没有必要用第二种方法了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文