Skip to content

Fix for Equality check on floating point values#294

Merged
samsmithnz merged 5 commits intomainfrom
finding-autofix-09385578
Dec 4, 2025
Merged

Fix for Equality check on floating point values#294
samsmithnz merged 5 commits intomainfrom
finding-autofix-09385578

Conversation

@samsmithnz
Copy link
Copy Markdown
Collaborator

To fix this issue, replace the equality comparison with an "approximate equality" comparison: check that the two values are within a small tolerance (delta). Use Assert.AreEqual with the delta parameter, which is standard in MSTest for this purpose. Specifically, in Assert.AreEqual(expected, actual, delta), the assertion passes if Math.Abs(expected - actual) <= delta.

Therefore, replace line 25:

Assert.IsTrue(System.Math.Round(result, 0) == 3393f);

with:

Assert.AreEqual(3393f, System.Math.Round(result, 0), 1f);

Here, 1f is a reasonable tolerance given we are rounding to whole numbers of skittles. Repeat this pattern for lines 40 and 55, which make similar comparisons for values 848f and 212f.

No extra imports or methods are needed, since Assert is already in scope and AreEqual is part of MSTest.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 4, 2025

Pull Request Test Coverage Report for Build 19940565103

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 81.992%

Totals Coverage Status
Change from base Build 19940235083: 0.0%
Covered Lines: 100
Relevant Lines: 101

💛 - Coveralls

@samsmithnz
Copy link
Copy Markdown
Collaborator Author

@copilot, make sure you fix this rule on all methods in this file.

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 4, 2025

@samsmithnz I've opened a new pull request, #299, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 3 commits December 4, 2025 16:50
Co-authored-by: samsmithnz <8389039+samsmithnz@users.noreply.github.com>
Fix floating point equality checks in SkittleTests.cs
@samsmithnz samsmithnz marked this pull request as ready for review December 4, 2025 19:02
@samsmithnz samsmithnz merged commit c5eca32 into main Dec 4, 2025
7 checks passed
@samsmithnz samsmithnz deleted the finding-autofix-09385578 branch December 4, 2025 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants