rust-num / num-integer

Integer trait and functions for Rust
Apache License 2.0
180 stars 48 forks source link

`Integer::gcd()` returns negative value #55

Open ijagberg opened 1 year ago

ijagberg commented 1 year ago

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 commented 1 year ago

Sure, we could call this out in documentation.