Skip to content

Integer::gcd() returns negative value #55

Open
@ijagberg

Description

@ijagberg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions