Skip to content

Commit d05f11d

Browse files
authored
Binary search on real values
1 parent d18e6e6 commit d05f11d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
double eps = 1e-6;
2+
int limit = 0;
3+
4+
// good function checks whether the searching value is valid or not.
5+
6+
bool good(double m) {
7+
// implement the standard good func;
8+
// m is the value you are searching for
9+
}
10+
11+
double f() {
12+
double l = 0, r = limit;
13+
double ans = l;
14+
for(int i=0;i<200 && l+eps <= r;i++) {
15+
double mid = (l+r)/2;
16+
if(good(mid)) ans = mid, // r = mid or l = mid;
17+
else // l = mid or r = mid;
18+
}
19+
return ans;
20+
}

0 commit comments

Comments
 (0)