Skip to content
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

ClockEstimate: Correct Average on Abort #1490

Merged
merged 1 commit into from
Jan 11, 2019

Conversation

ax3l
Copy link
Contributor

@ax3l ax3l commented Jan 7, 2019

Description

The clock estimator has a potential division by zero and the average is wrong on abort. Using iteration + 1 would be correct for an average and is consistent with the non-aborted result which is also sum / (max(i)+1).

Seen via coverity in a downstream project.

The clock estimator has a potential division by zero.
Using `iteration + 1` seems also more logical to me for
an average.

Found with coverity in a downstream project.
@codecov
Copy link

codecov bot commented Jan 7, 2019

Codecov Report

Merging #1490 into master will decrease coverage by 0.03%.
The diff coverage is 0%.

@@            Coverage Diff             @@
##           master    #1490      +/-   ##
==========================================
- Coverage   80.42%   80.39%   -0.03%     
==========================================
  Files         121      121              
  Lines        3421     3426       +5     
==========================================
+ Hits         2751     2754       +3     
- Misses        670      672       +2

1 similar comment
@codecov
Copy link

codecov bot commented Jan 7, 2019

Codecov Report

Merging #1490 into master will decrease coverage by 0.03%.
The diff coverage is 0%.

@@            Coverage Diff             @@
##           master    #1490      +/-   ##
==========================================
- Coverage   80.42%   80.39%   -0.03%     
==========================================
  Files         121      121              
  Lines        3421     3426       +5     
==========================================
+ Hits         2751     2754       +3     
- Misses        670      672       +2

Copy link
Contributor

@JoeyGrajciar JoeyGrajciar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that you should increase the i only in case of i == 0.

@ax3l
Copy link
Contributor Author

ax3l commented Jan 8, 2019

i think that you should increase the i only in case of i == 0.

No, the sum is already calculated at that point, should definitely be i+1 in all cases.

@JoeyGrajciar
Copy link
Contributor

so you want to divide sum by 2 in case of i=1?

@ax3l
Copy link
Contributor Author

ax3l commented Jan 8, 2019

the sum starts with zero and adds for i=0 the first delta. Divided by one (i+1) this is still delta.

in the second iteration, i=1, the sum is the first value plus the second delta. You also want to divide by i+1 (now two).

The last iteration is i=iterations-1. At the same point where you divide sum/iterations you could divide sum/(i+1) (if you were just before exiting the loop).

@JoeyGrajciar
Copy link
Contributor

okay in that case i acknowledge my mistake :)

@hbina
Copy link
Contributor

hbina commented Jan 9, 2019

Is it possible to rename this PR? Division by Zero is a bit misleading because imo this will never happen. The actual problem (as correctly pointed out by the author) is that the mean/average is actually inaccurate.

Edit: Reading through the code again, I think there is actually no problem here. The mean calculation is already correct. Note that the iteration is using pre-increment.

for( std::size_t i = 0; i < iterations; ++i ) {

Edit2:I am just stupid, ignore edit1.

@ax3l ax3l changed the title ClockEstimate: DivByZero ClockEstimate: Correct Average on Abort Jan 9, 2019
@ax3l
Copy link
Contributor Author

ax3l commented Jan 9, 2019

Hi @hbina, this is zero-indexed for-loop, with range for i from 0 to iterations-1.

// main.cpp
#include <cstddef>
#include <iostream>


int main()
{
    std::size_t iterations = 4u;

    for( std::size_t i = 0; i < iterations; ++i ) {
        std::cout << i << std::endl;
    }
    return 0;
}

Compile & run:

$ g++ main.cpp && ./a.out                                                                                                                                         
0
1
2
3

$

Here is how for-loops work in C++:

iteration_expression: any expression, which is executed after every iteration of the loop and before re-evaluating condition. Typically, this is the expression that increments the loop counter

@ax3l
Copy link
Contributor Author

ax3l commented Jan 11, 2019

@horenmar just in case: this is a simple fix for a corner-case, if you might want to take a look :)

@horenmar
Copy link
Member

Yeah, the estimate is wrong in case of early-abort, and veeeeeeery potentially can cause a division by zero (if your clock resolution is worse than 3 seconds, 🤷‍♂️ ).

There is no reason not to fix it though.

@horenmar horenmar merged commit d1e7344 into catchorg:master Jan 11, 2019
@ax3l ax3l deleted the fix-divByZeroClock branch January 11, 2019 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants