github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/misc/tour/solutions/complexcube.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build ignore 6 7 package main 8 9 import ( 10 "fmt" 11 "math/cmplx" 12 ) 13 14 const delta = 1e-10 15 16 func Cbrt(x complex128) complex128 { 17 z := x 18 for { 19 n := z - (z*z*z-x)/(3*z*z) 20 if cmplx.Abs(n-z) < delta { 21 break 22 } 23 z = n 24 } 25 return z 26 } 27 28 func main() { 29 const x = 2 30 mine, theirs := Cbrt(x), cmplx.Pow(x, 1./3.) 31 fmt.Println(mine, theirs, mine-theirs) 32 }