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

Decimal - add checked_div_euclid #1365

Closed
wants to merge 4 commits into from

Conversation

ueco-jb
Copy link
Contributor

@ueco-jb ueco-jb commented Jul 14, 2022

part of #1186

@ueco-jb ueco-jb self-assigned this Jul 14, 2022
@ueco-jb ueco-jb marked this pull request as ready for review July 14, 2022 06:52
@ueco-jb ueco-jb force-pushed the 1186-decimals-checked-div-euclid branch from f4626c8 to 7ebbe29 Compare July 14, 2022 09:36
Copy link
Member

@webmaster128 webmaster128 left a comment

Choose a reason for hiding this comment

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

Okay, it took me a bit to figure out what euclid division actually does. It seems to be relatively straight forward as long as no negative numbers are involved. I found https://www.probabilisticworld.com/euclidean-division-integer-division-with-remainders/ useful.

Looking at the Decimal implementation, it is possible to trigger unnecessary errors in the current implementation (fails with panicked at 'called Result::unwrap() on an Err value: Overflow'):

        // The regular division result (340282366920938463463.6) would be out of range.
        assert_eq!(
            dec("170141183460469231731.8")
                .checked_div_euclid(dec("0.5"))
                .unwrap(),
            dec("340282366920938463463")
        );

I suggest splitting the uint implementations and the decimal implementation in two PRs and try to find a better implementation for decimals.

@webmaster128 webmaster128 added this to the 1.1.0 milestone Jul 16, 2022
@ueco-jb ueco-jb marked this pull request as draft July 31, 2022 15:57
@ueco-jb ueco-jb changed the title Decimal/Uint - add checked_div_euclid Decimal - add checked_div_euclid Jul 31, 2022
@uint
Copy link
Contributor

uint commented Aug 15, 2022

@webmaster128 Euclidean division seems to be defined as an operation between two integers. See here. Since we're dealing with decimals here, it doesn't seem applicable.

@webmaster128
Copy link
Member

webmaster128 commented Aug 15, 2022

Ah, good point. Thanks for pointing that out! It is also not implemented for float types in Rust (https://doc.rust-lang.org/std/?search=checked_div_euclid).

@webmaster128
Copy link
Member

Closing due to #1365 (comment). The int implementations are here: #1376

@ueco-jb ueco-jb deleted the 1186-decimals-checked-div-euclid branch August 16, 2022 07:08
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.

3 participants