Last active
November 23, 2019 10:11
-
-
Save andreasvirkus/e9f0a9a1b305a1b91cade1090d404d95 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 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