]> Cypherpunks repositories - gostls13.git/commitdiff
math: use Abs in Pow rather than if x < 0 { x = -x }
authorPlekhanov Maxim <kishtatix@gmail.com>
Fri, 22 Dec 2017 23:25:35 +0000 (02:25 +0300)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 4 Oct 2018 17:33:04 +0000 (17:33 +0000)
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 <bradfitz@golang.org>
src/math/pow.go

index 336193bce12bf194eeb1152c620063911c7c85e6..2219a906b8d0851831bf407f24c52072df141b76 100644 (file)
@@ -83,13 +83,7 @@ func pow(x, y float64) float64 {
                return 1 / Sqrt(x)
        }
 
-       absy := y
-       flip := false
-       if absy < 0 {
-               absy = -absy
-               flip = true
-       }
-       yi, yf := Modf(absy)
+       yi, yf := Modf(Abs(y))
        if yf != 0 && x < 0 {
                return NaN()
        }
@@ -147,9 +141,9 @@ func pow(x, y float64) float64 {
        }
 
        // ans = a1*2**ae
-       // if flip { ans = 1 / ans }
+       // if y < 0 { ans = 1 / ans }
        // but in the opposite order
-       if flip {
+       if y < 0 {
                a1 = 1 / a1
                ae = -ae
        }