go-tour/exercise-loops-and-functions.go

22 lines
506 B
Go

package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
var p float64
for z := 1.0; z != p && math.Abs(z - p) > 0.000000000000001; z -= (z * z - x) / (2 * z) {
p = z
}
return p
}
func main() {
fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(333), Sqrt(333))
fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(33), Sqrt(33))
fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(17), Sqrt(17))
fmt.Printf("Stdlib: %g, Mine: %g\n", math.Sqrt(4), Sqrt(4))
}