Your expression is (effectively) ((z*z-x)/2)*z. Use parentheses to group the 2*z term: (z*z-x)/(2*z).
- elias On Friday, April 14, 2017 at 2:30:33 PM UTC+2, ksam...@redhat.com wrote: > > Hello, > > Am a newbie to golang, and was trying myself with golang tour. > > > I was trying https://tour.golang.org/flowcontrol/8 and below is my code > snippet for the same > > package main > > import ( > "fmt" > "math" > ) > > func Sqrt(x float64) float64 { > z := float64(1) > > for i := 0; i < 10; i++ { > //z = z - (z*z-x)/2*z -- this does not work why ??? > z = z - (float64(z*z)-float64(x))/float64(2*z) > } > return float64(z) > } > > func main() { > fmt.Printf("Ans: %0.2f \n", Sqrt(4)) > fmt.Printf("math.Sqrt: %0.2f", math.Sqrt(4)) > } > > > I assume that all data is handled as float64 only, if you see the > commented codes not seem to work especially for simple Sqrt(4) i get an > answer of 1.76 instead of 2 ;) .. but after i did the casting to > float64(..) it seem to work right.. > > I am not able to understand why .. > > any thoughts ? > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.