Created
April 21, 2013 02:12
-
-
Save vly/5428186 to your computer and use it in GitHub Desktop.
Golang tour #47
This file contains 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" | |
) | |
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