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,73 @@
#include <myx/math/float_cmp_types.hpp>
#include <myx/math/almost_equal_ulps_and_abs.hpp>
#include <cmath>
namespace myx {
namespace math {
bool almost_equal_ulps_and_abs( const float a, const float b,
const float maxAbsDiff, const int maxUlpsDiff )
{
// Check if the numbers are really close -- needed
// when comparing numbers near zero.
float absDiff = fabsf( a - b );
if ( absDiff <= maxAbsDiff )
{
return( true );
}
float_cmp_t uA( a );
float_cmp_t uB( b );
// Different signs means they do not match.
if ( uA.negative() != uB.negative() )
{
return( false );
}
// Find the difference in ULPs.
int ulpsDiff = std::abs( uA.i - uB.i );
if ( ulpsDiff <= maxUlpsDiff )
{
return( true );
}
return( false );
} // almost_equal_ulps_and_abs
bool almost_equal_ulps_and_abs( const double a, const double b,
const double maxAbsDiff, const int maxUlpsDiff )
{
// Check if the numbers are really close -- needed
// when comparing numbers near zero.
double absDiff = fabs( a - b );
if ( absDiff <= maxAbsDiff )
{
return( true );
}
double_cmp_t uA( a );
double_cmp_t uB( b );
// Different signs means they do not match.
if ( uA.negative() != uB.negative() )
{
return( false );
}
// Find the difference in ULPs.
auto ulpsDiff = std::abs( uA.i - uB.i );
if ( ulpsDiff <= maxUlpsDiff )
{
return( true );
}
return( false );
} // almost_equal_ulps_and_abs
} // namespace math
} // namespace myx