namespace NMA.Infrastructure.Builders
{
#region Usings
using System;
#endregion
///
/// Miscellaneous math utility methods.
///
/// Please be careful in your use of the methods that take left,
/// right and epsilon. Because of the imprecise nature of floating point
/// calculations, the comparison of epsilon to the difference between
/// left and right is not guaranteed to be precise. For example,
/// if left is 90.0001, right is 90.0000 and epsilon is 0.0001, this method
/// might not necessarily return true. I have seen these values produce a
/// diffence of 0.00010000000000332, which is actually greater than 0.0001.
/// You must take this fuzzyness into account in your client classes and
/// unit tests. This is precisely why we have these epsilon methods in the
/// first place!
///
public class MathUtil
{
///
/// Tests whether two float values are equal, within an acceptable margin
/// of difference.
///
/// the left side of the comparison
/// the right side of the comparison
/// the margin of difference
/// true if the values are equal, within epsilon; otherwise, false
///
///
///
public static bool FloatEqualTo(float left, float right, float epsilon)
{
return Math.Abs(left - right) <= epsilon;
}
///
/// Tests whether a float value is greater than another float value,
/// within an acceptable margin of difference.
///
/// the left side of the comparison
/// the right side of the comparison
/// the margin of difference
/// true if left is greater than right, within epsilon; otherwise, false
public static bool FloatGreaterThan(float left, float right, float epsilon)
{
// delegate
return FloatGreaterThan(left, right, epsilon, false);
}
///
/// Tests whether a float value is greater than or equal to another float
/// value, within an acceptable margin of difference.
///
/// the left side of the comparison
/// the right side of the comparison
/// the margin of difference
/// true if left is greater than or equal to right, within epsilon; otherwise, false
public static bool FloatGreaterThanOrEqualTo(float left, float right, float epsilon)
{
// delegate
return FloatGreaterThan(left, right, epsilon, true);
}
// impl
private static bool FloatGreaterThan(float left, float right, float epsilon, bool orEqualTo)
{
if (FloatEqualTo(left, right, epsilon))
{
// within epsilon, so considered equal
return orEqualTo;
}
return left > right;
}
///
/// Tests whether a float value is less than another float value,
/// within an acceptable margin of difference.
///
/// the left side of the comparison
/// the right side of the comparison
/// the margin of difference
/// true if left is less than right, within epsilon; otherwise, false
public static bool FloatLessThan(float left, float right, float epsilon)
{
// delegate
return FloatLessThan(left, right, epsilon, false);
}
///
/// Tests whether a float value is less than or equal to another float value,
/// within an acceptable margin of difference.
///
/// the left side of the comparison
/// the right side of the comparison
/// the margin of difference
/// true if left is less than or equal toright, within epsilon; otherwise, false
public static bool FloatLessThanOrEqualTo(float left, float right, float epsilon)
{
// delegate
return FloatLessThan(left, right, epsilon, true);
}
// impl
private static bool FloatLessThan(float left, float right, float epsilon, bool orEqualTo)
{
if (FloatEqualTo(left, right, epsilon))
{
// within epsilon, so considered equal
return orEqualTo;
}
return left < right;
}
///
/// Tests whether two double values are equal, within an acceptable margin
/// of difference.
///
/// the left side of the comparison
/// the right side of the comparison
/// the margin of difference
/// true if the values are equal, within epsilon; otherwise, false
///
public static bool DoubleEqualTo(double left, double right, double epsilon)
{
return Math.Abs(left - right) <= epsilon;
}
///
/// Tests whether a double value is greater than another double value,
/// within an acceptable margin of difference.
///
/// the left side of the comparison
/// the right side of the comparison
/// the margin of difference
/// true if left is greater than right, within epsilon; otherwise, false
public static bool DoubleGreaterThan(double left, double right, double epsilon)
{
// delegate
return DoubleGreaterThan(left, right, epsilon, false);
}
///
/// Tests whether a double value is greater than or equal to another double
/// value, within an acceptable margin of difference.
///
/// the left side of the comparison
/// the right side of the comparison
/// the margin of difference
/// true if left is greater than or equal to right, within epsilon; otherwise, false
public static bool DoubleGreaterThanOrEqualTo(double left, double right, double epsilon)
{
// delegate
return DoubleGreaterThan(left, right, epsilon, true);
}
// impl
private static bool DoubleGreaterThan(double left, double right, double epsilon, bool orEqualTo)
{
if (DoubleEqualTo(left, right, epsilon))
{
// within epsilon, so considered equal
return orEqualTo;
}
return left > right;
}
///
/// Tests whether a double value is less than another double value,
/// within an acceptable margin of difference.
///
/// the left side of the comparison
/// the right side of the comparison
/// the margin of difference
/// true if left is less than right, within epsilon; otherwise, false
public static bool DoubleLessThan(double left, double right, double epsilon)
{
// delegate
return DoubleLessThan(left, right, epsilon, false);
}
///
/// Tests whether a double value is less than or equal to another double
/// value, within an acceptable margin of difference.
///
/// the left side of the comparison
/// the right side of the comparison
/// the margin of difference
/// true if left is less than or equal to right, within epsilon; otherwise, false
public static bool DoubleLessThanOrEqualTo(double left, double right, double epsilon)
{
// delegate
return DoubleLessThan(left, right, epsilon, true);
}
// impl
private static bool DoubleLessThan(double left, double right, double epsilon, bool orEqualTo)
{
if (DoubleEqualTo(left, right, epsilon))
{
// within epsilon, so considered equal
return orEqualTo;
}
return left < right;
}
}
}