hypot.c 639 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. /* $Id$ */
  8. double
  9. hypot(x,y)
  10. double x,y;
  11. {
  12. /* Computes sqrt(x*x+y*y), avoiding overflow */
  13. extern double sqrt();
  14. if (x < 0) x = -x;
  15. if (y < 0) y = -y;
  16. if (x > y) {
  17. double t = y;
  18. y = x;
  19. x = t;
  20. }
  21. /* sqrt(x*x+y*y) = sqrt(y*y*(x*x/(y*y)+1.0)) = y*sqrt(x*x/(y*y)+1.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(p_compl)
  30. struct complex p_compl;
  31. {
  32. return hypot(p_compl.r, p_compl.i);
  33. }