Created
November 18, 2012 21:43
-
-
Save pstoll/4107678 to your computer and use it in GitHub Desktop.
Cube root example in GoLang tour #47
This file contains hidden or 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" | |
| 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