Skip to content

Instantly share code, notes, and snippets.

@pstoll
Created November 18, 2012 19:18
Show Gist options
  • Select an option

  • Save pstoll/4106979 to your computer and use it in GitHub Desktop.

Select an option

Save pstoll/4106979 to your computer and use it in GitHub Desktop.
Go sqrt example
// http://tour.golang.org/#45
// by Perry Stoll
package main
import (
"fmt"
"math"
)
func MySqrt(x float64) float64 {
z:=1.0
min_delta := 0.00000000001
delta := 1.0
i := 0
for ; math.Abs(delta) > min_delta ; i++{
delta = (z*z - x)/(2*z)
//fmt.Println(z,delta)
z = z - delta
}
fmt.Println("iterations: ", i)
return z
}
func main() {
val := 9987665555.0
fmt.Println(MySqrt(val))
fmt.Println(math.Sqrt(val))
}
@MannyIOI
Copy link
Copy Markdown

Can this function go a precision of min_delta =10 ^ -1000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment