-
Notifications
You must be signed in to change notification settings - Fork 344
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
Remove mixed type arithmetic Decimal * Uint128 #1485
Comments
This is not a "better solution" in my mind. Very cumbersome to use and not adding clarity to devs. If you want to make |
Yeah, this was not meant as the final API. It should just showcase how fn multiply_by(self, rhs: impl Fraction) -> Uint128 instead to keep the caller code concise. |
Along these lines, it would be very useful to have some ceil and floor multiplication for Uints and Decimals. Something like this: fn mul_ceiled(self, rhs: impl Fraction) -> Self;
fn mul_floored(self, rhs: impl Fraction) -> Self; This would allow for a lot of flexibility when dealing with rounding errors etc. |
@uint is 👆 something a macro implementation could help us with? I think we need those 4 functions implemented for Uint64, Uint128 and Uint256: fn mul_ceiled(self, rhs: impl Fraction) -> Self;
fn mul_floored(self, rhs: impl Fraction) -> Self;
fn checked_mul_ceiled(self, rhs: impl Fraction) -> Result<Self, EnumSinceWeHaveTwoDifferentErrorCases>;
fn checked_mul_floored(self, rhs: impl Fraction) -> Result<Self, EnumSinceWeHaveTwoDifferentErrorCases>; With those in place, we can confidentally remove the mixed type operator implementation in 2.0. |
Probably! Like an |
Yeah, or better specifically for the new additions Note that Uint512 should not get them since it does not have a |
|
@webmaster128 I took a shot at it yesterday. I think it's not as simple as reusing the logic in let left: Uint64;
let right: Decimal; // or even Decimal256
let result = left.checked_mul_floored(right).unwrap(); I suspect the multiplication of |
Right, this is something I vaguely imagined to become a problem. So what we do currently support is basically
with (more or less) Decimal being a
So the number if bits in the integer needs to be >= the number of bits of the decimal. We are lacking an easy way to get
However, if we do not do the multiplication size based on the integer size (current |
Hey folks, tried my hand at this here: #1566 for Uint128. A few things:
|
Just pushed an update to that PR that adds a macro. If it's acceptable to cast to Uint512 for all int sizes, we simply need to wrap all of the Uint structs in Edit: Ah, looks like it may be better to start a generic arithmetic macro instead? |
Checked math only makes sense if the contract actually handles the error cases. If they just |
There is no natural return type for Decimal * Uint128. It could be Decimal, Uint128 or even Uint512. See https://stackoverflow.com/a/44552464/2013738 for a more elaborate description of the problem.
The choice of Decimal * Uint128 = Uint128 has no reason other than its first use case. It has often confused users who expect Decimal * Uint128 = Decimal.
The better solution for the integer math is something like:
The better solution for the decimal math is something like:
The text was updated successfully, but these errors were encountered: