Skip to content

Instantly share code, notes, and snippets.

@dgroft
Created November 6, 2012 14:11
Show Gist options
  • Save dgroft/4024943 to your computer and use it in GitHub Desktop.
Save dgroft/4024943 to your computer and use it in GitHub Desktop.
Determine if two floats are "equal"
using System.Linq;
public static class Math
{
/// <summary>
/// Floating-point epsilon value.
/// </summary>
public const float FLT_EPSILON = 1.192092896e-07f;
/// <summary>
/// Determines whether two floating-point values are "equal,"
/// or close enough in value (by less than epsilon).
/// </summary>
public static bool AreEqual(float a, float b, float epsilon = FLT_EPSILON)
{
return System.Math.Abs(a - b) <= epsilon * new float[] { 1.0f, System.Math.Abs(a), System.Math.Abs(b) }.Max();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment