mirror of https://github.com/SixLabors/ImageSharp
1 changed files with 45 additions and 0 deletions
@ -0,0 +1,45 @@ |
|||||
|
namespace ImageSharp.Tests.TestUtilities |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Allows the comparison of single-precision floating point values by precision.
|
||||
|
/// </summary>
|
||||
|
public struct FloatRoundingComparer : IEqualityComparer<float> |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="FloatRoundingComparer"/> struct.
|
||||
|
/// </summary>
|
||||
|
/// <param name="precision">The number of decimal places (valid values: 0-7)</param>
|
||||
|
public FloatRoundingComparer(int precision) |
||||
|
{ |
||||
|
this.Precision = precision; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the number of decimal places (valid values: 0-7)
|
||||
|
/// </summary>
|
||||
|
public int Precision { get; } |
||||
|
|
||||
|
/// <inheritdoc />
|
||||
|
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<float>.Default.Compare(xp, yp) == 0; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc />
|
||||
|
public int GetHashCode(float obj) |
||||
|
{ |
||||
|
unchecked |
||||
|
{ |
||||
|
int hashCode = obj.GetHashCode(); |
||||
|
hashCode = (hashCode * 397) ^ this.Precision.GetHashCode(); |
||||
|
return hashCode; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue