Skip to content

num::rationals rounding method is incorrect #15826

Closed
@libfud

Description

@libfud
extern crate num;

use num::rational::BigRational;

fn main() {
    let x = from_str::<BigRational>("2/5").unwrap();
    println!("{}, {}", x, x.round());
}

Prints "2/5, 1".

The method is written here:
http://static.rust-lang.org/doc/master/src/num/home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libnum/rational.rs.html#138-140

pub fn round(&self) -> Ratio<T> {
    if *self < Zero::zero() {
        Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom)
    } else {
        Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom)
    }
}

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