[algogeeks] Re: Find square root a number

2011-08-30 Thread Dave
@Raghavan: What is wrong with return sqrt((double)num); Since most CPUs have square root instructions, and a square root takes about the same time as a division, how can you hope to be any faster with software? Dave On Aug 30, 1:07 am, Raghavan wrote: > how to design this logic effectively? >

Re: [algogeeks] Re: Find square root a number

2011-08-30 Thread SANDEEP CHUGH
@rajeev kumar in babylonian method.. initial approx shud be an over estimate of ur square root?? u taking it as 2.. isn't a wrong assumption? On Tue, Aug 30, 2011 at 9:36 PM, aditya kumar wrote: > let u have to find the square root of s . > a=sqrt(s) ; > a^2=s ; > 2(a^a)=s+a^2 ; > a=(s+a^2)/2

Re: [algogeeks] Re: Find square root a number

2011-08-30 Thread aditya kumar
let u have to find the square root of s . a=sqrt(s) ; a^2=s ; 2(a^a)=s+a^2 ; a=(s+a^2)/2a ; after 20th iteration you will get more approximated value . hopefully it will help .:) On Tue, Aug 30, 2011 at 9:14 PM, teja bala wrote: > @aditya kumar > Will u plz explain the logic involved here...? >

Re: [algogeeks] Re: Find square root a number

2011-08-30 Thread teja bala
@aditya kumar Will u plz explain the logic involved here...? On Tue, Aug 30, 2011 at 7:40 PM, aditya kumar wrote: > void getSquareRoot(float s) > { > float a=s; > int i=0; > for(i=0;i<20;i++) > { > a=(s+a*a)/(2*a); > } > printf("square root is %f",a); > } > > On Tue, Aug 30, 2011 at

[algogeeks] Re: Find square root a number

2011-08-30 Thread Don
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

Re: [algogeeks] Re: Find square root a number

2011-08-30 Thread aditya kumar
void getSquareRoot(float s) { float a=s; int i=0; for(i=0;i<20;i++) { a=(s+a*a)/(2*a); } printf("square root is %f",a); } On Tue, Aug 30, 2011 at 6:00 PM, Sanjay Rajpal wrote: > Binary Search kind of mathod is useful here : > > float SquareRoot(float n,float start,float end) > { > fl

Re: [algogeeks] Re: Find square root a number

2011-08-30 Thread Sanjay Rajpal
Binary Search kind of mathod is useful here : float SquareRoot(float n,float start,float end) { float s=(start+end)/2; if(n - sqr(s) < 0.001) && (n - sqr(s) > -0.001)) return (end+start)/2; else if(sqr(s) > n) return SquareRoot(n,0.0,s); else return SquareRoot(n,s,end); } Sanju :

Re: [algogeeks] Re: Find square root a number

2011-08-30 Thread Anup Ghatage
Anyone interested in laying out the trade off's of the Babylonian method vs Newton Raphson? -- Anup Ghatage -- 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

Re: [algogeeks] Re: Find square root a number

2011-08-30 Thread vaibhav shukla
newton raphson will do it.. :) On Tue, Aug 30, 2011 at 3:55 PM, UTKARSH SRIVASTAV wrote: > i don't whethe you have studied a subject cbnst from that use newton > raphson method > > > On Tue, Aug 30, 2011 at 2:39 AM, Ankuj Gupta wrote: > >> U can use binary search method >> >> On Aug 30, 1:56 pm,

Re: [algogeeks] Re: Find square root a number

2011-08-30 Thread UTKARSH SRIVASTAV
i don't whethe you have studied a subject cbnst from that use newton raphson method On Tue, Aug 30, 2011 at 2:39 AM, Ankuj Gupta wrote: > U can use binary search method > > On Aug 30, 1:56 pm, Rajeev Kumar wrote: > > use Babylonian method(Efficient) algrithm.. > > Refer : > http://e

[algogeeks] Re: Find square root a number

2011-08-30 Thread Ankuj Gupta
U can use binary search method On Aug 30, 1:56 pm, Rajeev Kumar wrote: > use Babylonian method(Efficient) algrithm.. > Refer > :http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylo... > > public *void* getSquareRoot(double s) { >   double Xn = 2.0; >   double lastXn