-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StatisticsMedian: Fix bug #1658
Conversation
Previously, this could return the wrong result when there was an even number of elements. There were two `nth_element` calls. The second call could change elements in `[center2, end])`, which was where `center` pointed. Therefore, `*center` sometimes had the wrong value after the second `nth_element` call. Rewrite to use `max_element` instead of the second call to `nth_element`. This avoids modifying the vector.
Please add tests. |
The tests already exist. They failed sometimes. That's how I found it. |
Could you show the failure? Define benchmark/test/statistics_gtest.cc Lines 15 to 19 in dfc8a92
|
1/2 of the time, non-deterministically.
If you use A longer sequence will decrease the probability of success factorially.
It is. |
// before | ||
// Did we have an odd number of samples? If yes, then center is the median. | ||
// If not, then we are looking for the average between center and the value | ||
// before. Instead of resorting, we just look for the max value before it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// before. Instead of resorting, we just look for the max value before it,
// which is not necessarily the element immediately preceding the `center`,
// since the `copy` is only *partially* sorted.
Ok, so the actual bug here is iterator invalidation, effectively. But this is still an improvement, so okay. |
No, the iterator is still valid. The problem is an assumption that |
I did say "effectively" - if you consider that the |
thanks! |
Previously, this could return the wrong result when there was an even number of elements.
There were two
nth_element
calls. The second call could change elements in[center2, end])
, which was wherecenter
pointed. Therefore,*center
sometimes had the wrong value after the secondnth_element
call.Rewrite to use
max_element
instead of the second call tonth_element
. This avoids modifying the vector.