23 lines
		
	
	
		
			671 B
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			23 lines
		
	
	
		
			671 B
		
	
	
	
		
			C#
		
	
	
	
	
	
namespace Engine.Core;
 | 
						|
 | 
						|
public static class FloatExtensions
 | 
						|
{
 | 
						|
    public static bool ApproximatelyEquals(this float a, float b)
 | 
						|
        => ApproximatelyEquals(a, b, float.Epsilon);
 | 
						|
    public static bool ApproximatelyEquals(this float a, float b, float epsilon)
 | 
						|
    {
 | 
						|
        if (a == b)
 | 
						|
            return true;
 | 
						|
 | 
						|
        const float floatNormal = (1 << 23) * float.Epsilon;
 | 
						|
        float absA = Math.Abs(a);
 | 
						|
        float absB = Math.Abs(b);
 | 
						|
        float diff = Math.Abs(a - b);
 | 
						|
 | 
						|
        if (a == 0.0f || b == 0.0f || diff < floatNormal)
 | 
						|
            return diff < (epsilon * floatNormal);
 | 
						|
 | 
						|
        return diff / Math.Min(absA + absB, float.MaxValue) < epsilon;
 | 
						|
    }
 | 
						|
}
 |