Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sqrt Algorithm in Chapter 1.2 #1150

Open
be1be1 opened this issue Aug 19, 2020 · 0 comments
Open

Sqrt Algorithm in Chapter 1.2 #1150

be1be1 opened this issue Aug 19, 2020 · 0 comments

Comments

@be1be1
Copy link

be1be1 commented Aug 19, 2020

In Chapter 1.2 $GOPATH and workspace, there is an algorithm describing a squre root math method writing in Golang.

// Source code of $GOPATH/src/mymath/sqrt.go
package mymath

func Sqrt(x float64) float64 {
	z := 0.0
	for i := 0; i < 1000; i++ {
		z -= (z*z - x) / (2 * x)
	}
	return z
}

In my point of view, this function intends to use newton method to get the approximate value of a squre root. However, if this is indeed using newton method, the method should be changed to:

// Source code of $GOPATH/src/mymath/sqrt.go
package mymath

func Sqrt(x float64) float64 {
	z := 100.0
	for i := 0; i < 1000; i++ {
		z -= (z*z - x) / (2 * z)
	}
	return z
}

Correct me if I am wrong. For reference: https://gist.github.com/abesto/3476594

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant