FWMath.h

Go to the documentation of this file.
00001 /* -*- mode:c++ -*- ********************************************************
00002  * file:        FWMath.h
00003  *
00004  * author:      Christian Frank
00005  *
00006  * copyright:   (C) 2004 Telecommunication Networks Group (TKN) at
00007  *              Technische Universitaet Berlin, Germany.
00008  *
00009  *              This program is free software; you can redistribute it
00010  *              and/or modify it under the terms of the GNU General Public
00011  *              License as published by the Free Software Foundation; either
00012  *              version 2 of the License, or (at your option) any later
00013  *              version.
00014  *              For further information see file COPYING
00015  *              in the top level directory
00016  ***************************************************************************
00017  * part of:     framework implementation developed by tkn
00018  **************************************************************************/
00019 
00026 #ifndef FWMATH_H
00027 #define FWMATH_H
00028 
00029 #include <cmath>
00030 
00031 
00032 #ifndef M_PI
00033 
00035 #define M_PI 3.14159265358979323846
00036 #endif
00037 
00038 #ifndef M_SQRT2
00039 
00041 #define M_SQRT2 1.41421356237309504880
00042 #endif
00043 
00044 #ifndef EPSILON
00045 
00047 #define EPSILON 0.00001
00048 #endif
00049 
00050 /* Provide function substitutes for Win32 architectures. */
00051 /*
00052 #ifdef _WIN32
00053 #ifndef _MINGW
00054 #include <float.h>
00055 #define finite  _finite
00056 #define isnan _isnan
00057 #define erf(X)  FWMath::erf(X)
00058 #define erfc(X) FWMath::erfc(X)
00059 #endif
00060 #endif
00061 */
00062 
00063 #ifdef __APPLE__
00064 #define isnan(x) ((x) != (x))
00065 #endif
00066 
00076 class FWMath
00077 {
00078 public:
00079 
00083     static double mod(double dividend, double divisor) {
00084         double i;
00085         return modf(dividend/divisor, &i)*divisor;
00086     }
00087 
00091     static double div(double dividend, double divisor) {
00092         double i;
00093         double f;
00094         f=modf(dividend/divisor, &i);
00095         return i;
00096     }
00097 
00103     static double modulo(double a, double n) {
00104     return (a - n * floor(a/n));
00105     }
00106 
00112     static bool close(double one, double two) {
00113         return fabs(one-two)<EPSILON;
00114     }
00115 
00120     static int stepfunction(double i) { return (i>EPSILON) ? 1 : close(i,0) ? 0 :-1; };
00121 
00122 
00126     static int sign(double i) { return (i>=0)? 1 : -1; };
00127 
00131     static int round(double d) { return (int)(ceil(d-0.5)); }
00132 
00136     static double floorToZero(double d) { return (d >= 0.0)? floor(d) : ceil(d); }
00137 
00141     static double max(double a, double b) { return (a<b)? b : a; }
00142 
00146     static double min(double a, double b) { return (a>b)? b : a; }
00147 
00151     static double dBm2mW(double dBm){
00152         return pow(10.0, dBm/10.0);
00153     }
00154 
00158   static double mW2dBm(double mW) { return (10 * log10(mW)); }
00159 
00164     static double torDist(double x1, double x2, double y1, double y2) {
00165         return (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2);
00166     }
00167 
00168   //TODO: resolve "extra qualification"-error for "erf" and "erfc" with mingw
00172   static double erfc(double x) {
00173     double t, u, y;
00174 
00175     if (x <= -6.0)
00176       return 2.0;
00177     if (x >= 6.0)
00178       return 0.0;
00179 
00180     t = 3.97886080735226 / (fabs(x) + 3.97886080735226);
00181     u = t - 0.5;
00182     y = (((((((((0.00127109764952614092 * u + 1.19314022838340944e-4) * u -
00183       0.003963850973605135) * u - 8.70779635317295828e-4) * u +
00184       0.00773672528313526668) * u + 0.00383335126264887303) * u -
00185       0.0127223813782122755) * u - 0.0133823644533460069) * u +
00186       0.0161315329733252248) * u + 0.0390976845588484035) * u +
00187       0.00249367200053503304;
00188     y = ((((((((((((y * u - 0.0838864557023001992) * u -
00189       0.119463959964325415) * u + 0.0166207924969367356) * u +
00190       0.357524274449531043) * u + 0.805276408752910567) * u +
00191       1.18902982909273333) * u + 1.37040217682338167) * u +
00192       1.31314653831023098) * u + 1.07925515155856677) * u +
00193       0.774368199119538609) * u + 0.490165080585318424) * u +
00194       0.275374741597376782) * t * exp(-x * x);
00195 
00196     return x < 0.0 ? 2.0 - y : y;
00197   }
00198 
00202   //static double erf(double x) { return 1.0 - erfc(x); }
00203 
00204 };
00205 
00206 #endif