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::gcd() returns negative value #55

Open
ijagberg opened this issue Sep 15, 2023 · 1 comment
Open

Integer::gcd() returns negative value #55

ijagberg opened this issue Sep 15, 2023 · 1 comment

Comments

@ijagberg
Copy link

ijagberg commented Sep 15, 2023

The docs for Integer::gcd() state "The result is always non-negative", but this is not the case when calculating the gcd of zero and the minimum value of a signed integer type (i8, i16, ...).

use num::Integer;

fn main() {
    assert!(i8::MIN.gcd(&0).is_negative()); // returns i8::MIN
}

The reason for this are the following lines in the impl_integer_for_isize macro, which in the above example result in .abs() being called on i8::MIN, resulting in a panic in debug mode, and the value i8::MIN being returned in release mode.

Don't know if this issue can be elegantly fixed, since the actual result does not fit in the signed type, but maybe it should at least be mentioned in the documentation for the gcd method.

@cuviper
Copy link
Member

cuviper commented Sep 18, 2023

Sure, we could call this out in documentation.

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

No branches or pull requests

2 participants