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

Fix wrong integer division where rounding up was intended #1254

Merged
merged 2 commits into from
Oct 6, 2022

Conversation

p12tic
Copy link
Contributor

@p12tic p12tic commented Oct 2, 2022

The intention in cases like std::ceil(a / b) (where a and b are integers) likely was to get floating-point division result and round it upwards. However, the division was performed with integer arguments, fractional part was lost and thus std::ceil did nothing. This PR introduces a separate function to perform such integer division and fixes the buggy cases.

If the original intention was wrong, this PR will break behavior. According to my limited understanding of the code the original intention was correct.

A subsequent PR will change all similar cases where we currently do int(std::ceil(double(a) / double(b))) to use divideRoundUp().

simogasp
simogasp previously approved these changes Oct 2, 2022
Copy link
Member

@simogasp simogasp left a comment

Choose a reason for hiding this comment

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

LGTM!

@p12tic p12tic force-pushed the fix-int-division-ceil branch 2 times, most recently from 1dadfe4 to 2103e16 Compare October 2, 2022 15:12
@p12tic
Copy link
Contributor Author

p12tic commented Oct 2, 2022

I've updated the function again. I managed to leave an error for negative numbers and ran missed it by running tests that haven't yet been recompiled with updated code.

@simogasp
Copy link
Member

simogasp commented Oct 2, 2022

it's funny how an apparently simple function like this still has so many entries on StackOverflow and many different answers.
A guy even went all the way to benchmark some of the proposed implementations with different datatype and compilers
https://stackoverflow.com/a/65784221

@simogasp simogasp added the bugfix label Oct 2, 2022
@simogasp simogasp added this to the 2.5.0 milestone Oct 2, 2022
simogasp
simogasp previously approved these changes Oct 2, 2022
The intention here likely was to get floating-point division result and
round it upwards. However, the division was performed with integer
arguments, fractional part was lost and thus std::ceil did nothing.
@fabiencastan fabiencastan merged commit ec91fab into alicevision:develop Oct 6, 2022
@p12tic p12tic deleted the fix-int-division-ceil branch October 6, 2022 23:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants