00001 /*************************************************************************** 00002 * Copyright (C) 2008 by Konstantin L. Metlov * 00003 * metlov@kinetic.ac.donetsk.ua * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program; if not, write to the * 00017 * Free Software Foundation, Inc., * 00018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00019 ***************************************************************************/ 00020 00024 #ifndef __SPECFUN_H__ 00025 #define __SPECFUN_H__ 00026 00027 #include <cstdlib> 00028 #include <complex> 00029 00033 typedef std::complex<double> dcmplx; 00034 00049 dcmplx ibeta(dcmplx z, dcmplx a, dcmplx b); 00050 00061 dcmplx lngamma(dcmplx z); 00062 00083 int GaussJacobiRule(double* x, double* w, const int Q, const double alpha, 00084 const double beta); 00085 00096 bool tol_eq(double a, double b); 00097 00105 bool tol_eq(dcmplx a, dcmplx b); 00106 00112 inline double HeavisideTheta(double x) { 00113 return x>0?1.0:0.0; 00114 }; 00115 00116 inline int sign(double x) { 00117 if (x==0.0) return 0; 00118 return x>0?1:-1; 00119 }; 00120 00121 /* define portable isnan() and isinf() as per autoconf recommendations */ 00122 00123 #ifndef isnan 00124 # define isnan(x) \ 00125 (sizeof (x) == sizeof (long double) ? isnan_ld (x) \ 00126 : sizeof (x) == sizeof (double) ? isnan_d (x) \ 00127 : isnan_f (x)) 00128 static inline int isnan_f (float x) { 00129 return x != x; 00130 } 00131 static inline int isnan_d (double x) { 00132 return x != x; 00133 } 00134 static inline int isnan_ld (long double x) { 00135 return x != x; 00136 } 00137 #endif 00138 00139 #ifndef isinf 00140 # define isinf(x) \ 00141 (sizeof (x) == sizeof (long double) ? isinf_ld (x) \ 00142 : sizeof (x) == sizeof (double) ? isinf_d (x) \ 00143 : isinf_f (x)) 00144 static inline int isinf_f (float x) { 00145 return isnan (x - x); 00146 } 00147 static inline int isinf_d (double x) { 00148 return isnan (x - x); 00149 } 00150 static inline int isinf_ld (long double x) { 00151 return isnan (x - x); 00152 } 00153 #endif 00154 00155 // tolerant comparison of floats from the paper "Comparing floating point numbers" by 00156 // Bruce Dawson. 00157 // http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm 00158 // Support functions and conditional compilation directives for the 00159 // master AlmostEqual function. 00160 #define INFINITYCHECK 00161 #define NANCHECK 00162 #define SIGNCHECK 00163 00164 inline bool IsInfinite(float A) 00165 { 00166 const int kInfAsInt = 0x7F800000; 00167 00168 // An infinity has an exponent of 255 (shift left 23 positions) and 00169 // a zero mantissa. There are two infinities - positive and negative. 00170 if ((*(int*)&A & 0x7FFFFFFF) == kInfAsInt) 00171 return true; 00172 return false; 00173 } 00174 00175 inline bool IsNan(float A) 00176 { 00177 // A NAN has an exponent of 255 (shifted left 23 positions) and 00178 // a non-zero mantissa. 00179 int exp = *(int*)&A & 0x7F800000; 00180 int mantissa = *(int*)&A & 0x007FFFFF; 00181 if (exp == 0x7F800000 && mantissa != 0) 00182 return true; 00183 return false; 00184 } 00185 00186 inline int Sign(float A) 00187 { 00188 // The sign bit of a number is the high bit. 00189 return (*(int*)&A) & 0x80000000; 00190 } 00191 00192 // This is the 'final' version of the AlmostEqualUlps function. 00193 // The optional checks are included for completeness, but in many 00194 // cases they are not necessary, or even not desirable. 00195 inline bool AlmostEqualUlps(float A, float B, int maxUlps) { 00196 // There are several optional checks that you can do, depending 00197 // on what behavior you want from your floating point comparisons. 00198 // These checks should not be necessary and they are included 00199 // mainly for completeness. 00200 00201 #ifdef INFINITYCHECK 00202 // If A or B are infinity (positive or negative) then 00203 // only return true if they are exactly equal to each other - 00204 // that is, if they are both infinities of the same sign. 00205 // This check is only needed if you will be generating 00206 // infinities and you don't want them 'close' to numbers 00207 // near FLT_MAX. 00208 if (IsInfinite(A) || IsInfinite(B)) 00209 return A == B; 00210 #endif 00211 00212 #ifdef NANCHECK 00213 // If A or B are a NAN, return false. NANs are equal to nothing, 00214 // not even themselves. 00215 // This check is only needed if you will be generating NANs 00216 // and you use a maxUlps greater than 4 million or you want to 00217 // ensure that a NAN does not equal itself. 00218 if (IsNan(A) || IsNan(B)) 00219 return false; 00220 #endif 00221 00222 #ifdef SIGNCHECK 00223 // After adjusting floats so their representations are lexicographically 00224 // ordered as twos-complement integers a very small positive number 00225 // will compare as 'close' to a very small negative number. If this is 00226 // not desireable, and if you are on a platform that supports 00227 // subnormals (which is the only place the problem can show up) then 00228 // you need this check. 00229 // The check for A == B is because zero and negative zero have different 00230 // signs but are equal to each other. 00231 if (Sign(A) != Sign(B)) 00232 return A == B; 00233 #endif 00234 int aInt = *(int*)&A; 00235 // Make aInt lexicographically ordered as a twos-complement int 00236 if (aInt < 0) 00237 aInt = 0x80000000 - aInt; 00238 // Make bInt lexicographically ordered as a twos-complement int 00239 int bInt = *(int*)&B; 00240 if (bInt < 0) 00241 bInt = 0x80000000 - bInt; 00242 00243 // Now we can compare aInt and bInt to find out how far apart A and B 00244 // are. 00245 int intDiff = std::abs(aInt - bInt); 00246 if (intDiff <= maxUlps) 00247 return true; 00248 return false; 00249 }; 00250 00251 #endif /* __SPECFUN_H__ */
1.6.3