Created
February 23, 2014 20:54
-
-
Save sighmin/9177130 to your computer and use it in GitHub Desktop.
Go tour "Cbrt" method: http://tour.golang.org
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 { | |
result := complex128(x) | |
for i := 0; i < 100; i++ { // To ensure high precision | |
result = result - ((cmplx.Pow(result, 3) - x)/(3 * cmplx.Pow(result, 2))) | |
} | |
return result | |
} | |
func main() { | |
fmt.Println("Actual :" , cmplx.Pow(9, 1.0/3.0)) | |
fmt.Println("Estimate :", Cbrt(9)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment