gsl_sys__hypot.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* sys/hypot.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough, Patrick Alken
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <math.h>
  21. #include "gsl_math.h"
  22. double gsl_hypot (const double x, const double y);
  23. double gsl_hypot3 (const double x, const double y, const double z);
  24. double gsl_hypot (const double x, const double y)
  25. {
  26. double xabs = fabs(x) ;
  27. double yabs = fabs(y) ;
  28. double min, max;
  29. if (xabs < yabs) {
  30. min = xabs ;
  31. max = yabs ;
  32. } else {
  33. min = yabs ;
  34. max = xabs ;
  35. }
  36. if (min == 0)
  37. {
  38. return max ;
  39. }
  40. {
  41. double u = min / max ;
  42. return max * sqrt (1 + u * u) ;
  43. }
  44. }
  45. double
  46. gsl_hypot3(const double x, const double y, const double z)
  47. {
  48. double xabs = fabs(x);
  49. double yabs = fabs(y);
  50. double zabs = fabs(z);
  51. double w = GSL_MAX(xabs, GSL_MAX(yabs, zabs));
  52. if (w == 0.0)
  53. {
  54. return (0.0);
  55. }
  56. else
  57. {
  58. double r = w * sqrt((xabs / w) * (xabs / w) +
  59. (yabs / w) * (yabs / w) +
  60. (zabs / w) * (zabs / w));
  61. return r;
  62. }
  63. }