Skip to content

Instantly share code, notes, and snippets.

@shaneharter
Created May 13, 2013 18:21
Show Gist options
  • Save shaneharter/5570296 to your computer and use it in GitHub Desktop.
Save shaneharter/5570296 to your computer and use it in GitHub Desktop.
Gotour: Newtonian square roots in Go
package main
import (
"fmt"
"math"
)
const min_delta = 0.000000000001
func Sqrt(x float64) float64 {
z := 0.0
for value := 1.0; math.Abs(value-z) > min_delta; {
z = value
value = z - (z*z-x)/(2*z)
}
return z
}
func main() {
fmt.Println(Sqrt(34500000))
fmt.Println("---")
fmt.Println(math.Sqrt(34500000))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment