Skip to content

Commit

Permalink
Reverted the removal of EPSILON_VALUE, because it broke older code th…
Browse files Browse the repository at this point in the history
…at relied on it. Also reverted back to using this defined EPSILON_VALUE, because it's larger and more appropriate than FLT_EPSILON or DBL_EPSILON in most cases, specifically the cases for which these functions were added to Cinder.
  • Loading branch information
paulhoux committed Dec 29, 2023
1 parent 4e8916a commit f2eb0c6
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions include/cinder/CinderMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,41 +123,44 @@ struct CI_API math<float>
#define M_PI 3.14159265358979323846
#endif

CI_API inline bool approxZero( float n, float epsilon = FLT_EPSILON )
constexpr double EPSILON_VALUE = 4.37114e-05;
#define EPSILON EPSILON_VALUE

CI_API inline bool approxZero( float n, float epsilon = float(EPSILON_VALUE) )
{
return std::abs( n ) < epsilon;
}

CI_API inline bool approxZero( double n, double epsilon = DBL_EPSILON )
CI_API inline bool approxZero( double n, double epsilon = EPSILON_VALUE )
{
return std::abs( n ) < epsilon;
}

CI_API inline float roundToZero( float n, float epsilon = FLT_EPSILON )
CI_API inline float roundToZero( float n, float epsilon = float(EPSILON_VALUE) )
{
if( std::abs( n ) < epsilon )
if( approxZero( n, epsilon ) )
return 0.0f;
return n;
}

CI_API inline double roundToZero( double n, double epsilon = DBL_EPSILON )
CI_API inline double roundToZero( double n, double epsilon = EPSILON_VALUE )
{
if( std::abs( n ) < epsilon )
if( approxZero( n, epsilon ) )
return 0.0;
return n;
}

CI_API inline bool approxEqual( float a, float b, float epsilon = FLT_EPSILON )
CI_API inline bool approxEqual( float a, float b, float epsilon = float(EPSILON_VALUE) )
{
return std::abs( b - a ) < epsilon;
}

CI_API inline bool approxEqual( double a, double b, double epsilon = DBL_EPSILON )
CI_API inline bool approxEqual( double a, double b, double epsilon = EPSILON_VALUE )
{
return std::abs( b - a ) < epsilon;
}

CI_API inline bool approxEqualRelative( float a, float b, float maxRelDiff = FLT_EPSILON )
CI_API inline bool approxEqualRelative( float a, float b, float maxRelDiff = float(EPSILON_VALUE) )
{
// See: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Expand All @@ -175,7 +178,7 @@ CI_API inline bool approxEqualRelative( float a, float b, float maxRelDiff = FLT
return false;
}

CI_API inline bool approxEqualRelative( double a, double b, double maxRelDiff = DBL_EPSILON )
CI_API inline bool approxEqualRelative( double a, double b, double maxRelDiff = EPSILON_VALUE )
{
// See: https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Expand Down

0 comments on commit f2eb0c6

Please sign in to comment.