INET Framework for OMNeT++/OMNEST
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 
00020 #ifndef FWMATH_H
00021 #define FWMATH_H
00022 
00023 //
00024 // Support functions for mathematical operations
00025 //
00026 
00027 #include <math.h>
00028 
00029 
00030 /* windows math.h doesn't define the PI variable so we have to do it
00031    by hand*/
00032 #ifndef M_PI
00033 #define M_PI 3.14159265358979323846
00034 #endif
00035 
00036 /* Constant for comparing doubles. Two doubles at most epsilon apart
00037    are declared equal.*/
00038 #ifndef EPSILON
00039 #define EPSILON 0.001
00040 #endif
00041 
00042 
00052 class INET_API FWMath {
00053 
00054  public:
00055 
00059   static double mod(double dividend, double divisor) {
00060       double i;
00061       return modf(dividend/divisor, &i)*divisor;
00062   }
00063 
00067   static double div(double dividend, double divisor) {
00068       double i;
00069       modf(dividend/divisor, &i);
00070       return i;
00071   }
00072 
00078   static bool close(double one, double two) {
00079       return fabs(one-two)<EPSILON;
00080   }
00081 
00086   static int stepfunction(double i) { return (i>EPSILON) ? 1 : close(i,0) ? 0 :-1; };
00087 
00088 
00092   static int sign(double i) { return (i>=0)? 1 : -1; };
00093 
00097   static int round(double d) { return (int)(ceil(d-0.5)); }
00098 
00102   static double max(double a, double b) { return (a<b)? b : a; }
00103 
00107   static double dBm2mW(double dBm){
00108       return pow(10.0, dBm/10.0);
00109   }
00110 
00111 };
00112 
00113 #endif