gsl_specfunc__elementary.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* specfunc/elementary.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  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. /* Author: G. Jungman */
  20. #include "gsl__config.h"
  21. #include "gsl_math.h"
  22. #include "gsl_errno.h"
  23. #include "gsl_sf_elementary.h"
  24. #include "gsl_specfunc__error.h"
  25. #include "gsl_specfunc__check.h"
  26. int
  27. gsl_sf_multiply_e(const double x, const double y, gsl_sf_result * result)
  28. {
  29. const double ax = fabs(x);
  30. const double ay = fabs(y);
  31. if(x == 0.0 || y == 0.0) {
  32. /* It is necessary to eliminate this immediately.
  33. */
  34. result->val = 0.0;
  35. result->err = 0.0;
  36. return GSL_SUCCESS;
  37. }
  38. else if((ax <= 1.0 && ay >= 1.0) || (ay <= 1.0 && ax >= 1.0)) {
  39. /* Straddling 1.0 is always safe.
  40. */
  41. result->val = x*y;
  42. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  43. return GSL_SUCCESS;
  44. }
  45. else {
  46. const double f = 1.0 - 2.0 * GSL_DBL_EPSILON;
  47. const double min = GSL_MIN_DBL(fabs(x), fabs(y));
  48. const double max = GSL_MAX_DBL(fabs(x), fabs(y));
  49. if(max < 0.9 * GSL_SQRT_DBL_MAX || min < (f * DBL_MAX)/max) {
  50. result->val = GSL_COERCE_DBL(x*y);
  51. result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
  52. CHECK_UNDERFLOW(result);
  53. return GSL_SUCCESS;
  54. }
  55. else {
  56. OVERFLOW_ERROR(result);
  57. }
  58. }
  59. }
  60. int
  61. gsl_sf_multiply_err_e(const double x, const double dx,
  62. const double y, const double dy,
  63. gsl_sf_result * result)
  64. {
  65. int status = gsl_sf_multiply_e(x, y, result);
  66. result->err += fabs(dx*y) + fabs(dy*x);
  67. return status;
  68. }
  69. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  70. #include "gsl_specfunc__eval.h"
  71. double gsl_sf_multiply(const double x, const double y)
  72. {
  73. EVAL_RESULT(gsl_sf_multiply_e(x, y, &result));
  74. }