Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := float64(1)
last_z := z
diff := -1.0
for diff == -1.0 || diff > 0.000001 {
last_z = z
z = z - ((z * z) - x) / (2 * z)
diff = z - last_z
if (diff < 0) { diff *= -1.0 }
}
return z
}
func main() {
x := 25.
fmt.Println("Sqrt()", Sqrt(x))
fmt.Println("math.Sqrt()", math.Sqrt(x))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.