-
Notifications
You must be signed in to change notification settings - Fork 110
Math module #2334
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
Math module #2334
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
50bbd87
math module moved
andrei-marinica 34c8fb1
math - linear interpolation & weighted average
andrei-marinica 0f2cd32
basic-features math features + test
andrei-marinica 4fa495d
cleanup
andrei-marinica 66bf318
comment fix
andrei-marinica 871e126
test fix
andrei-marinica 6768878
math - linear interpolation min_in < max_in
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
192 changes: 192 additions & 0 deletions
192
contracts/feature-tests/basic-features/scenarios/math_features.scen.json
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,192 @@ | ||
| { | ||
| "name": "math features", | ||
| "steps": [ | ||
| { | ||
| "step": "setState", | ||
| "accounts": { | ||
| "sc:basic-features": { | ||
| "nonce": "0", | ||
| "balance": "0", | ||
| "code": "mxsc:../output/basic-features.mxsc.json" | ||
| }, | ||
| "address:an_account": { | ||
| "nonce": "0", | ||
| "balance": "0" | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| "step": "scCall", | ||
| "id": "weighted_average_equal_weights", | ||
| "comment": "(10*1 + 20*1) / (1+1) = 15", | ||
| "tx": { | ||
| "from": "address:an_account", | ||
| "to": "sc:basic-features", | ||
| "function": "math_weighted_average", | ||
| "arguments": ["10", "1", "20", "1"], | ||
| "gasLimit": "50,000,000", | ||
| "gasPrice": "0" | ||
| }, | ||
| "expect": { | ||
| "out": ["15"], | ||
| "status": "0" | ||
| } | ||
| }, | ||
| { | ||
| "step": "scCall", | ||
| "id": "weighted_average_unequal_weights", | ||
| "comment": "(0*3 + 30*7) / (3+7) = 21", | ||
| "tx": { | ||
| "from": "address:an_account", | ||
| "to": "sc:basic-features", | ||
| "function": "math_weighted_average", | ||
| "arguments": ["0", "3", "30", "7"], | ||
| "gasLimit": "50,000,000", | ||
| "gasPrice": "0" | ||
| }, | ||
| "expect": { | ||
| "out": ["21"], | ||
| "status": "0" | ||
| } | ||
| }, | ||
| { | ||
| "step": "scCall", | ||
| "id": "weighted_average_truncates", | ||
| "comment": "(0*1 + 10*3) / (1+3) = 30/4 = 7 (truncated)", | ||
| "tx": { | ||
| "from": "address:an_account", | ||
| "to": "sc:basic-features", | ||
| "function": "math_weighted_average", | ||
| "arguments": ["0", "1", "10", "3"], | ||
| "gasLimit": "50,000,000", | ||
| "gasPrice": "0" | ||
| }, | ||
| "expect": { | ||
| "out": ["7"], | ||
| "status": "0" | ||
| } | ||
| }, | ||
| { | ||
| "step": "scCall", | ||
| "id": "weighted_average_round_up_equal_weights", | ||
| "comment": "(10*1 + 20*1) / (1+1) = 15 (exact, no rounding)", | ||
| "tx": { | ||
| "from": "address:an_account", | ||
| "to": "sc:basic-features", | ||
| "function": "math_weighted_average_round_up", | ||
| "arguments": ["10", "1", "20", "1"], | ||
| "gasLimit": "50,000,000", | ||
| "gasPrice": "0" | ||
| }, | ||
| "expect": { | ||
| "out": ["15"], | ||
| "status": "0" | ||
| } | ||
| }, | ||
| { | ||
| "step": "scCall", | ||
| "id": "weighted_average_round_up_rounds_up", | ||
| "comment": "(0*1 + 10*3 + 4 - 1) / (1+3) = 33/4 = 8 (rounded up from 7.5)", | ||
| "tx": { | ||
| "from": "address:an_account", | ||
| "to": "sc:basic-features", | ||
| "function": "math_weighted_average_round_up", | ||
| "arguments": ["0", "1", "10", "3"], | ||
| "gasLimit": "50,000,000", | ||
| "gasPrice": "0" | ||
| }, | ||
| "expect": { | ||
| "out": ["8"], | ||
| "status": "0" | ||
| } | ||
| }, | ||
| { | ||
| "step": "scCall", | ||
| "id": "weighted_average_round_up_exact", | ||
| "comment": "(1*1 + 4*2 + 3 - 1) / (1+2) = 11/3 = 3 (exact, no rounding)", | ||
| "tx": { | ||
| "from": "address:an_account", | ||
| "to": "sc:basic-features", | ||
| "function": "math_weighted_average_round_up", | ||
| "arguments": ["1", "1", "4", "2"], | ||
| "gasLimit": "50,000,000", | ||
| "gasPrice": "0" | ||
| }, | ||
| "expect": { | ||
| "out": ["3"], | ||
| "status": "0" | ||
| } | ||
| }, | ||
| { | ||
| "step": "scCall", | ||
| "id": "linear_interpolation_midpoint", | ||
| "comment": "(0*(100-50) + 200*(50-0)) / (100-0) = 10000/100 = 100", | ||
| "tx": { | ||
| "from": "address:an_account", | ||
| "to": "sc:basic-features", | ||
| "function": "math_linear_interpolation", | ||
| "arguments": ["0", "100", "50", "0", "200"], | ||
| "gasLimit": "50,000,000", | ||
| "gasPrice": "0" | ||
| }, | ||
| "expect": { | ||
| "out": ["100"], | ||
| "status": "0" | ||
| } | ||
| }, | ||
| { | ||
| "step": "scCall", | ||
| "id": "linear_interpolation_at_min", | ||
| "comment": "current_in == min_in => returns min_out = 5", | ||
| "tx": { | ||
| "from": "address:an_account", | ||
| "to": "sc:basic-features", | ||
| "function": "math_linear_interpolation", | ||
| "arguments": ["0", "10", "0", "5", "15"], | ||
| "gasLimit": "50,000,000", | ||
| "gasPrice": "0" | ||
| }, | ||
| "expect": { | ||
| "out": ["5"], | ||
| "status": "0" | ||
| } | ||
| }, | ||
| { | ||
| "step": "scCall", | ||
| "id": "linear_interpolation_at_max", | ||
| "comment": "current_in == max_in => returns max_out = 15", | ||
| "tx": { | ||
| "from": "address:an_account", | ||
| "to": "sc:basic-features", | ||
| "function": "math_linear_interpolation", | ||
| "arguments": ["0", "10", "10", "5", "15"], | ||
| "gasLimit": "50,000,000", | ||
| "gasPrice": "0" | ||
| }, | ||
| "expect": { | ||
| "out": ["15"], | ||
| "status": "0" | ||
| } | ||
| }, | ||
| { | ||
| "step": "scCall", | ||
| "id": "linear_interpolation_out_of_range", | ||
| "comment": "current_in > max_in => sc_panic", | ||
| "tx": { | ||
| "from": "address:an_account", | ||
| "to": "sc:basic-features", | ||
| "function": "math_linear_interpolation", | ||
| "arguments": ["0", "10", "11", "5", "15"], | ||
| "gasLimit": "50,000,000", | ||
| "gasPrice": "0" | ||
| }, | ||
| "expect": { | ||
| "out": [], | ||
| "status": "4", | ||
| "message": "str:current_in out of [min_in, max_in] range", | ||
| "gas": "*", | ||
| "refund": "*" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
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
40 changes: 40 additions & 0 deletions
40
contracts/feature-tests/basic-features/src/math_features.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,40 @@ | ||
| use multiversx_sc::imports::*; | ||
| use multiversx_sc::math; | ||
|
|
||
| #[multiversx_sc::module] | ||
| pub trait MathFeatures { | ||
| #[endpoint] | ||
| fn math_weighted_average( | ||
| &self, | ||
| first_value: BigUint, | ||
| first_weight: BigUint, | ||
| second_value: BigUint, | ||
| second_weight: BigUint, | ||
| ) -> BigUint { | ||
| math::weighted_average(first_value, first_weight, second_value, second_weight) | ||
| } | ||
|
|
||
| #[endpoint] | ||
| fn math_weighted_average_round_up( | ||
| &self, | ||
| first_value: BigUint, | ||
| first_weight: BigUint, | ||
| second_value: BigUint, | ||
| second_weight: BigUint, | ||
| ) -> BigUint { | ||
| math::weighted_average_round_up(first_value, first_weight, second_value, second_weight) | ||
| } | ||
|
|
||
| #[endpoint] | ||
| fn math_linear_interpolation( | ||
| &self, | ||
| min_in: BigUint, | ||
| max_in: BigUint, | ||
| current_in: BigUint, | ||
| min_out: BigUint, | ||
| max_out: BigUint, | ||
| ) -> BigUint { | ||
| math::linear_interpolation(min_in, max_in, current_in, min_out, max_out) | ||
| .unwrap_or_else(|_| sc_panic!("current_in out of [min_in, max_in] range")) | ||
| } | ||
| } |
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
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,7 @@ | ||
| /// Only used internally for computing logarithms for ManagedDecimal and BigUint. | ||
| pub(crate) mod internal_logarithm_i64; | ||
| pub mod linear_interpolation; | ||
| pub mod weighted_average; | ||
andrei-marinica marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| pub use linear_interpolation::{LinearInterpolationInvalidValuesError, linear_interpolation}; | ||
| pub use weighted_average::{weighted_average, weighted_average_round_up}; | ||
File renamed without changes.
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,40 @@ | ||
| use core::ops::{Add, Div, Mul, Sub}; | ||
|
|
||
| /// Error returned when `current_in` is outside the `[min_in, max_in]` range. | ||
| #[derive(Debug)] | ||
| pub struct LinearInterpolationInvalidValuesError; | ||
|
|
||
| /// Computes a linearly interpolated output value for a given input within a known range. | ||
| /// | ||
| /// Given an input range `[min_in, max_in]` and a corresponding output range `[min_out, max_out]`, | ||
| /// maps `current_in` proportionally to its position in the output range. | ||
| /// | ||
| /// Formula: | ||
| /// ```text | ||
| /// out = (min_out * (max_in - current_in) + max_out * (current_in - min_in)) / (max_in - min_in) | ||
| /// ``` | ||
| /// | ||
| /// Returns [`Err(LinearInterpolationInvalidValuesError)`] if `current_in` is outside `[min_in, max_in]`. | ||
| /// | ||
| /// See also: <https://en.wikipedia.org/wiki/Linear_interpolation> | ||
| pub fn linear_interpolation<T>( | ||
| min_in: T, | ||
| max_in: T, | ||
| current_in: T, | ||
| min_out: T, | ||
| max_out: T, | ||
| ) -> Result<T, LinearInterpolationInvalidValuesError> | ||
| where | ||
| T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + PartialOrd + Clone, | ||
| { | ||
| if current_in < min_in || current_in > max_in { | ||
| return Err(LinearInterpolationInvalidValuesError); | ||
| } | ||
|
|
||
| let min_out_weighted = min_out * (max_in.clone() - current_in.clone()); | ||
| let max_out_weighted = max_out * (current_in - min_in.clone()); | ||
| let in_diff = max_in - min_in; | ||
|
|
||
| let result = (min_out_weighted + max_out_weighted) / in_diff; | ||
andrei-marinica marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Ok(result) | ||
| } | ||
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,39 @@ | ||
| use core::ops::{Add, Div, Mul, Sub}; | ||
|
|
||
| /// Computes the weighted average of two values. | ||
| /// | ||
| /// Returns `(first_value * first_weight + second_value * second_weight) / (first_weight + second_weight)`. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics on division by zero if both weights are zero. | ||
| pub fn weighted_average<T>(first_value: T, first_weight: T, second_value: T, second_weight: T) -> T | ||
| where | ||
| T: Add<Output = T> + Mul<Output = T> + Div<Output = T> + Clone, | ||
| { | ||
| let weight_sum = first_weight.clone() + second_weight.clone(); | ||
| let weighted_sum = first_value * first_weight + second_value * second_weight; | ||
| weighted_sum / weight_sum | ||
| } | ||
|
|
||
| /// Computes the weighted average of two values, rounded up (ceiling division). | ||
| /// | ||
| /// Equivalent to [`weighted_average`], but rounds the result up instead of truncating: | ||
| /// `(weighted_sum + weight_sum - 1) / weight_sum`. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics on division by zero if both weights are zero. | ||
| pub fn weighted_average_round_up<T>( | ||
| first_value: T, | ||
| first_weight: T, | ||
| second_value: T, | ||
| second_weight: T, | ||
| ) -> T | ||
| where | ||
| T: Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Clone + From<u32>, | ||
| { | ||
| let weight_sum = first_weight.clone() + second_weight.clone(); | ||
| let weighted_sum = first_value * first_weight + second_value * second_weight; | ||
| (weighted_sum + weight_sum.clone() - T::from(1u32)) / weight_sum | ||
| } |
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
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.