Skip to content

Instantly share code, notes, and snippets.

@tae0y
Created May 11, 2019 04:18
Show Gist options
  • Save tae0y/e51af57debda31c3e4d6378d1c690381 to your computer and use it in GitHub Desktop.
Save tae0y/e51af57debda31c3e4d6378d1c690381 to your computer and use it in GitHub Desktop.
gotour_loop&function
package main
import (
"fmt"
"math"
)
/**
math.Sqrt값과 비교해 0.5이내의 오차범위내 반복
연산후 정확한 값이 나오면 z가 0이 되는 것으로 종료조건을 둘 수도 있다
*/
func Sqrt(z float64) (float64, int) {
cmp := float64(math.Sqrt(z))
cnt := 0
for (z-cmp) > 0.5 || (z-cmp) < -0.5 {
z = z - (z * z - 2) / (2 * z)
cnt++
}
return z, cnt
}
func main() {
fmt.Println(Sqrt(16))
}
@tae0y
Copy link
Author

tae0y commented May 11, 2019

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