Skip to content

Instantly share code, notes, and snippets.

@okwrtdsh
Created March 21, 2016 12:47
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 okwrtdsh/1cea81d56d4e9562f246 to your computer and use it in GitHub Desktop.
Save okwrtdsh/1cea81d56d4e9562f246 to your computer and use it in GitHub Desktop.
package main

import (
	"fmt"
	"math"
)

func Sqrt(x float64) float64 {
	z := float64(x)
	for i := 0; i < 100; i++ {
		z = z - (z*z-x)/(2*z)
	}
	return z
}

func main() {
	fmt.Println(Sqrt(2))
	fmt.Println(math.Sqrt(2))
	fmt.Println(math.Sqrt(2) - Sqrt(2))
}
1.414213562373095
1.4142135623730951
2.220446049250313e-16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment