namespace ImageSharp.Tests.TestUtilities
{
using System;
using System.Collections.Generic;
///
/// Allows the comparison of single-precision floating point values by precision.
///
public struct FloatRoundingComparer : IEqualityComparer
{
///
/// Initializes a new instance of the struct.
///
/// The number of decimal places (valid values: 0-7)
public FloatRoundingComparer(int precision)
{
this.Precision = precision;
}
///
/// Gets the number of decimal places (valid values: 0-7)
///
public int Precision { get; }
///
public bool Equals(float x, float y)
{
float xp = (float)Math.Round(x, this.Precision, MidpointRounding.AwayFromZero);
float yp = (float)Math.Round(y, this.Precision, MidpointRounding.AwayFromZero);
return Comparer.Default.Compare(xp, yp) == 0;
}
///
public int GetHashCode(float obj)
{
unchecked
{
int hashCode = obj.GetHashCode();
hashCode = (hashCode * 397) ^ this.Precision.GetHashCode();
return hashCode;
}
}
}
}