z_sqrt.c 581 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "f2c.h"
  2. #ifdef KR_headers
  3. double sqrt(), f__cabs();
  4. VOID z_sqrt(r, z) doublecomplex *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 z_sqrt(doublecomplex *r, doublecomplex *z)
  13. #endif
  14. {
  15. double mag, zi = z->i, zr = z->r;
  16. if( (mag = f__cabs(zr, zi)) == 0.)
  17. r->r = r->i = 0.;
  18. else if(zr > 0)
  19. {
  20. r->r = sqrt(0.5 * (mag + zr) );
  21. r->i = zi / r->r / 2;
  22. }
  23. else
  24. {
  25. r->i = sqrt(0.5 * (mag - zr) );
  26. if(zi < 0)
  27. r->i = - r->i;
  28. r->r = zi / r->i / 2;
  29. }
  30. }
  31. #ifdef __cplusplus
  32. }
  33. #endif