Skip to content

Ord:cmp does not properly handle Ratio<T> with negative denominators #146

Description

@mikem8891

I am not sure if Ratio is supposed to handle negative denominators properly, but the Ord::cmp function does not, see the example below.

use num_rational::Ratio; // 0.4.2

fn main() {
    let neg_half = Ratio::<i8>::new_raw(1, -2);
    let one_third = Ratio::<i8>::new_raw(1, 3);
    println!("{neg_half} is {:?} than {one_third}", neg_half.cmp(&one_third));
    println!("{one_third} is {:?} than {neg_half}", one_third.cmp(&neg_half));
}

results in

1/-2 is Greater than 1/3
1/3 is Less than 1/-2

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