From 21716644d44669356e7169bdbafd1cc67dd61b2e Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Sun, 20 Dec 2020 10:41:59 -0800 Subject: [PATCH] =?UTF-8?q?types:=20unconditionally=20divide=20by=202=20be?= =?UTF-8?q?cause=20of=20integer=20truncation=20=E2=8C=8Ax/2=E2=8C=8B=20(#8?= =?UTF-8?q?202)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 #7924 --- types/decimal.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/types/decimal.go b/types/decimal.go index 17b53ac56b..bf93f43c3f 100644 --- a/types/decimal.go +++ b/types/decimal.go @@ -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) }