Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 13, 2025

Problem

The IsEqualTo assertion was failing when comparing double.NaN values, even though both values were NaN. This is a regression compared to v0.67.19.

[Test]
public async Task Test1()
{
    await Assert.That(double.NaN).IsEqualTo(double.NaN);
}

Result:

AssertionException: Expected to be NaN
but found NaN

Interestingly, float.NaN comparisons worked correctly, but double.NaN did not.

Root Cause

The DoubleEqualsAssertion class was using the == operator to compare double values:

if (value == _expected)
{
    return Task.FromResult(AssertionResult.Passed);
}

According to the IEEE 754 floating-point standard, NaN == NaN always returns false, causing the assertion to fail incorrectly.

Solution

Changed the comparison to use double.Equals() instead:

if (double.Equals(value, _expected))
{
    return Task.FromResult(AssertionResult.Passed);
}

The double.Equals() method correctly treats NaN values as equal, which is the expected behavior for equality assertions.

Why float.NaN Worked

float.NaN comparisons worked correctly because there's no specialized FloatEqualsAssertion. Instead, float values use the generic EqualsAssertion<float> which internally uses EqualityComparer<float>.Default.Equals(), which already handles NaN correctly.

Tests Added

Added comprehensive tests for special floating-point values:

  • Double_NaN_EqualsTo_NaN_Success()
  • Double_PositiveInfinity_EqualsTo_PositiveInfinity_Success()
  • Double_NegativeInfinity_EqualsTo_NegativeInfinity_Success()

All tests pass successfully.

Fixes #[issue_number]

Original prompt

This section details on the original issue you should resolve

<issue_title>IsEqualTo does not handle double.NaN correctly</issue_title>
<issue_description>```cs
[Test]
public async Task Test1()
{
await Assert.That(double.NaN).IsEqualTo(double.NaN);
}

Result:

AssertionException: Expected to be NaN
but found NaN

This is a regression compared to v0.67.19
This doesn't happen with `float.NaN`</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Fixes #3359


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@thomhurst thomhurst marked this pull request as ready for review October 13, 2025 17:39
@thomhurst thomhurst enabled auto-merge (squash) October 13, 2025 17:39
Copilot AI changed the title [WIP] Fix IsEqualTo to handle double.NaN correctly Fix IsEqualTo assertion for double.NaN values Oct 13, 2025
Copilot AI requested a review from thomhurst October 13, 2025 17:39
@thomhurst thomhurst merged commit 8445472 into main Oct 13, 2025
12 of 13 checks passed
@thomhurst thomhurst deleted the copilot/fix-isequalto-nan-handling branch October 13, 2025 18:00
@claude claude bot mentioned this pull request Oct 14, 2025
1 task
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.

IsEqualTo does not handle double.NaN correctly

2 participants