Skip to content

Instantly share code, notes, and snippets.

@pstoll
Created November 18, 2012 21:43
Show Gist options
  • Select an option

  • Save pstoll/4107678 to your computer and use it in GitHub Desktop.

Select an option

Save pstoll/4107678 to your computer and use it in GitHub Desktop.
Cube root example in GoLang tour #47
package main
import "fmt"
import "math/cmplx"
func Cbrt(x complex128) complex128 {
z:=complex128(1.0)
min_delta := float64(0.00000000001)
delta := complex128(1.0)
var iter int
for iter = 0; cmplx.Abs(delta) > min_delta ; iter++{
delta = (z*z*z - x)/(3*z*z)
//fmt.Println(z,delta)
z = z - delta
}
fmt.Println("iterations: ", iter)
return z
}
func main() {
var val complex128 = 123123;
fmt.Println(Cbrt(val))
fmt.Println(cmplx.Pow(val,1.0/3.0))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment