c_sqrt.c 605 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include "f2c.h"
  2. #ifdef KR_headers
  3. extern double sqrt(), f__cabs();
  4. VOID c_sqrt(r, z) complex *r, *z;
  5. #else
  6. #undef abs
  7. #include "math.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. extern double f__cabs(double, double);
  12. void c_sqrt(complex *r, complex *z)
  13. #endif
  14. {
  15. double mag, t;
  16. double zi = z->i, zr = z->r;
  17. if( (mag = f__cabs(zr, zi)) == 0.)
  18. r->r = r->i = 0.;
  19. else if(zr > 0)
  20. {
  21. r->r = t = sqrt(0.5 * (mag + zr) );
  22. t = zi / t;
  23. r->i = 0.5 * t;
  24. }
  25. else
  26. {
  27. t = sqrt(0.5 * (mag - zr) );
  28. if(zi < 0)
  29. t = -t;
  30. r->i = t;
  31. t = zi / t;
  32. r->r = 0.5 * t;
  33. }
  34. }
  35. #ifdef __cplusplus
  36. }
  37. #endif