Created
March 8, 2015 04:53
-
-
Save xiaokangwang/e0e63c09ecaf3d638175 to your computer and use it in GitHub Desktop.
Newton's method for sqrt()
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" | |
func main() { | |
i := 1 | |
var sqr, x, cm float64 | |
sqr = 16 | |
x = 1 | |
for i != 0 { | |
cm = ((x*x - sqr) / (x * sqr)) | |
x = x - cm | |
if cm < 1e-7 && -cm < 1e-7 { | |
i = 0 | |
} | |
} | |
fmt.Println(x) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment