Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize floatutils #191

Merged
15 changes: 0 additions & 15 deletions src/common/FloatUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,11 @@ bool FloatUtils::areDisequal( double x, double y, double epsilon )
return !areEqual( x, y, epsilon );
}

bool FloatUtils::isZero( double x, double epsilon )
{
return ( -epsilon <= x ) && ( x <= epsilon );
}

double FloatUtils::roundToZero( double x, double epsilon )
{
return isZero( x, epsilon ) ? 0.0 : x;
}

bool FloatUtils::isPositive( double x, double epsilon )
{
return ( !isZero( x, epsilon ) ) && ( x > 0.0 );
}

bool FloatUtils::isNegative( double x, double epsilon )
{
return ( !isZero( x, epsilon ) ) && ( x < 0.0 );
}

bool FloatUtils::isFinite( double x )
{
return ( x != infinity() ) && ( x != negativeInfinity() );
Expand Down
22 changes: 19 additions & 3 deletions src/common/FloatUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,37 @@

#include "GlobalConfiguration.h"
#include "MString.h"
#include "Debug.h"
yuvaljacoby marked this conversation as resolved.
Show resolved Hide resolved

#include <cfloat>

class FloatUtils
{
public:
inline static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
ASSERT( epsilon > 0 );
double lower = -epsilon;
double upper = epsilon;
return ( x - upper ) * ( x - lower ) <= 0;
}
yuvaljacoby marked this conversation as resolved.
Show resolved Hide resolved
inline static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
ASSERT( epsilon > 0 );
return x > epsilon;
}
inline static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
ASSERT( epsilon > 0 );
return x < -epsilon;
}

static bool areEqual( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static double abs( double x );
static bool areDisequal( double x,
double y,
double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static double roundToZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool gt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool gte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static bool lt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
Expand Down