This code takes advantage of IEEE format of floating point numbers to get a close approximation. Then two iterations of Newton's method get about 12 digits of accuracy. Don
float sqrt(float z) { union { int tmp; float f; } u; u.f = z; u.tmp -= 1<<23; u.tmp >>= 1; u.tmp += 1<<29; u.f = u.f-(u.f*u.f-z)/(2.0*u.f); return u.f-(u.f*u.f-z)/(2.0*u.f); } On Aug 30, 1:07 am, Raghavan <its...@gmail.com> wrote: > how to design this logic effectively? > > double squareRoot(int num){ > > } > > -- > Thanks and Regards, > Raghavan KL -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.