Created
December 25, 2014 19:19
-
-
Save rndD/ce001e7c7de14caef981 to your computer and use it in GitHub Desktop.
Exercise: Loops and Functions in go tour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
const MAX_DELTA = 0.01 | |
func isEnough(d float64) bool { | |
if d < 0 { | |
d = -d | |
} | |
return d < MAX_DELTA | |
} | |
func Sqrt(x float64, z float64) float64 { | |
var tmp float64 | |
for { | |
tmp = z - (z * z - x) / 2 * z | |
if d := tmp - z; isEnough(d) { | |
return tmp | |
} | |
z = tmp | |
} | |
} | |
func main() { | |
fmt.Println(Sqrt(9, 1)) | |
fmt.Println(math.Sqrt(9)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment