Skip to content

Instantly share code, notes, and snippets.

@brendanmckenzie
Created April 29, 2014 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendanmckenzie/11400011 to your computer and use it in GitHub Desktop.
Save brendanmckenzie/11400011 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := float64(1)
last_z := z
diff := -1.0
for diff == -1.0 || diff > 0.000001 {
last_z = z
z = z - ((z * z) - x) / (2 * z)
diff = z - last_z
if (diff < 0) { diff *= -1.0 }
}
return z
}
func main() {
x := 25.
fmt.Println("Sqrt()", Sqrt(x))
fmt.Println("math.Sqrt()", math.Sqrt(x))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment