-
Notifications
You must be signed in to change notification settings - Fork 608
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
Fix spotprice sigfig rounding for small spot prices #1699
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package osmomath | ||
|
||
import sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
var pointOne = sdk.OneDec().QuoInt64(10) | ||
|
||
func SigFigRound(d sdk.Dec, tenToSigFig sdk.Int) sdk.Dec { | ||
// for d > .1, we do round(d * 10^sigfig) / 10^sigfig | ||
// for k, where 10^k*d > 1 && 10^{k-1}*d < .1, we do: | ||
// (round(10^k * d * 10^sigfig) / (10^sigfig * 10^k) | ||
// take note of floor div, vs normal div | ||
k := uint64(0) | ||
dTimesK := d | ||
for ; dTimesK.LT(pointOne); k += 1 { | ||
dTimesK.MulInt64Mut(10) | ||
} | ||
// d * 10^k * 10^sigfig | ||
dkSigFig := dTimesK.MulInt(tenToSigFig) | ||
numerator := dkSigFig.RoundInt().ToDec() | ||
denominator := tenToSigFig | ||
if k != 0 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if k == 0, then Please consider removing it. I found translating the comment above directly to code to be very helpful. At first, this check got me confused. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I guess thats fair, don't need to optimize on two multiplications right now / there are better ways to do that. |
||
tenToK := sdk.NewInt(10).ToDec().Power(k) | ||
denominator = denominator.Mul(tenToK.TruncateInt()) | ||
} | ||
return numerator.QuoInt(denominator) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's create an issue to add unit tests directly for this function