-
Notifications
You must be signed in to change notification settings - Fork 110
ManagedDecimal - mul/div half up, exp_approx, compounded_interest #2338
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ccaccb9
ManagedDecimal mul/div half up impl
andrei-marinica 7e12c60
ManagedDecimal mul/div half up test + fix
andrei-marinica 6e05e04
ManagedDecimal mul/div half up docs
andrei-marinica 80f86c7
ManagedDecimal - compounded_interest
andrei-marinica c435552
ManagedDecimal - exp_approx
andrei-marinica e4c2856
ManagedDecimal - exp_approx optimization
andrei-marinica d5b790d
ManagedDecimal - compounded_interest_factor rename
andrei-marinica 2f40b12
fix after review
andrei-marinica File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
framework/base/src/types/managed/wrapped/decimal/managed_decimal_half_up.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| use crate::{api::ManagedTypeApi, types::Sign}; | ||
|
|
||
| use super::{Decimals, ManagedDecimal, ManagedDecimalSigned}; | ||
|
|
||
| impl<M: ManagedTypeApi, D1: Decimals> ManagedDecimal<M, D1> { | ||
| /// Multiplies two decimals with half-up rounding to a target precision. | ||
| /// | ||
| /// Both operands are first rescaled to `precision`. The rescaled raw values | ||
| /// are multiplied, producing a result with `2 * precision` implied decimal | ||
| /// places. That intermediate value is then rounded back to `precision` using | ||
| /// the standard pre-bias trick: | ||
| /// | ||
| /// ```text | ||
| /// rounded = (product + scale / 2) / scale | ||
| /// ``` | ||
| /// | ||
| /// Adding `scale / 2` before the integer division means that any remainder | ||
| /// ≥ half the scale (i.e. the fractional part ≥ 0.5) causes the quotient | ||
| /// to increment by one — equivalent to round-half-up. | ||
| /// | ||
| /// # Credits | ||
| /// Original implementation by [@mihaieremia](https://github.com/mihaieremia). | ||
| pub fn mul_half_up<D2: Decimals, DResult: Decimals>( | ||
| &self, | ||
| other: &ManagedDecimal<M, D2>, | ||
| precision: DResult, | ||
| ) -> ManagedDecimal<M, DResult> { | ||
| // Use target precision directly, no +1 | ||
| let scaled_a = self.rescale(precision.clone()); | ||
| let scaled_b = other.rescale(precision.clone()); | ||
|
|
||
| // Perform multiplication in BigUint | ||
| let product = scaled_a.data * scaled_b.data; | ||
|
|
||
| // Half-up rounding at precision | ||
| let scale = precision.scaling_factor(); | ||
| let half_scaled = &*scale / 2u64; | ||
|
|
||
| // Round half-up | ||
| let rounded_product = (product + half_scaled) / &*scale; | ||
|
|
||
| ManagedDecimal::from_raw_units(rounded_product, precision) | ||
| } | ||
|
|
||
| /// Divides two decimals with half-up rounding to a target precision. | ||
| /// | ||
| /// Both operands are rescaled to `precision`. The numerator is then | ||
| /// multiplied by `scale` so the division produces a result with the correct | ||
| /// number of decimal places. The quotient is rounded using the pre-bias | ||
| /// trick: | ||
| /// | ||
| /// ```text | ||
| /// rounded = (numerator * scale + denominator / 2) / denominator | ||
| /// ``` | ||
| /// | ||
| /// Adding `denominator / 2` means that once the true quotient's remainder | ||
| /// reaches half the denominator (i.e. fractional part ≥ 0.5), integer | ||
| /// division increments the result — equivalent to round-half-up. | ||
| /// | ||
| /// # Credits | ||
| /// Original implementation by [@mihaieremia](https://github.com/mihaieremia). | ||
| pub fn div_half_up<D2: Decimals, DResult: Decimals>( | ||
| &self, | ||
| other: &ManagedDecimal<M, D2>, | ||
| precision: DResult, | ||
| ) -> ManagedDecimal<M, DResult> { | ||
| let scaled_a = self.rescale(precision.clone()); | ||
| let scaled_b = other.rescale(precision.clone()); | ||
|
|
||
| // Perform division in BigUint | ||
| let scale = precision.scaling_factor(); | ||
| let numerator = scaled_a.into_raw_units() * &*scale; | ||
| let denominator = scaled_b.into_raw_units(); | ||
|
|
||
| // Half-up rounding | ||
| let half_denominator = denominator.clone() / 2u64; | ||
| let rounded_quotient = (numerator + half_denominator) / denominator; | ||
|
|
||
| ManagedDecimal::from_raw_units(rounded_quotient, precision) | ||
| } | ||
| } | ||
|
|
||
| impl<M: ManagedTypeApi, D1: Decimals> ManagedDecimalSigned<M, D1> { | ||
| /// Multiplies two signed decimals with half-up (away-from-zero) rounding | ||
| /// to a target precision. | ||
| /// | ||
| /// The algorithm mirrors [`ManagedDecimal::mul_half_up`], but uses `BigInt` | ||
| /// arithmetic and adjusts the pre-bias direction based on the sign of the | ||
| /// intermediate product: | ||
| /// | ||
| /// ```text | ||
| /// if product < 0: rounded = (product - scale / 2) / scale | ||
| /// else: rounded = (product + scale / 2) / scale | ||
| /// ``` | ||
| /// | ||
| /// The VM's integer division truncates toward zero. Subtracting the bias | ||
| /// for a negative product pushes it *further* from zero before truncation, | ||
| /// so the final result rounds away from zero in both directions — matching | ||
| /// the conventional financial definition of "round half up" for signed | ||
| /// numbers. | ||
| /// | ||
| /// # Credits | ||
| /// Original implementation by [@mihaieremia](https://github.com/mihaieremia). | ||
| pub fn mul_half_up_signed<D2: Decimals, DResult: Decimals>( | ||
| &self, | ||
| other: &ManagedDecimalSigned<M, D2>, | ||
| precision: DResult, | ||
| ) -> ManagedDecimalSigned<M, DResult> { | ||
| let scaled_a = self.rescale(precision.clone()); | ||
| let scaled_b = other.rescale(precision.clone()); | ||
|
|
||
| // Perform multiplication in BigInt | ||
| let product = scaled_a.data * scaled_b.data; | ||
|
|
||
| // Half-up rounding at precision | ||
| let scale = precision.scaling_factor(); | ||
| let half_scaled = (scale.clone() / 2u64).into_big_int(); | ||
andrei-marinica marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Sign-aware "away-from-zero" rounding | ||
| let rounded_product = if product.sign() == Sign::Minus { | ||
| (product - half_scaled) / scale.as_big_int() | ||
| } else { | ||
| (product + half_scaled) / scale.as_big_int() | ||
| }; | ||
|
|
||
| ManagedDecimalSigned::from_raw_units(rounded_product, precision) | ||
| } | ||
|
|
||
| /// Divides two signed decimals with half-up (away-from-zero) rounding | ||
| /// to a target precision. | ||
| /// | ||
| /// The numerator is scaled up by `scale` (as in [`ManagedDecimal::div_half_up`]) | ||
| /// and then pre-biased before T-division (truncates toward zero). The bias | ||
| /// direction depends solely on the sign of the numerator — **not** on the | ||
| /// sign of the denominator: | ||
| /// | ||
| /// ```text | ||
| /// half = |denominator| / 2 | ||
| /// if numerator < 0: rounded = (numerator - half) / denominator | ||
| /// else: rounded = (numerator + half) / denominator | ||
| /// ``` | ||
| /// | ||
| /// This is correct because `half` is always non-negative. When `numerator > 0`, | ||
| /// adding `half` increases the numerator's magnitude; when the denominator is | ||
| /// negative, dividing a larger positive numerator yields a more-negative | ||
| /// result — farther from zero. The rule therefore rounds away from zero for | ||
| /// all four sign combinations of `(numerator, denominator)`. | ||
| /// | ||
| /// Using `sign(denominator)` as the branch condition instead would produce | ||
| /// wrong results whenever the denominator is negative. | ||
| /// | ||
| /// # Credits | ||
| /// Original implementation by [@mihaieremia](https://github.com/mihaieremia). | ||
| pub fn div_half_up_signed<D2: Decimals, DResult: Decimals>( | ||
| &self, | ||
| other: &ManagedDecimalSigned<M, D2>, | ||
| precision: DResult, | ||
| ) -> ManagedDecimalSigned<M, DResult> { | ||
| let scaled_a = self.rescale(precision.clone()); | ||
| let scaled_b = other.rescale(precision.clone()); | ||
|
|
||
| let scale = precision.scaling_factor(); | ||
| let numerator = scaled_a.data * scale.as_big_int(); | ||
| let denominator = scaled_b.data; | ||
|
|
||
| // Half-up rounding | ||
| let half_denominator = (denominator.magnitude() / 2u64).into_big_int(); | ||
| let rounded_quotient = if numerator.sign() == Sign::Minus { | ||
| (numerator - half_denominator) / denominator | ||
| } else { | ||
| (numerator + half_denominator) / denominator | ||
| }; | ||
|
|
||
| ManagedDecimalSigned::from_raw_units(rounded_quotient, precision) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.