Skip to content

Fix for Equality check on floating point values#296

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

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

Conversation

@samsmithnz
Copy link
Copy Markdown
Collaborator

To fix the problem, floating-point values should not be compared directly using ==. Instead, we should compare the absolute difference between the calculated value and the expected value against a small "epsilon" value, which defines the acceptable margin of error due to floating point imprecision.

Since the assertion is about a rounded number of M&Ms, and the expected value is an integer, we can either (1) compare integer values by casting the rounded value to an integer, or (2) define a small tolerance for float comparison (using something like Math.Abs(a - b) < epsilon). Using the integer comparison is simple and appropriate here since the value is rounded to zero decimals.

Therefore, in this file, change line 124 from:

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

to:

Assert.AreEqual(5, (int)Math.Round(result, 0));

This will assert the rounded value equals 5 as an integer, which avoids all floating point equality issues.

Make similar changes on lines 96 and 110.

No new imports are needed, as Math is already in use.


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 19939620337

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 19937062453: 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, #297, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 2 commits December 4, 2025 16:53
Co-authored-by: samsmithnz <8389039+samsmithnz@users.noreply.github.com>
Fix floating-point equality checks in all test assertions
@samsmithnz samsmithnz marked this pull request as ready for review December 4, 2025 18:26
@samsmithnz samsmithnz enabled auto-merge December 4, 2025 18:26
@samsmithnz samsmithnz merged commit 5ce56d5 into main Dec 4, 2025
7 checks passed
@samsmithnz samsmithnz deleted the finding-autofix-ff32f6d5 branch December 4, 2025 18:29
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