Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Last active November 23, 2019 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasvirkus/e9f0a9a1b305a1b91cade1090d404d95 to your computer and use it in GitHub Desktop.
Save andreasvirkus/e9f0a9a1b305a1b91cade1090d404d95 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
const round = 1000000.0
func Sqrt(x float64) float64 {
guess := 1.0
for i, z := 1, 1.0; i < 10; i++ {
z -= (z*z - x) / (2 * z)
fmt.Println(z)
z = math.Round(z*round) / round
if math.Abs(guess-z) < 0.001 {
guess = z
break
}
guess = z
fmt.Println(guess)
}
return guess
}
func main() {
fmt.Println("Custom result:", Sqrt(3))
fmt.Println("Math lib result:", math.Sqrt(3))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment