Skip to content

Comparing floating-point with ‘==’ or ‘!=’ is unsafe #819

@xqgex

Description

@xqgex

When compiling the latest stable version (v2.6.1) using GCC 14 I am getting the following errors:

unity.c:356:16: error: comparing floating-point with ‘==’ or ‘!=’ is unsafe [-Werror=float-equal]
  356 |     if (number == 0.0f)
      |                ^~
unity.c:423:52: error: comparing floating-point with ‘==’ or ‘!=’ is unsafe [-Werror=float-equal]
  423 |         if ((n & 1) && (((UNITY_DOUBLE)n - number) == 0.5f))
      |                                                    ^~

In master latest commit 747c2ee those are at lines 370 and 437

Unity/src/unity.c

Lines 369 to 373 in 747c2ee

/* handle zero, NaN, and +/- infinity */
if (number == 0.0f)
{
UnityPrint("0");
}

Unity/src/unity.c

Lines 435 to 439 in 747c2ee

#ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO
/* round to even if exactly between two integers */
if ((n & 1) && (((UNITY_DOUBLE)n - number) == 0.5f))
n--;
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions