blob: d4dc0e255e373e15d2471adf889c840f8b5b33c2 [file] [log] [blame]
package main
import (
"fmt"
"math/cmplx"
)
const delta = 1e-10
func Cbrt(x complex128) complex128 {
z := x
for {
n := z - (z*z*z-x)/(3*z*z)
if cmplx.Abs(n-z) < delta {
break
}
z = n
}
return z
}
func main() {
const x = 2
mine, theirs := Cbrt(x), cmplx.Pow(x, 1./3.)
fmt.Println(mine, theirs, mine-theirs)
}