Skip to content

Instantly share code, notes, and snippets.

@sighmin
Created February 23, 2014 20:54
Show Gist options
  • Save sighmin/9177130 to your computer and use it in GitHub Desktop.
Save sighmin/9177130 to your computer and use it in GitHub Desktop.
Go tour "Cbrt" method: http://tour.golang.org
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