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

What is the correct result of Equals<NaN, NaN>? #2

Open
SparkieLabs opened this issue Aug 14, 2021 · 1 comment
Open

What is the correct result of Equals<NaN, NaN>? #2

SparkieLabs opened this issue Aug 14, 2021 · 1 comment

Comments

@SparkieLabs
Copy link
Owner

Under IEEE rules NaN is not equal to NaN.

The way this is implemented for double is that the == operator obeys the IEEE rule but Equals doesn't:

    [Fact]
    public void DoubleEqualityOpNaNTest()
    {
        double a = double.NaN;
        // Test passes
        Assert.False(a == a);
    }

    [Fact]
    public void DoubleEqualsNaNTest()
    {
        double a = double.NaN;
        // Test fails
        Assert.False(a.Equals(a));
    }

Which is strange as I would expect == and Equals to be consistent, but it is even mentioned in the docs.

The non-generic Vector types return false for both the operator and Equals method, which is what is currently implemented here.

Also note that if you use the equality operator generated for records with double properties, as it uses the double Equals method, neither the == operator nor Equals method returns false:

    readonly record struct DoublePropertyRecord(double Value);

    [Fact]
    public void DoublePropertyRecordEqualsNaNTest1()
    {
        DoublePropertyRecord a = new DoublePropertyRecord(double.NaN);
        // Test fails
        Assert.False(a == a);
    }

    [Fact]
    public void DoublePropertyRecordEqualsNaNTest2()
    {
        DoublePropertyRecord a = new DoublePropertyRecord(double.NaN);
        // Test fails
        Assert.False(a.Equals(a));
    }
@SparkieLabs
Copy link
Owner Author

Repository owner deleted a comment from officialarmannqureshi Feb 26, 2024
Repository owner deleted a comment from officialarmannqureshi Feb 26, 2024
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

No branches or pull requests

1 participant