Skip to content

Instantly share code, notes, and snippets.

@vly
Created April 21, 2013 02:12
Show Gist options
  • Save vly/5428186 to your computer and use it in GitHub Desktop.
Save vly/5428186 to your computer and use it in GitHub Desktop.
Golang tour #47
package main
import ("fmt"
"math/cmplx"
)
func Cbrt(x complex128) complex128 {
guess := x/2
// assumes stability after 20 iterations
for i:=0;i<20;i++ {
guess = guess-(cmplx.Pow(guess, 3) -x)/(3*cmplx.Pow(guess,2))
}
return guess
}
func main() {
fmt.Println("My func: ", Cbrt(2))
fmt.Println("Builtin: ", cmplx.Pow(2, 1.0/3.0))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment