added cmplx cube example
This commit is contained in:
parent
9f62f4ed0c
commit
e26ef31903
23
exercises/04-cube/go.go
Normal file
23
exercises/04-cube/go.go
Normal 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))
|
||||
}
|
Loading…
Reference in New Issue
Block a user