Fix failed build with compile flag: -Werror=float-equal (#741)

This commit is contained in:
Artem Sharipov
2025-03-23 23:22:27 +05:00
parent cdf1d0297e
commit 8c26b8516f
2 changed files with 17 additions and 4 deletions
+4 -2
View File
@@ -337,10 +337,12 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
static const int sig_digits = 9; static const int sig_digits = 9;
static const UNITY_INT32 min_scaled = 100000000; static const UNITY_INT32 min_scaled = 100000000;
static const UNITY_INT32 max_scaled = 1000000000; static const UNITY_INT32 max_scaled = 1000000000;
static const UNITY_DOUBLE epsilon = UNITY_DOUBLE_PRECISION;
#else #else
static const int sig_digits = 7; static const int sig_digits = 7;
static const UNITY_INT32 min_scaled = 1000000; static const UNITY_INT32 min_scaled = 1000000;
static const UNITY_INT32 max_scaled = 10000000; static const UNITY_INT32 max_scaled = 10000000;
static const UNITY_DOUBLE epsilon = UNITY_FLOAT_PRECISION;
#endif #endif
UNITY_DOUBLE number = input_number; UNITY_DOUBLE number = input_number;
@@ -353,7 +355,7 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
} }
/* handle zero, NaN, and +/- infinity */ /* handle zero, NaN, and +/- infinity */
if (number == 0.0f) if (UNITY_ABS(number) < epsilon)
{ {
UnityPrint("0"); UnityPrint("0");
} }
@@ -420,7 +422,7 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
#ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO #ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO
/* round to even if exactly between two integers */ /* round to even if exactly between two integers */
if ((n & 1) && (((UNITY_DOUBLE)n - number) == 0.5f)) if ((n & 1) && (UNITY_ABS((UNITY_DOUBLE)n - number - 0.5f) < epsilon))
n--; n--;
#endif #endif
+13 -2
View File
@@ -259,8 +259,14 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT;
#ifndef UNITY_IS_NAN #ifndef UNITY_IS_NAN
#ifndef isnan #ifndef isnan
/* NaN is the only floating point value that does NOT equal itself. /* NaN is the only floating point value that does NOT equal itself.
* Therefore if n != n, then it is NaN. */ * Therefore if n != n, then it is NaN.
#define UNITY_IS_NAN(n) ((n != n) ? 1 : 0) *
* Another way to define NaN is to check whether the number belongs
* simultaneously to the set of negative and positive numbers, including 0.
* Therefore if ((n >= 0) == (n < 0)), then it is NaN.
* This implementation will not cause an error with the compilation flag:
* -Werror=float-equal */
#define UNITY_IS_NAN(n) ((((n) >= 0.0f) == ((n) < 0.0f)) ? 1 : 0)
#else #else
#define UNITY_IS_NAN(n) isnan(n) #define UNITY_IS_NAN(n) isnan(n)
#endif #endif
@@ -276,6 +282,11 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT;
#endif #endif
#endif #endif
/* Calculates the absolute value of the given number */
#ifndef UNITY_ABS
#define UNITY_ABS(n) (((n) < 0) ? -(n) : (n))
#endif
#endif #endif
/*------------------------------------------------------- /*-------------------------------------------------------