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

Integer::extended_gcd should not permit unsigned value. #40

Open
aobatact opened this issue Apr 26, 2021 · 5 comments
Open

Integer::extended_gcd should not permit unsigned value. #40

aobatact opened this issue Apr 26, 2021 · 5 comments

Comments

@aobatact
Copy link
Contributor

This code fails to get the y value, with the error 'attempt to subtract with overflow'.

#[test]
fn extended_gcd_test(){
  use num_integer::Integer;
  let _x = 10.extended_gcd(&4);
  let _y = 10_u32.extended_gcd(&4);
}
@aobatact
Copy link
Contributor Author

aobatact commented Apr 26, 2021

May be we can require Self: Signed for extended_gcd

@aobatact aobatact changed the title Integer::extended_gcd fails for unsigned value. Integer::extended_gcd should not permit unsigned value. Apr 26, 2021
@cuviper
Copy link
Member

cuviper commented Apr 26, 2021

Yeah, we do require Signed on extended_gcd_lcm, but that was overlooked on extended_gcd. Unfortunately it requires a breaking change to add that constraint.

@aj-r
Copy link

aj-r commented Jan 8, 2024

Alternatively, it's possible to support extended_gcd for unsigned integers as described here: https://stackoverflow.com/q/67097428/1299394, which would avoid a breaking change.

Edit: because x and y must both be positive for unsigned integers, you'd need to modify the check() function used for testing - instead of assert_eq!(gcd, x * a + y * b); it would need to be something like:

assert_eq!(
  gcd,
  (x * a + y * b) % (a * b),
);

@cuviper
Copy link
Member

cuviper commented Jan 8, 2024

@aj-r interesting, thanks! I'm open to "fixing" unsigned with that kind of implementation, as long as we're careful to document the difference -- and ideally how to reconstruct what the signed result would have been.

@cuviper
Copy link
Member

cuviper commented Jan 8, 2024

Maybe we could then drop Signed from extended_gcd_lcm -- but I think that's technically a breaking change for anyone that overrode that method, if they depended on the conditional Signed constraint in their impl. What a mess...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants