Skip to content

Instantly share code, notes, and snippets.

@poppen
Last active April 7, 2016 04:37
Show Gist options
  • Save poppen/978a38bb34d731fef60643daf573c4bf to your computer and use it in GitHub Desktop.
Save poppen/978a38bb34d731fef60643daf573c4bf to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func Sqrt_10(x float64) float64 {
i := 0
z := 1.0
if i < 9 {
z = z - ((z*z - x) / 2 * z)
}
return z
}
func Sqrt(x float64) float64{
z := 1.0
var prev_z float64
for {
prev_z = z
z = z - ((z*z - x) / 2 * z)
if math.Abs(prev_z - z) < 0.0001 {
return z
}
}
}
func main() {
fmt.Printf("math.Sqrt: %g\n", math.Sqrt(2))
fmt.Printf("Sqrt_10: %g\n", Sqrt_10(2))
fmt.Printf("Sqrt: %g\n", Sqrt(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment