Skip to content

Commit 7ec4fc5

Browse files
committed
Fix test, fix the confusion between double and int
1 parent d0ba604 commit 7ec4fc5

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

contents/approximate_counting/code/c++/approximate_counting.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ auto n(double v, double a) { return a * (pow((1 + 1 / a), v) - 1); }
2525
// - v: value in register
2626
// - a: a scaling value for the logarithm based on Morris's paper
2727
// It returns a new value for v
28-
auto increment(double v, double a) {
28+
auto increment(int v, double a) {
2929
// delta is the probability of incrementing our counter
3030
const auto delta = 1 / (n(v + 1, a) - n(v, a));
3131
return (drand() <= delta) ? v + 1 : v;
@@ -35,7 +35,7 @@ auto increment(double v, double a) {
3535
// - n_items: number of items to count and loop over
3636
// - a: a scaling value for the logarithm based on Morris's paper
3737
// It returns n(v,a), the approximate count
38-
auto approximate_count(int n_items, int a) {
38+
auto approximate_count(int n_items, double a) {
3939
auto v = 0;
4040
for (auto i = 0; i < n_items; ++i)
4141
v = increment(v, a);
@@ -55,7 +55,7 @@ auto test_approximate_count(
5555
for (auto i = 0; i < n_trials; ++i)
5656
sum += approximate_count(n_items, a);
5757
const auto avg = sum / n_trials;
58-
return (avg - n_items) / n_items < threshold ? "pass" : "fail";
58+
return std::abs((avg - n_items) / n_items) < threshold ? "pass" : "fail";
5959
}
6060

6161
int main() {

0 commit comments

Comments
 (0)