-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Signed carry carrying_add in bigint_helper_methods #90541
Comments
I agree that this is a bug. There's a carry when the calculation is done as unsigned, but this is not the same in signed. Another example: assert_eq!(1i8.carrying_add(-1, false), (0, false)); That incorrectly returns |
There's some discussion about that in the tracking issue, starting with #85532 (comment) |
Yeah, it looks like I rehashed the same arguments, that's what I get for reading comments and doc too fast. Still, I think that this is surprising enough that it would be nice to address it somehow. |
I agree this is a bug. It looks like it's implementing the signed version using the unsigned one, returning the same carry flag (https://doc.rust-lang.org/1.56.0/src/core/num/int_macros.rs.html#1366-1369), but that's incorrect. Here's another demonstration that it needs an update: assert_eq!(i8::MAX.carrying_add(0, true), (0, true));
Would you be up to making a fix PR, @anisse? You can use |
I have no bandwidth for making a fix right now. Since this was supposed to be helpers for bigint operations, my first idea was to just remove the signed variants. I have no idea if this would be appropriate. Anyone is welcome to do a proper fix, it shouldn't be too complicated. |
A thought: What should It feels to me like the carry_out from that should perhaps be We do have a type that's -1, 0, or 1: Or should we just delete the signed versions for now? |
I think If I am correct, then this raises another question: should intermediate overflow be detected or not ? Tho I'd like other's opinion. |
I think so. The unsigned versions have clear semantics, but it gets weird with signed values, and as a "bigint helper" I'd be surprised to see one that uses signed digits in the first place. |
I had this question on the currently unreleased
#![feature(bigint_helper_methods)]
#85532; on signed carry (probably applies to substraction too ?); I have this test failing with the current code:Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=a161cf42872e087303d1435648e6eceb
Is the test correct ? A carry did happen in an intermediate calculation, but should the overall calculation report a carry ?
The text was updated successfully, but these errors were encountered: