24 lines
379 B
Go
24 lines
379 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math/cmplx"
|
|
)
|
|
|
|
const threshold = 1e-5
|
|
|
|
func Cbrt(x complex128) (z complex128) {
|
|
z = x
|
|
for delta, last := 1e9, z; delta > threshold; last = z {
|
|
z -= (z*z*z - x) / (3 * z * z)
|
|
delta = cmplx.Abs(z - last)
|
|
}
|
|
return
|
|
}
|
|
|
|
func main() {
|
|
n := 12392.13 + 1982i
|
|
fmt.Println("calculated ", Cbrt(n))
|
|
fmt.Println("expected ", cmplx.Pow(n, 1/3.0))
|
|
}
|