Skip to content

Instantly share code, notes, and snippets.

@phigasui
Last active October 10, 2017 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phigasui/4157832b747195ee232bd146d29cd5d3 to your computer and use it in GitHub Desktop.
Save phigasui/4157832b747195ee232bd146d29cd5d3 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) (i float64, z float64, loop_count int, err float64) {
z = x
i = x
loop_count = 0
var diff float64;
for {
diff = (z * z - x) / (2.0 * z)
if diff <= 1 / (20 ^ 9) {
err = math.Sqrt(x) - z
return
}
z -= diff
loop_count++
}
}
func main() {
for i:=10;i<13;i++ {
fmt.Println(Sqrt(float64(i)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment