Advertisement
KorSnack

Square Root

Apr 20th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.36 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. double sqrt(double x) {
  4.   long long i, r;
  5.   double x2=x*0.5, y=x;
  6.   i = *(long long*)&y;
  7.   i = 0x5fe6eb50c7b537a9 - (i>>1);
  8.   y = *(double*)&i;
  9.   for(r=0 ; r<10 ; r++) y = y * (1.5 - (x2*y*y));
  10.   return x * y;
  11. }
  12.  
  13. int main() {
  14.   double n;
  15.   while(1) {
  16.     scanf("%lf", &n);
  17.     printf("sqrt = %.10lf\n", sqrt(n));
  18.   }
  19.   return 0;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement