Skip to content

Instantly share code, notes, and snippets.

@slawosz
Created July 1, 2013 17:32
Show Gist options
  • Save slawosz/5902860 to your computer and use it in GitHub Desktop.
Save slawosz/5902860 to your computer and use it in GitHub Desktop.
cube root in Go
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