Skip to content

Instantly share code, notes, and snippets.

@yosmoc
Created March 8, 2014 03:41
Show Gist options
  • Save yosmoc/9424989 to your computer and use it in GitHub Desktop.
Save yosmoc/9424989 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := 1.0
for {
old := z
z = z - ((z*z - x) / 2 * z)
if diff := math.Abs(old - z); diff < 0.0001 {
return z
}
}
}
func main() {
fmt.Println("newton: ", Sqrt(2))
fmt.Println("math: ", math.Sqrt(2))
fmt.Println("diff:", math.Sqrt(2)-Sqrt(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment