bgc-net/BasicGeometry/UtilityFP32.cs

52 lines
1.6 KiB
C#

/*
* Author: Andrey Pokidov
* License: Apache-2.0
* Date: 12 Nov 2024
*/
namespace BGC
{
public class UtilityFP32
{
public const float EPSYLON = 4.76837E-7f;
public const float SQUARE_EPSYLON = EPSYLON * EPSYLON;
public const float EPSYLON_EFFECTIVENESS_LIMIT = 1.0f;
public const float ONE_THIRD = 0.333333333f;
public const float ONE_SIXTH = 0.166666667f;
public const float ONE_NINETH = 0.111111111f;
public const float GOLDEN_RATIO_HIGH = 1.618034f;
public const float GOLDEN_RATIO_LOW = 0.618034f;
public static bool IsZero(float value)
{
return -EPSYLON <= value && value <= EPSYLON;
}
public static bool IsUnit(float value)
{
return (1.0f - EPSYLON) <= value && value <= (1.0f + EPSYLON);
}
public static bool IsSqareUnit(float square)
{
return (1.0f - 2.0f * EPSYLON) <= square && square <= (1.0f + 2.0f * EPSYLON);
}
public static bool AreClose(float value1, float value2)
{
float difference = value1 - value2;
float squareValue1 = value1 * value1;
float squareValue2 = value2 * value2;
float squareDifference = difference * difference;
if (squareValue1 <= EPSYLON_EFFECTIVENESS_LIMIT || squareValue2 <= EPSYLON_EFFECTIVENESS_LIMIT) {
return squareDifference <= SQUARE_EPSYLON;
}
return squareDifference <= SQUARE_EPSYLON * squareValue1 && squareDifference <= SQUARE_EPSYLON * squareValue2;
}
}
}