| 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 | |
| 20 | /* Author: G. Jungman */ |
| 21 | |
| 22 | #include "gsl__config.h" |
| 23 | #include "gsl_math.h" |
| 24 | #include "gsl_errno.h" |
| 25 | #include "gsl_sf_pow_int.h" |
| 26 | |
| 27 | |
| 28 | /*-*-*-*-*-*-*-*-*-*-*-* Functions w/ Error handling *-*-*-*-*-*-*-*-*-*-*-*/ |
| 29 | |
| 30 | int gsl_sf_pow_int_e(double x, int n, gsl_sf_result * result) |
| 31 | { |
| 32 | double value = 1.0; |
| 33 | int count = 0; |
| 34 | |
| 35 | /* CHECK_POINTER(result) */ |
| 36 | |
| 37 | |
| 38 | if(n < 0) { |
| 39 | n = -n; |
| 40 | |
| 41 | if(x == 0.0) { |
| 42 | double u = 1.0 / x; |
| 43 | result->val = (n % 2) ? u : (u * u) ; /* correct sign of infinity */ |
| 44 | result->err = GSL_POSINF; |
| 45 | GSL_ERROR ("overflow", GSL_EOVRFLW); |
| 46 | } |
| 47 | |
| 48 | x = 1.0/x; |
| 49 | } |
| 50 | |
| 51 | /* repeated squaring method |
| 52 | * returns 0.0^0 = 1.0, so continuous in x |
| 53 | */ |
| 54 | do { |
| 55 | if(GSL_IS_ODD(n)) value *= x; |
| 56 | n >>= 1; |
| 57 | x *= x; |
| 58 | ++count; |
| 59 | } while (n); |
| 60 | |
| 61 | result->val = value; |
| 62 | result->err = 2.0 * GSL_DBL_EPSILON * (count + 1.0) * fabs(value); |
| 63 | |
| 64 | return GSL_SUCCESS; |
| 65 | } |
| 66 | |
| 67 | /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/ |
| 68 | |
| 69 | #include "gsl_specfunc__eval.h" |
| 70 | |
| 71 | double gsl_sf_pow_int(const double x, const int n) |
| 72 | { |
| 73 | EVAL_RESULT(gsl_sf_pow_int_e(x, n, &result)); |
| 74 | } |