MOOSE - Multiscale Object Oriented Simulation Environment
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
numutil.h
Go to the documentation of this file.
1 /*******************************************************************
2  * File: numutil.h
3  * Description:
4  * Author: Subhasis Ray
5  * E-mail: ray dot subhasis at gmail dot com
6  * Created: 2007-11-02 11:47:21
7  ********************************************************************/
8 /**********************************************************************
9  ** This program is part of 'MOOSE', the
10  ** Messaging Object Oriented Simulation Environment,
11  ** also known as GENESIS 3 base code.
12  ** copyright (C) 2003-2005 Upinder S. Bhalla. and NCBS
13  ** It is made available under the terms of the
14  ** GNU General Public License version 2
15  ** See the file COPYING.LIB for the full notice.
16  **********************************************************************/
17 
18 #ifndef _NUMUTIL_H
19 #define _NUMUTIL_H
20 
21 #include <cmath>
22 #include <cfloat>
23 #include <limits>
24 
25 const int WORD_LENGTH = 32; // number of bits in a word - check for portability
26 const double LN2 = 0.69314718055994528622676;
27 const unsigned long LN2BYTES = 0xB1721814;
28 const double NATURAL_E = 2.718281828459045;
29 
30 //extern const double getMachineEpsilon();
31 //extern const double EPSILON;
32 
33 #ifndef M_PI
34 #define M_PI 3.14159265358979323846
35 #endif
36 
37 #ifndef M_E
38 #define M_E 2.7182818284590452353
39 #endif
40 
41 
45  template<class T>
46 bool isNaN( T value )
47 {
48  return value != value;
49 }
50 
51  template< typename T >
52 bool isInfinity( T value )
53 {
54  return value == std::numeric_limits< T >::infinity();
55 }
56 
66  template< class T >
67 bool isClose( T a, T b, T tolerance )
68 {
69  T epsilon = std::numeric_limits< T >::epsilon();
70 
71  if ( a == b )
72  return true;
73 
74  if ( a == 0 || b == 0 )
75  return ( fabs( a - b ) < tolerance * epsilon );
76 
77  return (
78  fabs( ( a - b ) / a ) < tolerance * epsilon
79  &&
80  fabs( ( a - b ) / b ) < tolerance * epsilon
81  );
82 }
83 
84 bool almostEqual(float x, float y, float epsilon = FLT_EPSILON);
85 bool almostEqual(double x, double y, double epsilon = DBL_EPSILON);
86 bool almostEqual(long double x, long double y, long double epsilon = LDBL_EPSILON);
87 
88 // round, isinf and isnan are not defined in VC++ or Borland C++
89 #if defined(__TURBOC__) || defined(__BORLANDC__) || defined(_MSC_VER)
90 #define isinf(param) !_finite(param)
91 #define isnan(param) _isnan(param)
92 #define round(param) floor(param+0.5)
93 #endif
94 #endif
uint32_t value
Definition: moosemodule.h:42
bool isClose(T a, T b, T tolerance)
Definition: numutil.h:67
const unsigned long LN2BYTES
Definition: numutil.h:27
const double LN2
Definition: numutil.h:26
bool isNaN(T value)
Definition: numutil.h:46
bool isInfinity(T value)
Definition: numutil.h:52
const double NATURAL_E
Definition: numutil.h:28
const int WORD_LENGTH
Definition: numutil.h:25
bool almostEqual(float x, float y, float epsilon=FLT_EPSILON)
Definition: numutil.cpp:27