gsl_specfunc__pow_int.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* specfunc/pow_int.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_pow_int.h"
  24. /*-*-*-*-*-*-*-*-*-*-*-* Functions w/ Error handling *-*-*-*-*-*-*-*-*-*-*-*/
  25. int gsl_sf_pow_int_e(double x, int n, gsl_sf_result * result)
  26. {
  27. double value = 1.0;
  28. int count = 0;
  29. /* CHECK_POINTER(result) */
  30. if(n < 0) {
  31. n = -n;
  32. if(x == 0.0) {
  33. double u = 1.0 / x;
  34. result->val = (n % 2) ? u : (u * u) ; /* correct sign of infinity */
  35. result->err = GSL_POSINF;
  36. GSL_ERROR ("overflow", GSL_EOVRFLW);
  37. }
  38. x = 1.0/x;
  39. }
  40. /* repeated squaring method
  41. * returns 0.0^0 = 1.0, so continuous in x
  42. */
  43. do {
  44. if(GSL_IS_ODD(n)) value *= x;
  45. n >>= 1;
  46. x *= x;
  47. ++count;
  48. } while (n);
  49. result->val = value;
  50. result->err = 2.0 * GSL_DBL_EPSILON * (count + 1.0) * fabs(value);
  51. return GSL_SUCCESS;
  52. }
  53. /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
  54. #include "gsl_specfunc__eval.h"
  55. double gsl_sf_pow_int(const double x, const int n)
  56. {
  57. EVAL_RESULT(gsl_sf_pow_int_e(x, n, &result));
  58. }