Created
July 16, 2013 13:48
-
-
Save athom/6008849 to your computer and use it in GitHub Desktop.
cube root iterate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/cmplx" | |
"math" | |
) | |
func equal(x, y complex128) bool { | |
delta := 0.0001 | |
r1, t1 := cmplx.Polar(x) | |
r2, t2 := cmplx.Polar(y) | |
return math.Abs(r1-r2) < delta && math.Abs(t1-t2) < delta | |
} | |
func Cbrt(x complex128) complex128 { | |
z := x | |
for { | |
i := z - (cmplx.Pow(z, 3)-x)/(3*cmplx.Pow(z,2)) | |
if equal(i, z) { | |
return i | |
} | |
z = i | |
} | |
} | |
func main() { | |
fmt.Println(Cbrt(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment