Skip to content

Instantly share code, notes, and snippets.

@rndD
Created December 25, 2014 19:19
Show Gist options
  • Save rndD/ce001e7c7de14caef981 to your computer and use it in GitHub Desktop.
Save rndD/ce001e7c7de14caef981 to your computer and use it in GitHub Desktop.
Exercise: Loops and Functions in go tour
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