Last active
April 19, 2021 10:16
-
-
Save tommylees112/a90c6c0eb4c6be6dae05ba34a3d453c4 to your computer and use it in GitHub Desktop.
Playing around with go following the tutorials here: https://tour.golang.org/welcome/3
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" | |
) | |
const INITIAL_Z = 1 | |
const DELTA = 1e-7 | |
// Newton's Method for Sqrt Algorithm | |
func Sqrt(x float64) float64 { | |
var z float64 = INITIAL_Z | |
var i int = 0 | |
// infinite loop | |
for { | |
delta := (z*z - x) / (2 * z) | |
z = z - delta | |
fmt.Printf("%v: %v \t(Delta: %v)\n", i, z, delta) | |
i += 1 | |
// absolute delta for comparison with DELTA | |
if delta < 0 { | |
delta = -delta | |
} | |
// stopping criterion | |
if delta < DELTA { | |
fmt.Println("Stopping Criterion Met") | |
return z | |
} | |
} | |
return z | |
} | |
func main() { | |
fmt.Println(Sqrt(64)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment