Created
April 10, 2017 10:48
-
-
Save zainfathoni/e39649e6b52a2a474f7212635624800a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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