A Tour of Go Advanced Exercise: Complex cube roots

时间:2023-02-14 06:06:09

Let's explore Go's built-in support for complex numbers via the complex64 and complex128 types. For cube roots, Newton's method amounts to repeating:

A Tour of Go  Advanced Exercise: Complex cube roots

Find the cube root of 2, just to make sure the algorithm works. There is a Pow function in the math/cmplx package.

package main

import "fmt"

func Cbrt(x complex128) complex128 {
z := complex128()
for i := ;i < ;i++{
z = z - (z*z*z -x)/(*z*z)
}
return z
} func main() {
fmt.Println(Cbrt())
}