-
Notifications
You must be signed in to change notification settings - Fork 2
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
let mid = (lo + hi) / 2;
Overflows
#2
Comments
Thanks for the added context. As for why bit shift works for singed integers, when signed integers overflow in addition, the sign bit is incorrectly flipped, but this error can be salvaged by blindly shifting every bit to the right, including the flipped sign bit. I.e. the unsigned bit shift right |
Fixes SteadBytes#2 as suggested by @kaltu (Yulin Liu) in the issue. See also: https://research.google/blog/extra-extra-read-all-about-it-nearly-all-binary-searches-and-mergesorts-are-broken/
bisection/src/lib.rs
Line 140 in 14c9562
Overflow may happen in
(lo + hi)
where(lo, hi): (usize, usize)
consider changing it to
lo + (hi - lo) / 2
This is not an issue in the original Python implementation because Python handles overflow to large numbers automatically
But that is not the case for Rust
The text was updated successfully, but these errors were encountered: