Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// The following method compares any 2 objects and test if they are equal.
- /// It will also compare equality of numbers in a very special way.
- /// Examples:
- /// isequal(Int64.MaxValue, Int64.MaxValue) //is true
- /// isequal(Int64.MaxValue, Int64.MaxValue-1) //is false
- /// isequal(123, 123.0) //is true
- /// isequal(654f, 654d) //is true
- /// </summary>
- public bool isequal(object a, object b)
- {
- if (a == null && b == null) //both are null
- return true;
- if (a == null || b == null) //one is null, the other isn't
- return false;
- if (IsNumber(a) && IsNumber(b))
- {
- if (isFloatingPoint(a) || isFloatingPoint(b))
- {
- double da, db;
- if (Double.TryParse(a.ToString(), out da) && Double.TryParse(b.ToString(), out db))
- return Math.Abs(da - db) < 0.000001;
- }
- else
- {
- if (a.ToString().StartsWith("-") || b.ToString().StartsWith("-"))
- return Convert.ToInt64(a) == Convert.ToInt64(b);
- else
- return Convert.ToUInt64(a) == Convert.ToUInt64(b);
- }
- }
- return a.Equals(b);
- }
- private bool isFloatingPoint(object value)
- {
- if (value is float) return true;
- if (value is double) return true;
- if (value is decimal) return true;
- return false;
- }
- private bool IsNumber(object value)
- {
- if (value is sbyte) return true;
- if (value is byte) return true;
- if (value is short) return true;
- if (value is ushort) return true;
- if (value is int) return true;
- if (value is uint) return true;
- if (value is long) return true;
- if (value is ulong) return true;
- if (value is float) return true;
- if (value is double) return true;
- if (value is decimal) return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement