Created
January 31, 2014 20:46
-
-
Save ciaranarcher/8742792 to your computer and use it in GitHub Desktop.
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) (z float64) { | |
z = 1.0 | |
for i := 0; i < 100000000; i++ { | |
prev:= z | |
z = z - ((z * z - x) / 2 * z) | |
if diff := math.Abs(prev - z); diff < 0.0001 { | |
fmt.Println("good enough!", i, diff) | |
break; | |
} | |
} | |
return | |
} | |
func main() { | |
fmt.Println("per Newton:", Sqrt(2)) | |
fmt.Println("per math lib:", math.Sqrt(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment