Created
November 6, 2012 14:11
-
-
Save dgroft/4024943 to your computer and use it in GitHub Desktop.
Determine if two floats are "equal"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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