Created
July 1, 2013 17:32
-
-
Save slawosz/5902860 to your computer and use it in GitHub Desktop.
cube root in Go
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" | |
"math/cmplx" | |
) | |
func Cbrt(x complex128) complex128 { | |
z := x | |
// fmt.Println(cmplx.Abs(cmplx.Pow(z, 2) - x)) | |
// fmt.Println(cmplx.Abs(cmplx.Pow(10, 10))) | |
for cmplx.Abs(cmplx.Pow(z, 3) - x) > cmplx.Abs(cmplx.Pow(10, -8)) { | |
fmt.Println(cmplx.Pow(z, 3)) | |
z = z - (cmplx.Pow(z, 3) - x)/(3*(cmplx.Pow(z, 2))) | |
} | |
return z | |
} | |
func main() { | |
fmt.Println(Cbrt(2)) | |
fmt.Println(cmplx.Pow(10, -10)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment