Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

MathFunctions.h (Maintainer: Marc Sturm)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework 
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Marc Sturm $
00025 // --------------------------------------------------------------------------
00026 
00027 
00028 #include <cmath>
00029 #include <OpenMS/CONCEPT/Types.h>
00030 // #include <iostream> // debugging
00031 
00032 #ifndef OPENMS_MATH_MISC_MATHFUNCTIONS_H
00033 #define OPENMS_MATH_MISC_MATHFUNCTIONS_H
00034 
00035 // MinGW32 lacks proper definition of isnan/isinf
00036 #ifndef isnan
00037           # define isnan(x) \
00038               (sizeof (x) == sizeof (long double) ? isnan_ld (x) \
00039                : sizeof (x) == sizeof (double) ? isnan_d (x) \
00040                : isnan_f (x))
00041           static inline int isnan_f  (float       x) { return x != x; }
00042           static inline int isnan_d  (double      x) { return x != x; }
00043           static inline int isnan_ld (long double x) { return x != x; }
00044           #endif
00045           
00046           #ifndef isinf
00047           # define isinf(x) \
00048               (sizeof (x) == sizeof (long double) ? isinf_ld (x) \
00049                : sizeof (x) == sizeof (double) ? isinf_d (x) \
00050                : isinf_f (x))
00051           static inline int isinf_f  (float       x) { return isnan (x - x); }
00052           static inline int isinf_d  (double      x) { return isnan (x - x); }
00053           static inline int isinf_ld (long double x) { return isnan (x - x); }
00054           #endif
00055 
00056 namespace OpenMS 
00057 {
00065   namespace Math
00066   {
00078     inline static double ceil_decimal(double x, int decPow)
00079     {
00080       return (ceil(x/pow(10,decPow)))*pow(10,decPow); // decimal shift right, ceiling, decimal shift left
00081     }
00082     
00093     inline static double round_decimal(double x, int decPow)  
00094     {
00095       if (x>0) return (floor(0.5+x/pow(10,decPow)))*pow(10,decPow);
00096       return -((floor(0.5+fabs(x)/pow(10,decPow)))*pow(10,decPow));
00097     }
00098     
00104     inline static double intervalTransformation(double x,double left1,double right1,double left2,double right2) 
00105     { 
00106       return left2 + (x - left1) * (right2 - left2) / (right1 - left1);
00107     }
00108   
00116     inline double linear2log(double x)
00117     {
00118       return log10(x+1); //+1 to avoid negative logarithms
00119     }
00120     
00128     inline double log2linear(double x)
00129     {
00130       return pow(10,x)-1;
00131     }
00132     
00138     inline bool isOdd(UInt x)
00139     {
00140       return ((x & 1)!=0);
00141     }
00142 
00143   } // namespace Math
00144 } // namespace OpenMS
00145 
00146 #endif // OPENMS_MATH_MISC_MATHFUNCTIONS_H

Generated Tue Apr 1 15:36:35 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1