Handling float discrepancy in Unity3d
I can't quite figure out in which situation you would use approximatelyEqual() and in which essentiallyEqual(). and what the difference really should be.

I made some tests and overall it seems to work. I did though find the below strange behavior:

Testing with these two values:

    float a = 1.0f; 
    float b = 1.0f + 1e-6f;

    Console.WriteLine($"a: {a:F15}");
    Console.WriteLine($"b: {b:F15}");
    Console.WriteLine($"a - b: {a - b:F15}");
    Console.WriteLine($"b - a: {b - a:F15}");
    Console.WriteLine();

    Console.WriteLine($"approximatelyEqual: {approximatelyEqual(a, b)}");
    Console.WriteLine($"essentiallyEqual: {essentiallyEqual(a, b)}");
    Console.WriteLine($"definitelyGreaterThan: {definitelyGreaterThan(a, b)}");
    Console.WriteLine($"definitelyLessThan: {definitelyLessThan(a, b)}");
    Console.WriteLine();
    Console.WriteLine($"HEquals: {HEquals(a, b)}");
    Console.WriteLine($"HGreaterThan: {HGreaterThan(a, b)}");
    Console.WriteLine($"HLessThan: {HLessThan(a, b)}");


I get the expected output:

a: 1.000000000000000
b: 1.000001000000000
a - b: -0.000000953674300
b - a: 0.000000953674300

approximatelyEqual: True
essentiallyEqual: True
definitelyGreaterThan: False
definitelyLessThan: False

HEquals: True
HGreaterThan: False
HLessThan: False


But if I try this:

    float a = 0.0f; 
    float b = 1e-6f;


I get this:

a: 0.000000000000000
b: 0.000001000000000
a - b: -0.000001000000000
b - a: 0.000001000000000

approximatelyEqual: False
essentiallyEqual: False
definitelyGreaterThan: False
definitelyLessThan: True

HEquals: True
HGreaterThan: False
HLessThan: False


But I would have expected the same result?



The H-functions is my own naive implementations:

public static bool HEquals(float a, float b)
{
  return Math.Abs(a - b) &lt; Accuracy;
}

public static bool HGreaterThan(float a, float b)
{
  return a - b &gt;= Accuracy;
}

public static bool HLessThan(float a, float b)
{
  return a - b &lt;= -Accuracy;
}




Disclaimer: I'm not using Unity3ds Mathf.Abs() but System.Math.Abs()