Skip to content

Instantly share code, notes, and snippets.

@freeformz
Created July 13, 2012 21:08
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 freeformz/3107457 to your computer and use it in GitHub Desktop.
Save freeformz/3107457 to your computer and use it in GitHub Desktop.
My GoTour Exercise #44 (http://tour.golang.org/#44) solution
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))
}
@hgmnz
Copy link

hgmnz commented Jul 13, 2012

tabs!

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