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

Fix Decimal::to_i64 for I64::MIN #496

Closed
wants to merge 1 commit into from
Closed

Conversation

arthurprs
Copy link
Contributor

The overflow check didn't consider that the maximum mantissa for negative numbers can use the extra bit.

@@ -2127,14 +2127,14 @@ impl ToPrimitive for Decimal {
fn to_i64(&self) -> Option<i64> {
let d = self.trunc();
// Quick overflow check
if d.hi != 0 || (d.mid & 0x8000_0000) > 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is close, but isn't quite right either I don't think. I think we need to check explicitly for i64::MIN since that is the value which is unable to be represented.
I'll write a couple more tests and contribute to this PR.

paupino added a commit that referenced this pull request Mar 19, 2022
Co-authored-by: Paul Mason <paul@paulmason.me>
Co-authored-by: Arthur Silva <arthurprs@gmail.com>
@paupino
Copy link
Owner

paupino commented Mar 19, 2022

Merged manually in 2de2a6d

@paupino paupino closed this Mar 19, 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
Development

Successfully merging this pull request may close these issues.

None yet

2 participants