mysqrt.cxx 504 B

1234567891011121314151617181920212223242526272829
  1. #include <stdio.h>
  2. #include "MathFunctions.h"
  3. // a hack square root calculation using simple operations
  4. double mysqrt(double x)
  5. {
  6. if (x <= 0)
  7. {
  8. return 0;
  9. }
  10. double result;
  11. double delta;
  12. result = x;
  13. // do ten iterations
  14. int i;
  15. for (i = 0; i < 10; ++i)
  16. {
  17. if (result <= 0)
  18. {
  19. result = 0.1;
  20. }
  21. delta = x - (result*result);
  22. result = result + 0.5*delta/result;
  23. fprintf(stdout,"Computing sqrt of %g to be %g\n",x,result);
  24. }
  25. return result;
  26. }