Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FloatingPointComparisson.hpp
Go to the documentation of this file.
1 
9 #ifndef FLOATINGPOINTCOMPARISSON_HPP_
10 #define FLOATINGPOINTCOMPARISSON_HPP_
11 
12 #include <limits>
13 
14 namespace Numerics {
15 
16 
23  inline double AbsoluteValue( double a )
24  {
25  return( ( ( a < static_cast< double >( 0 ) )? -a : a ) );
26  }
27 
37  inline double ___safe_fpt_division( const double f1, const double f2 )
38  {
39  #ifdef ASSERT_ON
40  assert( f1 >= static_cast< double >( 0 ) );
41  assert( f2 >= static_cast< double >( 0 ) );
42  #endif
43 
44  /* Avoid overflow */
45  if( f2 < static_cast< double >( 1 ) && f1 > f2*std::numeric_limits< double >::max( ) )
47 
48 
49  /* Avoid underflow */
50  if( f1 == static_cast< double >( 0 ) ||
51  f2 > static_cast< double >( 1 ) && f1 < f2*std::numeric_limits< double >::min( ) )
52  return static_cast< double >( 0 );
53 
54  return( f1/f2 );
55  }
56 
64  inline bool fpointequals( const double a, const double b, double TOL=1e-9 )
65  {
66  double diff = a-b;
67  double adiff = AbsoluteValue( diff );
68  double d1 = ___safe_fpt_division( adiff, AbsoluteValue( a ) );
69  double d2 = ___safe_fpt_division( adiff, AbsoluteValue( b ) );
70  bool predicate1 = (d1 <= TOL);
71  bool predicate2 = (d2 <= TOL);
72 
73  if( (predicate1 || predicate2) )
74  return true;
75  return false;
76  }
77 
78 
79 }
80 #endif /* FLOATINGPOINTCOMPARISSON_HPP_ */
Vector_n max(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:354
const double TOL
Definition: GeoPrimitives.H:17
double AbsoluteValue(double a)
Returns the absolute value of the value a.
Vector_n min(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:346
double ___safe_fpt_division(const double f1, const double f2)
Performs a safe floating point division f1/f2.
bool fpointequals(const double a, const double b, double TOL=1e-9)
Checks if two floating numbers are &quot;nearly&quot; equal.