Created
August 20, 2015 23:43
-
-
Save thenormalsquid/95b5726a0d7ebab86223 to your computer and use it in GitHub Desktop.
Newton approx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math" | |
) | |
func Sqrt(x float64) float64 { | |
delta, z := float64(1), float64(1) | |
for delta > 1e-9 { | |
prev := z | |
z = z - (math.Pow(z, 2) - x) / (2 * z) | |
delta = math.Abs(z - prev) | |
} | |
fmt.Println(math.Sqrt(x)) | |
return z | |
} | |
func main() { | |
fmt.Println(Sqrt(4)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment