Skip to content

Instantly share code, notes, and snippets.

@athom
Created July 16, 2013 13:48
Show Gist options
  • Save athom/6008849 to your computer and use it in GitHub Desktop.
Save athom/6008849 to your computer and use it in GitHub Desktop.
cube root iterate
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