Skip to content

Fix for Equality check on floating point values#295

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

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

Conversation

@samsmithnz
Copy link
Copy Markdown
Collaborator

To fix the issue, the code should avoid comparing floating-point values for equality using ==. Since System.Math.Round(result, 0) is meant to convert the floating-point result to the nearest whole number, it is better to either:

  1. Compare integer values directly (i.e., cast the rounded value to an integer before comparing); or
  2. Use a tolerance to check if the value is "close enough" to the expected value.

Since the test is logically intended to check that, after rounding, the number of M&Ms matches the expected integer, the most robust approach is to cast the rounded result to an integer and then compare it to the integer value 45. To make the code clearer, both values being compared should be integers.

Specifically, on line 71, replace:

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

with:

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

This both simplifies the test assertion (using AreEqual rather than IsTrue), and compares integer values, removing reliance on floating-point equality.

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

@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 4, 2025

Pull Request Test Coverage Report for Build 19940168901

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 19939713209: 0.0%
Covered Lines: 100
Relevant Lines: 101

💛 - Coveralls

Copilot AI and others added 2 commits December 4, 2025 16:53
Co-authored-by: samsmithnz <8389039+samsmithnz@users.noreply.github.com>
[WIP] Fix equality check on floating point values
@samsmithnz samsmithnz marked this pull request as ready for review December 4, 2025 18:45
@samsmithnz samsmithnz enabled auto-merge December 4, 2025 18:45
@samsmithnz samsmithnz merged commit c27ce1d into main Dec 4, 2025
7 checks passed
@samsmithnz samsmithnz deleted the finding-autofix-b8aa9045 branch December 4, 2025 18:47
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