-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Added nth root function to sdk.Decimal type #5447
Conversation
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.
Power
and NewIntFromUint64
look good. I have a question on the semantics of ApproxRoot
.
// approximate answer. It returns -(sqrt(abs(d)) if input is negative. | ||
func (d Dec) ApproxSqrt() Dec { | ||
// approximate answer. It returns -(sqrt(abs(d)) if input is negative. | ||
func (d Dec) ApproxRoot(root uint64) (guess Dec, err error) { |
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.
Where does the panic stem from? I believe all Dec/Int methods panic when too large. Why have this method deviated from that pattern?
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.
It comes from L353 where we take previous guess
, to the power of root-1
. This causes the big Int to overflow. I feel an error is more proper here, than a panic, because its harder to predict when it will happen. Like in normal arithmetic operations, you know that your inputs are in the danger zone. It's a bit harder to predict here, so I feel throwing an error is more reasonable than panicking.
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.
I see.
It comes from L353 where we take previous guess, to the power of root-1. This causes the big Int to overflow.
Is there a way to prevent a guess or short-circuit when we know it'd overflow? This way we can avoid the error return and panic.
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.
You mean try to catch the panic, and then restart with a different initial guess? Couldn't that possibly get into a loop that never ends then
Co-Authored-By: Kevin Davis <karzak@users.noreply.github.com>
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.
Minor changes. Pending changelog entry too
Co-Authored-By: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
CI needs to pass. See the follow-up question on avoiding a panic/error. |
Co-Authored-By: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
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.
ACK
If there already exists a pending log from the previous PR, just update that. Otherwise, add a new entry 👍 |
Codecov Report
@@ Coverage Diff @@
## master #5447 +/- ##
==========================================
+ Coverage 54.41% 54.47% +0.05%
==========================================
Files 313 313
Lines 18854 18892 +38
==========================================
+ Hits 10259 10291 +32
- Misses 7808 7810 +2
- Partials 787 791 +4
|
bump @fedekunze |
Parameterization of #5219 to find any nth root of an sdk.Dec for which n is a non-negative integer.
Closes: #XXX
Description
For contributor use:
docs/
) or specification (x/<module>/spec/
)godoc
comments.Unreleased
section inCHANGELOG.md
Files changed
in the Github PR explorerFor admin use:
WIP
,R4R
,docs
, etc)