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

increase precision by doing wmul first #47

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

cupOJoseph
Copy link

potential loss of precision

For example, for an ETH/HAI price of approximately 3000*1e18, the ideal calculation for the inverted price should preserve as much precision as possible:
10^18 x 10^18/ (3000 x 10^18) = 333 333 333 333 333.333333...
However, the current implementation first performs the division, which truncates the result and leads to a loss of decimal precision.
Therefore it will it will use this 333 333 333 333 333 instead.

By changing the order of operations theres more precision in the calculation

Comment on lines 67 to 72
_priceSourceValue = WAD.wdiv(_priceSourceValue);
_result = WAD.wmul(_denominationPriceSourceValue).wdiv(_priceSourceValue);
} else {
_result = _priceSourceValue.wmul(_denominationPriceSourceValue);
}

_result = _priceSourceValue.wmul(_denominationPriceSourceValue);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is changing the overall behaviour... _result is being then redeclared..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh true thats actually a mistake. The final _result declaration should have been removed.

@cupOJoseph
Copy link
Author

Sorry, missed deleting that line.

Its now logically the same as before, but since wmul happens before wdiv the precision will be better.

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

Successfully merging this pull request may close these issues.

2 participants