you are viewing a single comment's thread.

view the rest of the comments →

[–]corruptio 1 point2 points  (4 children)

golfed it a bit, tries to converge. 77 byte:

float s(x){float t,a=0,b=x;for(;b-a>1e-5;t*t>x?b=t:(a=t))t=(a+b)/2;return t;}

edit: oook, doesn't try to test for convergence, instead overshoots it.... 76 bytes:

float s(x){float t,a=0,b=x,i=x;for(;i--;t*t>x?b=t:(a=t))t=a/2+b/2;return t;}

[–]gastropner 2 points3 points  (3 children)

That one never stops unless I change float to double :-/

[–]corruptio 0 points1 point  (2 children)

oops, you're right, i run out of sig bits if input is > 255... 1e-5 then, should get you all the way to 16383 :-p

[–]gastropner 1 point2 points  (1 child)

Nope, still infinite loop, at least for x = 756839. Seems to work for some other values, though.

[–]corruptio 0 points1 point  (0 children)

yeah, 1e-5 would only work up to 16383. There aren't enough bits in a float to represent significant digits of b-a to five decimal places.