When doing binary/ternary search for double I prefer to do this:
hi = 1e100 lo = 0 for i = 0 to MAX_ITERATIONS --- ha = (hi+lo)/2 etc This way your loop is guaranteed to end after aconstant number of iterations. If you pick the constant well, you can also be sure about the error : hi/(2^MAX_ITERATIONS) . On May 21, 3:01 pm, bychance <[email protected]> wrote: > I uses these code to do binary search: > > l=0; > r=1e12; > while(fabs(l-r)>1e-6) > { > //some code > > } > > but it stucked with large data in my local PC. > I also doesn't work in Code Jam Africa and Arabia 2011 Online > Competition Problem C Radio Receiver. > > I saw the code from the first place of today's match. > He uses this line of code instead: > > REP(i,100){ > > means iterates for 100 times. > 1e15/2^100<1e-6, so it fit the precious. > > I'm wondering that is it caused by the precious of float number > operations? -- You received this message because you are subscribed to the Google Groups "google-codejam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
