00001 /*************************************************************************** 00002 ipower.h - description 00003 ------------------- 00004 begin : Sun Feb 16 2003 00005 copyright : (C) 2003 by Moritz Franosch 00006 email : mail@Franosch.org 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #ifndef IPOWER_H 00019 #define IPOWER_H 00020 00021 00022 inline double ipow(double x, int n) { 00023 if (n<0) { 00024 x=1/x; 00025 n=-n; 00026 } 00027 double r=1; 00028 while (n>0) { 00029 if (n & 1>0) { 00030 r*=x; 00031 } 00032 n >>= 1; 00033 x*=x; 00034 } 00035 return r; 00036 } 00037 00038 00039 template<int n> double pow(double x) { 00040 return ( n>=0 ? ( pow<n/2>(x*x) * (n % 2>0 ? x : 1) ) : pow<-n>(1/x) ); 00041 } 00042 00043 00044 template<> inline double pow<0>(double) { 00045 return 1; 00046 } 00047 00048 00049 #endif // IPOWER_H