github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/misc/tour/solutions/loops.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" 12 ) 13 14 const delta = 1e-10 15 16 func Sqrt(x float64) float64 { 17 z := x 18 for { 19 n := z - (z*z-x)/(2*z) 20 if math.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 := Sqrt(x), math.Sqrt(x) 31 fmt.Println(mine, theirs, mine-theirs) 32 }