Skip to content
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

Improve S1244: Add message to use "IsX" instead of "== double.X" #6521

Closed
martin-strecker-sonarsource opened this issue Dec 12, 2022 · 1 comment · Fixed by #6670
Closed
Assignees
Labels
Area: C# C# rules related issues.
Milestone

Comments

@martin-strecker-sonarsource
Copy link
Contributor

martin-strecker-sonarsource commented Dec 12, 2022

Description

S1244 (Floating point numbers should not be tested for equality) complains about a comparison like this

if (d == double.NaN) // Noncompliant {{Do not check floating point equality with exact values, use a range instead.}}
{ }

In this case, the message should read: Do not check floating point equality with exact values, use double.IsNaN() instead.

The following methods should be supported:

For backward compatibility below .Net 7 double and float should be supported directly.

@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title Improve S1244: Add message to use "IsNan" instead of "== double.NaN" Improve S1244: Add message to use "IsNaN" instead of "== double.NaN" Dec 23, 2022
@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 8.52 milestone Jan 17, 2023
@antonioaversa
Copy link
Contributor

After discussing with @pavel-mikula-sonarsource, the following changes to the issue:

  • with exact values used in the messages for NaN, PositiveInfinity and NegativeInfinity, instead of the text with special values: any special value (namely constants defined in well-known floating-point types before .NET 7, and static abstract properties IFloatingPointIeee754<TSelf> since .NET 7) is also an exact value (such as 1.0, 2.3f, ...).
  • INumberBase.IsInfinity and INumberBase.IsFinite are methods checking multiple floating point values (IsInfinity checks for both PositiveInfinity and NegativeInfinity, IsFinite checks for zero and for all normal and subnormal values). As such they don't fall into the category of == Something, and are considered out of scope for this task.

Conversely, == type.IsNaN(), == type.IsPositiveInfinity() and == type.IsNegativeInfinity() all have corresponding constant or properties, and are considered in scope.

@antonioaversa antonioaversa changed the title Improve S1244: Add message to use "IsNaN" instead of "== double.NaN" Improve S1244: Add message to use "IsX" instead of "== double.X" Jan 25, 2023
@antonioaversa antonioaversa modified the milestones: 8.52, 8.53 Jan 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues.
Projects
None yet
3 participants