bgc-net/BasicGeometry/UtilityFP64.cs

48 lines
1.6 KiB
C#

using System;
namespace BasicGeometry
{
public class UtilityFP64
{
public const double EPSYLON = 4.996003611E-14;
public const double SQUARE_EPSYLON = 2.496005208112504E-27;
public const double EPSYLON_EFFECTIVENESS_LIMIT = 1.0;
public const double ONE_THIRD = 0.333333333333333333;
public const double ONE_SIXTH = 0.166666666666666667;
public const double ONE_NINETH = 0.111111111111111111;
public const double GOLDEN_RATIO_HIGH = 1.61803398874989485;
public const double GOLDEN_RATIO_LOW = 0.61803398874989485;
public static bool IsZero(double value)
{
return -EPSYLON <= value && value <= EPSYLON;
}
public static bool IsUnit(double value)
{
return (1.0 - EPSYLON) <= value && value <= (1.0 + EPSYLON);
}
public static bool IsSqareUnit(double square)
{
return (1.0 - 2.0 * EPSYLON) <= square && square <= (1.0 + 2.0 * EPSYLON);
}
public static bool AreClose(double value1, double value2)
{
double difference = value1 - value2;
double squareValue1 = value1 * value1;
double squareValue2 = value2 * value2;
double 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;
}
}
}