Skip to content

Fix S1940 FP: for floating point numbers that can be NaN "!(a <= b)" is not the same as "a > b" #9779

@tobiass-sdl

Description

@tobiass-sdl

Description

Not sure if this is a FP in the strong sense but the "why is this an issue" text should at least clearly state that the suggested "fix" behaves differently than the original code if a or b is NaN.

public static bool Foo(ReadOnlySpan<double> a, ReadOnlySpan<double> b)
{
   for (; i < a.Length; ++i)
   {
      if (!(a[i] <= b[i])) return false;
   }

   return true;
}

we have several places in our code base that intentionally invert the result of a floating point comparison. Applying the suggestion to remove the invert would cause bugs.

Obviously the solution on our side is to double check that we have unittests that protect against blind application of the so called fix.

Reproducer

public static bool Foo(ReadOnlySpan<double> a, ReadOnlySpan<double> b)
{
   for (; i < a.Length; ++i)
   {
      if (!(a[i] <= b[i])) return false;
   }

   return true;
}

Product and Version

Sonarqube server, Data Center Edition v2025.1 (102418)

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