Skip to content

Commit 47e71f3

Browse files
kishtatixbradfitz
authored andcommitted
math: use Abs in Pow rather than if x < 0 { x = -x }
name old time/op new time/op delta PowInt 55.7ns ± 1% 53.4ns ± 2% -4.15% (p=0.000 n=9+9) PowFrac 133ns ± 1% 133ns ± 2% ~ (p=0.587 n=8+9) Change-Id: Ica0f4c2cbd554f2195c6d1762ed26742ff8e3924 Reviewed-on: https://go-review.googlesource.com/c/85375 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 497d241 commit 47e71f3

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

src/math/pow.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,7 @@ func pow(x, y float64) float64 {
8383
return 1 / Sqrt(x)
8484
}
8585

86-
absy := y
87-
flip := false
88-
if absy < 0 {
89-
absy = -absy
90-
flip = true
91-
}
92-
yi, yf := Modf(absy)
86+
yi, yf := Modf(Abs(y))
9387
if yf != 0 && x < 0 {
9488
return NaN()
9589
}
@@ -147,9 +141,9 @@ func pow(x, y float64) float64 {
147141
}
148142

149143
// ans = a1*2**ae
150-
// if flip { ans = 1 / ans }
144+
// if y < 0 { ans = 1 / ans }
151145
// but in the opposite order
152-
if flip {
146+
if y < 0 {
153147
a1 = 1 / a1
154148
ae = -ae
155149
}

0 commit comments

Comments
 (0)