1
0
Fork 0

added cmplx cube example

This commit is contained in:
Stephen M. McQuay 2012-08-02 17:48:46 -06:00
parent 9f62f4ed0c
commit e26ef31903
1 changed files with 23 additions and 0 deletions

23
exercises/04-cube/go.go Normal file
View File

@ -0,0 +1,23 @@
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))
}