Skip to content

Instantly share code, notes, and snippets.

@meeech
Last active December 27, 2015 20:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save meeech/7385402 to your computer and use it in GitHub Desktop.
gotour ex 24
//Exercise 24 (23 in gotour local)
package main
import (
"fmt"
"math"
)
func Approx(x float64, z float64) float64 {
return z - (((z * z) - x) / (2 * z))
}
func Sqrt(x float64) float64 {
previous := 0.0
delta := 1.0
z := x
for delta > 0.005 {
z = Approx(x, z)
if previous > 0.0 {
delta = previous - z
}
previous = z
}
return z
}
func main() {
for i := 1; i < 11; i++ {
value := float64(i)
fmt.Println("Calculating Sqrt for ", value)
real := math.Sqrt(value)
approx := Sqrt(value)
fmt.Println("Real ", real)
fmt.Println("Approx ", approx)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment