Skip to content

Instantly share code, notes, and snippets.

@zainfathoni
Created April 10, 2017 10:48
Show Gist options
  • Save zainfathoni/e39649e6b52a2a474f7212635624800a to your computer and use it in GitHub Desktop.
Save zainfathoni/e39649e6b52a2a474f7212635624800a to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
const DELTA = 0.00000000001
const INITIAL_Z = 100.0
func Sqrt(x float64) (z float64) {
z = INITIAL_Z
step := func() float64 {
return z - (z*z - x) / (2 * z)
}
for zz := step(); math.Abs(zz - z) > DELTA
{
z = zz
zz = step()
}
return
}
func main() {
fmt.Println(Sqrt(-5))
fmt.Println(math.Sqrt(-500))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment