]> Cypherpunks repositories - gostls13.git/commitdiff
math: use Abs rather than if x < 0 { x = -x }
authorThanabodee Charoenpiriyakij <wingyminus@gmail.com>
Sun, 17 Dec 2017 09:11:14 +0000 (16:11 +0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 13 Feb 2018 20:12:23 +0000 (20:12 +0000)
This is the benchmark result base on darwin with amd64 architecture:

name     old time/op  new time/op  delta
Cos      10.2ns ± 2%  10.3ns ± 3%  +1.18%  (p=0.032 n=10+10)
Cosh     25.3ns ± 3%  24.6ns ± 2%  -3.00%  (p=0.000 n=10+10)
Hypot    6.40ns ± 2%  6.19ns ± 3%  -3.36%  (p=0.000 n=10+10)
HypotGo  7.16ns ± 3%  6.54ns ± 2%  -8.66%  (p=0.000 n=10+10)
J0       66.0ns ± 2%  63.7ns ± 1%  -3.42%  (p=0.000 n=9+10)

Fixes #21812

Change-Id: I2b88fbdfc250cd548f8f08b44ce2eb172dcacf43
Reviewed-on: https://go-review.googlesource.com/84437
Reviewed-by: Giovanni Bajo <rasky@develer.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Giovanni Bajo <rasky@develer.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/math/hypot.go
src/math/j0.go
src/math/sin.go
src/math/sinh.go

index 2087cb05b36b361265913b5ecfbd1eda9e1c4b04..c7f19d4d38f5a0539dc80b66ffb088e6777726cb 100644 (file)
@@ -26,12 +26,7 @@ func hypot(p, q float64) float64 {
        case IsNaN(p) || IsNaN(q):
                return NaN()
        }
-       if p < 0 {
-               p = -p
-       }
-       if q < 0 {
-               q = -q
-       }
+       p, q = Abs(p), Abs(q)
        if p < q {
                p, q = q, p
        }
index fe26791175117a048096a123445c54d7b7c0f296..5523fc34a001f1543f2b1fd47d61bb61dd79a7d8 100644 (file)
@@ -99,9 +99,7 @@ func J0(x float64) float64 {
                return 1
        }
 
-       if x < 0 {
-               x = -x
-       }
+       x = Abs(x)
        if x >= 2 {
                s, c := Sincos(x)
                ss := s - c
index 7a75a5f63d4e2987d335e146832a56687a3aed0b..929cac34ecaa6f2912b4cbf231075d051fa181ca 100644 (file)
@@ -131,9 +131,7 @@ func cos(x float64) float64 {
 
        // make argument positive
        sign := false
-       if x < 0 {
-               x = -x
-       }
+       x = Abs(x)
 
        j := int64(x * M4PI) // integer part of x/(Pi/4), as integer for tests on the phase angle
        y := float64(j)      // integer part of x/(Pi/4), as float
index 2bdd7b12057c973270ec6844e826caefad6c8721..30bbc0661ec949a5dbf7d97ae7e48457bc5ada5d 100644 (file)
@@ -71,9 +71,7 @@ func sinh(x float64) float64 {
 func Cosh(x float64) float64
 
 func cosh(x float64) float64 {
-       if x < 0 {
-               x = -x
-       }
+       x = Abs(x)
        if x > 21 {
                return Exp(x) / 2
        }