diff options
Diffstat (limited to 'src/pkg/math/cbrt.go')
-rw-r--r-- | src/pkg/math/cbrt.go | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/src/pkg/math/cbrt.go b/src/pkg/math/cbrt.go index d2b7e910b..8c43f0afb 100644 --- a/src/pkg/math/cbrt.go +++ b/src/pkg/math/cbrt.go @@ -33,11 +33,9 @@ func Cbrt(x float64) float64 { C3 = 6.46502159e-02 C4 = 1.412333954e-01 ) - // TODO(rsc): Remove manual inlining of IsNaN, IsInf - // when compiler does it for us // special cases switch { - case x == 0 || x != x || x < -MaxFloat64 || x > MaxFloat64: // x == 0 || IsNaN(x) || IsInf(x, 0): + case x == 0 || IsNaN(x) || IsInf(x, 0): return x } sign := false @@ -45,22 +43,21 @@ func Cbrt(x float64) float64 { x = -x sign = true } - // Reduce argument - f, e := Frexp(x) + // Reduce argument and estimate cube root + f, e := Frexp(x) // 0.5 <= f < 1.0 m := e % 3 if m > 0 { m -= 3 e -= m // e is multiple of 3 } - f = Ldexp(f, m) // 0.125 <= f < 1.0 - - // Estimate cube root switch m { case 0: // 0.5 <= f < 1.0 f = A1*f + A2 - A3/(A4+f) - case -1: // 0.25 <= f < 0.5 + case -1: + f *= 0.5 // 0.25 <= f < 0.5 f = B1*f + B2 - B3/(B4+f) - default: // 0.125 <= f < 0.25 + default: // m == -2 + f *= 0.25 // 0.125 <= f < 0.25 f = C1*f + C2 - C3/(C4+f) } y := Ldexp(f, e/3) // e/3 = exponent of cube root |