-
Notifications
You must be signed in to change notification settings - Fork 74
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
with_prec
handles negative numbers weirdly; in particular, differently from round
#75
Comments
As of version 0.4.1, many things have changed. |
Finally going through backlog of issues here. Yes, |
Something like this: 5cce611 |
That's much better, thank you. #[test]
fn test_with_prec() {
let test_cases = vec![
("-1.555", 3, "-1.56"),
( "1.555", 3, "1.56"),
("-1", 0, "0"),
( "1", 0, "0"),
("-9999.444455556666", 14, "-9999.4444555567"),
( "9999.444455556666", 14, "9999.4444555567"),
("-12345678987654321.123456789", 25, "-12345678987654321.12345679"),
( "12345678987654321.123456789", 25, "12345678987654321.12345679"),
("-1.234", 2, "-1.2"),
( "1.234", 2, "1.2"),
];
for &(x, prec, y) in test_cases.iter() {
let a = BigDecimal::from_str(x).unwrap();
let b = BigDecimal::from_str(y).unwrap();
assert_eq!(a.with_prec(prec), b, "test: {:?}", (x, prec, y));
}
} |
Actually I did write tests but never pushed to GitHub before working on another branch. I incorporated your test values, and made the test stricter |
Long before
with_scale
andround
were shipped, I used my own version ofround
based onwith_prec
:It doesn’t suffer from the bug #69 and passes all test cases for
round
but the following four:(However,
with_accu
fails the test case("7", -2, "0")
in this simple implementation, so maybe this test is worth adding to the test suit forround
.)This means that
with_prec
andround
use different logic for the rounding of negative numbers. In fact,with_prec
in general handles negative numbers in an unexpected way. In particular, the following test passes:(Compare the last two test cases! This is probably not OK, and maybe deserves a separate issue.)
And every one of the following test cases fails (both the versions with correct precision and the versions with incorrect precision adjusted to counter the odd
with_prec
behavior with negative numbers so it at least gives the correct number of digits):I didn’t put any thought in the question whether this should be fixed and if yes then how exactly, but I’m quite certain that this issue deserves being recorded here.
The text was updated successfully, but these errors were encountered: