Skip to content

Commit

Permalink
types: unconditionally divide by 2 because of integer truncation ⌊x/2⌋ (
Browse files Browse the repository at this point in the history
cosmos#8202)

Simplify the code in Dec.Power to unconditionally divide by 2, since:
    x /= 2
is the same result for both even and odd integers, and produces the
floor of the result of x/2 -> ⌊x/2⌋:
    99/2 ~> 49.5 ⌊49.5⌋ = 49
    98/2 ~> 49.0 ⌊49.0⌋ = 49
aka "integer truncation".

Fixes cosmos#7924
  • Loading branch information
odeke-em authored Dec 20, 2020
1 parent 25bb17d commit 2171664
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,10 @@ func (d Dec) Power(power uint64) Dec {
tmp := OneDec()

for i := power; i > 1; {
if i%2 == 0 {
i /= 2
} else {
if i%2 != 0 {
tmp = tmp.Mul(d)
i = (i - 1) / 2
}
i /= 2
d = d.Mul(d)
}

Expand Down

0 comments on commit 2171664

Please sign in to comment.