-
Notifications
You must be signed in to change notification settings - Fork 338
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
impl Div<Decimal256> for Uint256 #1822
Comments
This also raises the question about why we don't have impl From<Decimal256> for Uint256 // should floor by default and impl TryFrom<Uint256> for Decimal256 I imagine these would be very useful to many. |
After playing around I suppose this is probably the cleanest solution: pub fn div(numerator: Uint256, denominator: Decimal256) -> Uint256 {
numerator * Decimal256::one().atomics() / denominator.atomics()
} and perhaps to prevent overflow during the first mul then this would be the most robust version: pub fn checked_div(numerator: Uint256, denominator: Decimal256) -> StdResult<Uint256> {
Ok(numerator.full_mul(Decimal256::one().atomics()).checked_div(Uint512::from(denominator.atomics()))?.try_into()?)
} |
Mixed type arithmetic is discouraged and should not be implemented. I was a mistake to implement it in the first place. As a consequence, However, #1566 added things like |
Ah yeah I suppose that makes sense. Adding those conversions would definitely be really helpful I think. I've personally found it a bit cumbersome! Thanks for the reply though. Feel free to close this issue, or change the title to implementing those conversions! |
With #1825 being shipped in 1.4, I think we are good here. Closing for now. |
Currently this is not implemented, my guess as to why being that the Output type is not obvious.
The most obvious workaround being:
This comes with the drawback that if
my_dec
is very large you begin to lose significant precision with .inv(), to the point that it could become 0. Another possible solution is perhaps:My question is then, Do you plan to implement this? and if not, is this method the best way to go about it?
The text was updated successfully, but these errors were encountered: