Created
July 13, 2012 21:08
-
-
Save freeformz/3107457 to your computer and use it in GitHub Desktop.
My GoTour Exercise #44 (http://tour.golang.org/#44) solution
This file contains 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" | |
) | |
func sqrt(target, guess float64) float64 { | |
return guess - (math.Pow(guess, 2)-target)/(2*guess) | |
} | |
func inequiv(a, b float64) bool { | |
return math.Nextafter(a, b) != b | |
} | |
func Sqrt(x float64) float64 { | |
curr, guess := 0.0, 10.0 | |
for inequiv(curr, guess) { | |
curr = sqrt(x, guess) | |
guess = (curr + guess) / 2 | |
} | |
return guess | |
} | |
func main() { | |
fmt.Println(Sqrt(2)) | |
fmt.Println(math.Sqrt(2)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tabs!