URL
https://opencores.org/ocsvn/openrisc/openrisc/trunk
Subversion Repositories openrisc
[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [math/] [cbrt.go] - Rev 747
Compare with Previous | Blame | View Log
// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package math/*The algorithm is based in part on "Optimal Partitioning ofNewton's Method for Calculating Roots", by Gunter Meinardusand G. D. Taylor, Mathematics of Computation © 1980 AmericanMathematical Society.(http://www.jstor.org/stable/2006387?seq=9, accessed 11-Feb-2010)*/// Cbrt returns the cube root of its argument.//// Special cases are:// Cbrt(±0) = ±0// Cbrt(±Inf) = ±Inf// Cbrt(NaN) = NaNfunc Cbrt(x float64) float64 {const (A1 = 1.662848358e-01A2 = 1.096040958e+00A3 = 4.105032829e-01A4 = 5.649335816e-01B1 = 2.639607233e-01B2 = 8.699282849e-01B3 = 1.629083358e-01B4 = 2.824667908e-01C1 = 4.190115298e-01C2 = 6.904625373e-01C3 = 6.46502159e-02C4 = 1.412333954e-01)// special casesswitch {case x == 0 || IsNaN(x) || IsInf(x, 0):return x}sign := falseif x < 0 {x = -xsign = true}// Reduce argument and estimate cube rootf, e := Frexp(x) // 0.5 <= f < 1.0m := e % 3if m > 0 {m -= 3e -= m // e is multiple of 3}switch m {case 0: // 0.5 <= f < 1.0f = A1*f + A2 - A3/(A4+f)case -1:f *= 0.5 // 0.25 <= f < 0.5f = B1*f + B2 - B3/(B4+f)default: // m == -2f *= 0.25 // 0.125 <= f < 0.25f = C1*f + C2 - C3/(C4+f)}y := Ldexp(f, e/3) // e/3 = exponent of cube root// Iterates := y * y * yt := s + xy *= (t + x) / (s + t)// Reiterates = (y*y*y - x) / xy -= y * (((14.0/81.0)*s-(2.0/9.0))*s + (1.0 / 3.0)) * sif sign {y = -y}return y}
