Merge branch 'master' of gitlab.2:f1x1t/myxlib

This commit is contained in:
2019-11-27 21:22:29 +03:00
10 changed files with 427 additions and 1 deletions

View File

@ -0,0 +1,56 @@
#include <myx/math/almost_equal_relative_and_abs.hpp>
#include <cmath>
namespace myx {
namespace math {
bool almost_equal_relative_and_abs( const float a, const float b,
const float maxAbsDiff, const float maxRelDiff )
{
// Check if the numbers are really close -- needed
// when comparing numbers near zero.
float diff = fabsf( a - b );
if ( diff <= maxAbsDiff )
{
return( true );
}
float aN = fabsf( a );
float bN = fabsf( b );
float largest = ( bN > aN ) ? bN : aN;
if ( diff <= largest * maxRelDiff )
{
return( true );
}
return( false );
}
bool almost_equal_relative_and_abs( const double a, const double b,
const double maxAbsDiff, const double maxRelDiff )
{
// Check if the numbers are really close -- needed
// when comparing numbers near zero.
double diff = fabs( a - b );
if ( diff <= maxAbsDiff )
{
return( true );
}
double aN = fabs( a );
double bN = fabs( b );
double largest = ( bN > aN ) ? bN : aN;
if ( diff <= largest * maxRelDiff )
{
return( true );
}
return( false );
}
} // namespace math
} // namespace myx