Created
February 14, 2017 21:39
-
-
Save randomradio/cebb8921698aa842bb8b6ffd9af62528 to your computer and use it in GitHub Desktop.
golang newton sqrt
This file contains 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 { | |
z := 1.0 | |
for { | |
tmp := z - (z*z-x)/(2*z) | |
fmt.Println(tmp) | |
if tmp == z || math.Abs(tmp)-z < 0.0000000000000001 { | |
z = tmp | |
break | |
} | |
z = tmp | |
} | |
return z | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment