Skip to content

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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,12 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
static const int sig_digits = 9;
static const UNITY_INT32 min_scaled = 100000000;
static const UNITY_INT32 max_scaled = 1000000000;
static const UNITY_DOUBLE epsilon = UNITY_DOUBLE_PRECISION;
#else
static const int sig_digits = 7;
static const UNITY_INT32 min_scaled = 1000000;
static const UNITY_INT32 max_scaled = 10000000;
static const UNITY_DOUBLE epsilon = UNITY_FLOAT_PRECISION;
#endif

UNITY_DOUBLE number = input_number;
Expand All @@ -353,7 +355,7 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)
}

/* handle zero, NaN, and +/- infinity */
if (number == 0.0f)
if (UNITY_ABS(number) < epsilon)
{
UnityPrint("0");
}
Expand Down Expand Up @@ -420,7 +422,7 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number)

#ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO
/* 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--;
#endif

Expand Down
15 changes: 13 additions & 2 deletions src/unity_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,14 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT;
#ifndef UNITY_IS_NAN
#ifndef isnan
/* NaN is the only floating point value that does NOT equal itself.
* Therefore if n != n, then it is NaN. */
#define UNITY_IS_NAN(n) ((n != n) ? 1 : 0)
* Therefore if n != n, then it is NaN.
*
* 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
#define UNITY_IS_NAN(n) isnan(n)
#endif
Expand All @@ -276,6 +282,11 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT;
#endif
#endif

/* Calculates the absolute value of the given number */
#ifndef UNITY_ABS
#define UNITY_ABS(n) (((n) < 0) ? -(n) : (n))
#endif

#endif

/*-------------------------------------------------------
Expand Down