If only .Net had class or interface like Number in Java, you could well have put something like
// Doesn't compile; just the idea
bool IsEqual<T>(this T a, T b, T offset)
where T: Number { // <- T can be any integer or floating point type
a = Math.Abs(a);
....
Unfortunately, .Net doesn't provide such an interface and so you have to implement overload versions of IsEqual
:
bool IsEqual(this Double a, Double b, Double offset) {
return (Math.Abs(a - b) < offset);
}
bool IsEqual(this Single a, Single b, Single offset) {
return (Math.Abs(a - b) < offset);
}
bool IsEqual(this long a, long b, long offset) {
return (Math.Abs(a - b) < offset);
}
bool IsEqual(this int a, int b, int offset) {
return (Math.Abs(a - b) < offset);
}
...