| 1 | /* sys/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 | #include "gsl__config.h" |
| 20 | #include <math.h> |
| 21 | #include "gsl_pow_int.h" |
| 22 | |
| 23 | #ifndef HIDE_INLINE_STATIC |
| 24 | double gsl_pow_2(const double x) { return x*x; } |
| 25 | double gsl_pow_3(const double x) { return x*x*x; } |
| 26 | double gsl_pow_4(const double x) { double x2 = x*x; return x2*x2; } |
| 27 | double gsl_pow_5(const double x) { double x2 = x*x; return x2*x2*x; } |
| 28 | double gsl_pow_6(const double x) { double x2 = x*x; return x2*x2*x2; } |
| 29 | double gsl_pow_7(const double x) { double x3 = x*x*x; return x3*x3*x; } |
| 30 | double gsl_pow_8(const double x) { double x2 = x*x; double x4 = x2*x2; return x4*x4; } |
| 31 | double gsl_pow_9(const double x) { double x3 = x*x*x; return x3*x3*x3; } |
| 32 | #endif |
| 33 | |
| 34 | double gsl_pow_int(double x, int n) |
| 35 | { |
| 36 | double value = 1.0; |
| 37 | |
| 38 | if(n < 0) { |
| 39 | x = 1.0/x; |
| 40 | n = -n; |
| 41 | } |
| 42 | |
| 43 | /* repeated squaring method |
| 44 | * returns 0.0^0 = 1.0, so continuous in x |
| 45 | */ |
| 46 | do { |
| 47 | if(n & 1) value *= x; /* for n odd */ |
| 48 | n >>= 1; |
| 49 | x *= x; |
| 50 | } while (n); |
| 51 | |
| 52 | return value; |
| 53 | } |
| 54 |