Skip to content

Commit

Permalink
Fix for comparing positive infinity with positive infinity.
Browse files Browse the repository at this point in the history
  • Loading branch information
GregFinzer committed Apr 21, 2022
1 parent 9577fbb commit 7c8610a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Compare-NET-Objects-Tests/BugTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ public void Cleanup()

#region Tests

[Test]
public void PositiveInfinityShouldBeTheSame()
{
double positiveInfinity = Double.PositiveInfinity;
double positiveInfinity2 = Double.PositiveInfinity;

CompareLogic compareLogic = new CompareLogic();
var result = compareLogic.Compare(positiveInfinity, positiveInfinity2);
Console.WriteLine(result.DifferencesString);
Assert.IsTrue(result.AreEqual);
}

[Test]
public void PositiveInfinityAndNegativeInfinityShouldBeDifferent()
{
Expand Down
6 changes: 6 additions & 0 deletions Compare-NET-Objects/TypeComparers/DoubleComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public override void CompareType(CompareParms parms)
Double double1 = (Double)parms.Object1;
Double double2 = (Double)parms.Object2;

if (Double.IsPositiveInfinity(double1) && Double.IsPositiveInfinity(double2))
return;

if (Double.IsNegativeInfinity(double1) && Double.IsNegativeInfinity(double2))
return;

double diff = double1 - double2;
if ((Math.Abs(diff) > parms.Config.DoublePrecision)
|| (Double.IsNaN(diff) && (!Double.IsNaN(double1) || !Double.IsNaN(double2))))
Expand Down

0 comments on commit 7c8610a

Please sign in to comment.