MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
numutil.cpp
Go to the documentation of this file.
1 /*******************************************************************
2  * File: numutil.cpp
3  * Description:
4  * Author: Subhasis Ray
5  * E-mail: ray dot subhasis at gmail dot com
6  * Created: 2007-11-02 11:46:40
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-2013 Upinder S. Bhalla. and NCBS
13  ** It is made available under the terms of the
14  ** GNU Lesser General Public License version 2.1
15  ** See the file COPYING.LIB for the full notice.
16  **********************************************************************/
17 
18 #ifndef _NUMUTIL_CPP
19 #define _NUMUTIL_CPP
20 
21 #ifdef ENABLE_CPP11
22 #include <ctgmath>
23 #else /* ----- not ENABLE_CPP11 ----- */
24 #include <cmath>
25 #endif /* ----- not ENABLE_CPP11 ----- */
26 
27 bool almostEqual(float x, float y, float epsilon)
28 {
29  if (x == 0.0 && y == 0.0) {
30  return true;
31  }
32 
33  if (fabs(x) > fabs(y)) {
34  return fabs((x - y) / x) < epsilon;
35  } else {
36  return fabs((x - y) / y) < epsilon;
37  }
38 }
39 
40 bool almostEqual(double x, double y, double epsilon)
41 {
42  if (x == 0.0 && y == 0.0){
43  return true;
44  }
45  if (fabs(x) > fabs(y)){
46  return fabs((x - y) / x) < epsilon;
47  } else {
48  return fabs((x - y) / y) < epsilon;
49  }
50 }
51 
52 bool almostEqual(long double x, long double y, long double epsilon)
53 {
54  if (x == 0.0 && y == 0.0){
55  return true;
56  }
57  if (std::fabs(x) > std::fabs(y)){
58  return std::fabs((x - y) / x) < epsilon;
59  } else {
60  return std::fabs((x - y) / y) < epsilon;
61  }
62 }
63 
64 #endif
bool almostEqual(float x, float y, float epsilon)
Definition: numutil.cpp:27