Skip to content

Instantly share code, notes, and snippets.

@Medeah
Created March 10, 2013 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Medeah/5128961 to your computer and use it in GitHub Desktop.
Save Medeah/5128961 to your computer and use it in GitHub Desktop.
A Tour of Go 47: Advanced Exercise: Complex cube roots
package main
import (
"fmt"
"math/cmplx"
)
func Cbrt(x complex128) complex128 {
z := complex(1.0, 0)
for cmplx.Abs(z*z*z-x) > 1e-9 {
z = z - (z*z*z-x)/(3*z*z)
}
return z
}
func main() {
test := complex(2, 1)
fmt.Println(Cbrt(test))
fmt.Println(cmplx.Pow(test, 1.0/3))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment