Skip to content

Commit 497d241

Browse files
kishtatixbradfitz
authored andcommitted
math: use Abs in Mod rather than if x < 0 { x = -x}
goos: linux goarch: amd64 pkg: math name old time/op new time/op delta Mod 64.7ns ± 2% 63.7ns ± 2% -1.52% (p=0.003 n=8+10) Change-Id: I851bec0fd6c223dab73e4a680b7393d49e81a0e8 Reviewed-on: https://go-review.googlesource.com/c/85095 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent f22c357 commit 497d241

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

src/math/mod.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,12 @@ func mod(x, y float64) float64 {
2424
if y == 0 || IsInf(x, 0) || IsNaN(x) || IsNaN(y) {
2525
return NaN()
2626
}
27-
if y < 0 {
28-
y = -y
29-
}
27+
y = Abs(y)
3028

3129
yfr, yexp := Frexp(y)
32-
sign := false
3330
r := x
3431
if x < 0 {
3532
r = -x
36-
sign = true
3733
}
3834

3935
for r >= y {
@@ -43,7 +39,7 @@ func mod(x, y float64) float64 {
4339
}
4440
r = r - Ldexp(y, rexp-yexp)
4541
}
46-
if sign {
42+
if x < 0 {
4743
r = -r
4844
}
4945
return r

0 commit comments

Comments
 (0)