Skip to content

Instantly share code, notes, and snippets.

@ton-katsu
Created April 15, 2014 09:42
Show Gist options
  • Save ton-katsu/10718240 to your computer and use it in GitHub Desktop.
Save ton-katsu/10718240 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math"
)
func Sqrt(x float64) float64 {
z := float64(1)
for i := 0; i < 10; i++ {
//fmt.Println(i)
z2 := math.Float64bits(z)
z = z - ((math.Pow(z, 2) - x) / (2 * z))
fmt.Println(math.Float64bits(z))
if z2 - math.Float64bits(z) <= 1 {
return z
}
}
return z
}
func main() {
v := 2.0
fmt.Println(Sqrt(v))
fmt.Println(math.Sqrt(v))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment