From f2eb0c610a826f45fe3c5c02ac475a5de79bf6f7 Mon Sep 17 00:00:00 2001 From: paulhoux Date: Fri, 29 Dec 2023 12:34:54 +0100 Subject: [PATCH] Reverted the removal of EPSILON_VALUE, because it broke older code that 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. --- include/cinder/CinderMath.h | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/include/cinder/CinderMath.h b/include/cinder/CinderMath.h index 7eb9791ecb..d9cf19efbc 100644 --- a/include/cinder/CinderMath.h +++ b/include/cinder/CinderMath.h @@ -123,41 +123,44 @@ struct CI_API math #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/ @@ -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/