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

math: support power operation with two real numbers (std::Decimal) #1857

Open
byeongsu-hong opened this issue Sep 4, 2023 · 6 comments
Open

Comments

@byeongsu-hong
Copy link

Is there any plans to support power operation between two Decimals? I'm currently developing the pool swap simulator on Osmosis, but there's some limitation of implementing "math stuffs" like pow operation.

@webmaster128
Copy link
Member

Which operation exactly do you need?

@byeongsu-hong
Copy link
Author

byeongsu-hong commented Sep 4, 2023

Which operation exactly do you need?

image

I need operation between Bt and Wt in a real number

@byeongsu-hong byeongsu-hong changed the title math: support power operation with two Decimals math: support power operation with two real numbers (std::Decimal) Sep 4, 2023
@webmaster128
Copy link
Member

I would not recommend doing that kind of math with cosmwasm_std::Decimal or cosmwasm_std::Decimal256 because they are both fixed point decimals. This means especially multiplications and powers can quickly lead to significant rounding errors. I'd look for a library specific for that kind of job and have a layer of converting inputs and outputs.

Real numbers could be implemented with either floats or floating point decimals.

Floats are currently disabled, but might be shipped as part of the next release if everything goes well: #1845. If that works out you can use native f32/f64.

Floating point decimals are like Decimal but the dot is moving instead of being always 18 positions left of the end. This allows very high values or very low values with much lower loss of precision. Some examples for that approach are:

@byeongsu-hong
Copy link
Author

byeongsu-hong commented Sep 4, 2023

rust-decimal which has impl Pow for Decimal (not used myself but work for Proof of Engagement)

I'm using this library to solve this problem, but it's not fit with the big numbers from Osmosis' weighted pool. I think it does not solve the issue that i surfaced even if cosmwasm supports f32/f64.

This means especially multiplications and powers can quickly lead to significant rounding errors

I'm totally agree with your opinion and this should not be applied on this library for same reason.

I found Osmosis team uses Taylor Expansion + big.Int to approximate pow operation. So there's two point to choose if i really want to use pow operation.

  1. Wait until Osmosis team opens query for using their math.
  2. Make my own math library with Uint256 or something

@webmaster128
Copy link
Member

webmaster128 commented Sep 4, 2023

What's a typical value range for those t, B and W? Maybe the 18 digit fixed point is good enough?

@byeongsu-hong
Copy link
Author

I'll let you know if i check the actual value of it

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

No branches or pull requests

2 participants