MOOSE - Multiscale Object Oriented Simulation Environment
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
numutil.h File Reference
#include <cmath>
#include <cfloat>
#include <limits>
+ Include dependency graph for numutil.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define M_E   2.7182818284590452353
 
#define M_PI   3.14159265358979323846
 

Functions

bool almostEqual (float x, float y, float epsilon=FLT_EPSILON)
 
bool almostEqual (double x, double y, double epsilon=DBL_EPSILON)
 
bool almostEqual (long double x, long double y, long double epsilon=LDBL_EPSILON)
 
template<class T >
bool isClose (T a, T b, T tolerance)
 
template<typename T >
bool isInfinity (T value)
 
template<class T >
bool isNaN (T value)
 

Variables

const double LN2 = 0.69314718055994528622676
 
const unsigned long LN2BYTES = 0xB1721814
 
const double NATURAL_E = 2.718281828459045
 
const int WORD_LENGTH = 32
 

Macro Definition Documentation

#define M_E   2.7182818284590452353

Function Documentation

bool almostEqual ( float  x,
float  y,
float  epsilon = FLT_EPSILON 
)

Definition at line 27 of file numutil.cpp.

Referenced by Interpol::setXmax(), and Interpol::setXmin().

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 }

+ Here is the caller graph for this function:

bool almostEqual ( double  x,
double  y,
double  epsilon = DBL_EPSILON 
)

Definition at line 40 of file numutil.cpp.

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 }
bool almostEqual ( long double  x,
long double  y,
long double  epsilon = LDBL_EPSILON 
)

Definition at line 52 of file numutil.cpp.

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 }
template<class T >
bool isClose ( a,
b,
tolerance 
)

Check 2 floating-point numbers for "equality". Algorithm (from Knuth) 'a' and 'b' are close if: | ( a - b ) / a | < e AND | ( a - b ) / b | < e where 'e' is a small number.

In this function, 'e' is computed as: e = tolerance * machine-epsilon

Definition at line 67 of file numutil.h.

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 }
template<typename T >
bool isInfinity ( value)

Definition at line 52 of file numutil.h.

53 {
54  return value == std::numeric_limits< T >::infinity();
55 }
uint32_t value
Definition: moosemodule.h:42
template<class T >
bool isNaN ( value)

Functions for floating point comparisons

Definition at line 46 of file numutil.h.

References value.

47 {
48  return value != value;
49 }
uint32_t value
Definition: moosemodule.h:42

Variable Documentation

const double LN2 = 0.69314718055994528622676

Definition at line 26 of file numutil.h.

const unsigned long LN2BYTES = 0xB1721814

Definition at line 27 of file numutil.h.

const double NATURAL_E = 2.718281828459045

Definition at line 28 of file numutil.h.

const int WORD_LENGTH = 32

Definition at line 25 of file numutil.h.