hypot.c 652 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. * Author: Ceriel J.H. Jacobs
  6. */
  7. #include <math.h>
  8. /* $Id$ */
  9. double
  10. hypot(double x,double y)
  11. {
  12. /* Computes sqrt(x*x+y*y), avoiding overflow */
  13. if (x < 0) x = -x;
  14. if (y < 0) y = -y;
  15. if (x > y) {
  16. double t = y;
  17. y = x;
  18. x = t;
  19. }
  20. /* sqrt(x*x+y*y) = sqrt(y*y*(x*x/(y*y)+1.0)) = y*sqrt(x*x/(y*y)+1.0) */
  21. if (y == 0.0) return 0.0;
  22. x /= y;
  23. return y*sqrt(x*x+1.0);
  24. }
  25. struct complex {
  26. double r,i;
  27. };
  28. double
  29. cabs(struct complex p_compl)
  30. {
  31. return hypot(p_compl.r, p_compl.i);
  32. }