Skip to content

num::rationals rounding method is incorrect #15826

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

Closed
libfud opened this issue Jul 20, 2014 · 2 comments
Closed

num::rationals rounding method is incorrect #15826

libfud opened this issue Jul 20, 2014 · 2 comments

Comments

@libfud
Copy link

libfud commented Jul 20, 2014

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)
    }
}
jbcrail added a commit to jbcrail/rust that referenced this issue Aug 28, 2014
The implemented fix rounds half-way cases away from zero as described in
the original comments.

This rounding algorithm is sometimes called arithmetic rounding. It is
described further here:
http://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero

I also added several new tests to prevent regressions.
@jbcrail
Copy link
Contributor

jbcrail commented Sep 1, 2014

The rounding error was fixed by #16778. This issue can be closed.

@alexcrichton
Copy link
Member

Thanks @jbcrail!

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

4 participants