Skip to content
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

from_str rounding issues when overflow from too many digits #453

Closed
chris-cantor opened this issue Dec 29, 2021 · 2 comments
Closed

from_str rounding issues when overflow from too many digits #453

chris-cantor opened this issue Dec 29, 2021 · 2 comments

Comments

@chris-cantor
Copy link

chris-cantor commented Dec 29, 2021

I am trying to add tests for the overflow behaviour of from_str and discovered a problem:

assert_eq!(
    Decimal::from_str("99999_99999_99999_99999_99999_99999.99999"),
    // it returns  Ok(100000_00000_00000_00000_00000_000) now
    Err(Error::from("Invalid decimal: overflow from too many digits"))
);

https://github.com/cantortrading/rust-decimal/runs/4657432688?check_suite_focus=true#step:9:87

It seems that the function is trying to round the input, such that 999,000 could be rounded to 1000,000 which is off by a lot.

What is the intended behaviour here? May be we should not attempt rounding before the decimal point?

@chris-cantor
Copy link
Author

chris-cantor commented Dec 29, 2021

I discovered another edge case: the decimal parsing lost precision when there are too many leading 0s (i.e. ended up having > 28 coefficients)

assert_eq!(
    Decimal::from_str("00000_00000_00000_00000_00001.00001").unwrap(),
    Decimal::from_i128_with_scale(100001, 5) // it is now returning '1.0000'
);

https://github.com/cantortrading/rust-decimal/runs/4658904929?check_suite_focus=true#step:9:92

@chris-cantor chris-cantor changed the title from_str round integer when overflow before the decimal point from_str rounding issues when overflow from too many digits Dec 30, 2021
@paupino
Copy link
Owner

paupino commented Jan 8, 2022

Fixed in #456

@paupino paupino closed this as completed Jan 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants