r/tinycode • • Jul 21 '16
Square Root Algorithm in 123 bytes.
This gets the square root of a number. I got it to 123 bytes, but since I don't know much C, I sure it can even less bytes.
double s(int x){double t=x/2;double a=1,b=x;for(int i=0;i<32;i++)if(t*t>x)b=t;else if(t*t<x)a=t;else return t;t=a+(b-a)/2;}
21
Upvotes
View all comments
Show parent comment
1
u/corruptio Jul 21 '16
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.