Last active
March 10, 2024 09:36
-
-
Save Megaprog/4a7816c7c1ccb06d1d9a8a89efd3cc34 to your computer and use it in GitHub Desktop.
Sqrt by Newton's method https://en.wikipedia.org/wiki/Newton%27s_method
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 { | |
z := x / 2 | |
epsilon := 0.001 | |
for { | |
delta := (z*z - x) / (2*z) | |
fmt.Println(delta) | |
z -= delta | |
if math.Abs(delta) < epsilon { | |
break | |
} | |
} | |
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