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
81 changes: 0 additions & 81 deletions src/common/FloatUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include "FloatUtils.h"
#include <iomanip>
#include <math.h>
#include <sstream>

// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
Expand All @@ -39,81 +38,6 @@ bool FloatUtils::areEqual( double x, double y, double epsilon )
return false;
}

double FloatUtils::abs( double x )
{
return fabs( x );
}

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() );
}

bool FloatUtils::gt( double x, double y, double epsilon )
{
return isPositive( x - y, epsilon );
}

bool FloatUtils::gte( double x, double y, double epsilon )
{
return !isNegative( x - y, epsilon );
}

bool FloatUtils::lt( double x, double y, double epsilon )
{
return gt( y, x, epsilon );
}

bool FloatUtils::lte( double x, double y, double epsilon )
{
return gte( y, x, epsilon );
}

double FloatUtils::min( double x, double y, double epsilon )
{
return lt( x, y, epsilon ) ? x : y;
}

double FloatUtils::max( double x, double y, double epsilon )
{
return gt( x, y, epsilon ) ? x : y;
}

double FloatUtils::infinity()
{
return DBL_MAX;
}

double FloatUtils::negativeInfinity()
{
return -DBL_MAX;
}

String FloatUtils::doubleToString( double x, unsigned precision )
{
std::ostringstream strout;
Expand All @@ -128,11 +52,6 @@ String FloatUtils::doubleToString( double x, unsigned precision )
return str;
}

bool FloatUtils::wellFormed( double x )
{
return !isNan( x ) && !isInf( x );
}

bool FloatUtils::isNan( double x )
{
return isnan( x );
Expand Down
107 changes: 89 additions & 18 deletions src/common/FloatUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
#ifndef __FloatUtils_h__
#define __FloatUtils_h__

#include "Debug.h"
#include "GlobalConfiguration.h"
#include "MString.h"

#include <cfloat>
#include <math.h>

#ifdef _WIN32
#undef max
Expand All @@ -30,28 +32,97 @@ class FloatUtils
{
public:
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 );
static bool lte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static double min( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static double max( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS );
static double infinity();
static double negativeInfinity();
static bool isFinite( double x );
static String doubleToString( double x,
unsigned precision = GlobalConfiguration::DEFAULT_DOUBLE_TO_STRING_PRECISION );
static bool wellFormed( double x );

static bool isNan( double x );

static bool isInf( double x );

static bool isZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return ( -epsilon <= x ) && ( x <= epsilon );
}
yuvaljacoby marked this conversation as resolved.
Show resolved Hide resolved

static bool isPositive( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
ASSERT( epsilon > 0 );
return x > epsilon;
}

static bool isNegative( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
ASSERT( epsilon > 0 );
return x < -epsilon;
}

static double abs( double x )
{
return fabs( x );
}

static bool areDisequal( double x,
double y,
double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return !areEqual( x, y, epsilon );
}

static double roundToZero( double x, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return isZero( x, epsilon ) ? 0.0 : x;
}

static bool gt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return isPositive( x - y, epsilon );
}

static bool gte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return !isNegative( x - y, epsilon );
}

static bool lt( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return gt( y, x, epsilon );
}

static bool lte( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return gte( y, x, epsilon );
}

static double min( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return lt( x, y, epsilon ) ? x : y;
}

static double max( double x, double y, double epsilon = GlobalConfiguration::DEFAULT_EPSILON_FOR_COMPARISONS )
{
return gt( x, y, epsilon ) ? x : y;
}

static double infinity()
{
return DBL_MAX;
}

static double negativeInfinity()
{
return -DBL_MAX;
}

static bool isFinite( double x )
{
return ( x != infinity() ) && ( x != negativeInfinity() );
}

static bool wellFormed( double x )
{
return !isNan( x ) && !isInf( x );
}

};

#endif // __FloatUtils_h__
Expand Down