File: Numerics.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (216 lines) | stat: -rw-r--r-- 7,511 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System;

namespace NUnit.Framework.Constraints
{
	/// <summary>
	/// The Numerics class contains common operations on numeric values.
	/// </summary>
	public class Numerics
	{
		#region Numeric Type Recognition
		/// <summary>
		/// Checks the type of the object, returning true if
		/// the object is a numeric type.
		/// </summary>
		/// <param name="obj">The object to check</param>
		/// <returns>true if the object is a numeric type</returns>
		public static bool IsNumericType(Object obj)
		{
			return IsFloatingPointNumeric( obj ) || IsFixedPointNumeric( obj );
		}

		/// <summary>
		/// Checks the type of the object, returning true if
		/// the object is a floating point numeric type.
		/// </summary>
		/// <param name="obj">The object to check</param>
		/// <returns>true if the object is a floating point numeric type</returns>
		public static bool IsFloatingPointNumeric(Object obj)
		{
			if (null != obj)
			{
				if (obj is double) return true;
				if (obj is float) return true;

				if (obj is System.Double) return true;
				if (obj is System.Single) return true;
			}
			return false;
		}
		/// <summary>
		/// Checks the type of the object, returning true if
		/// the object is a fixed point numeric type.
		/// </summary>
		/// <param name="obj">The object to check</param>
		/// <returns>true if the object is a fixed point numeric type</returns>
		public static bool IsFixedPointNumeric(Object obj)
		{
			if (null != obj)
			{
				if (obj is byte) return true;
				if (obj is sbyte) return true;
				if (obj is decimal) return true;
				if (obj is int) return true;
				if (obj is uint) return true;
				if (obj is long) return true;
				if (obj is short) return true;
				if (obj is ushort) return true;

				if (obj is System.Byte) return true;
				if (obj is System.SByte) return true;
				if (obj is System.Decimal) return true;
				if (obj is System.Int32) return true;
				if (obj is System.UInt32) return true;
				if (obj is System.Int64) return true;
				if (obj is System.UInt64) return true;
				if (obj is System.Int16) return true;
				if (obj is System.UInt16) return true;
			}
			return false;
		}
		#endregion

		#region Numeric Equality
        /// <summary>
        /// Test two numeric values for equality, performing the usual numeric 
        /// conversions and using a provided or default tolerance. If the value 
        /// referred to by tolerance is null, this method may set it to a default.
        /// </summary>
        /// <param name="expected">The expected value</param>
        /// <param name="actual">The actual value</param>
        /// <param name="tolerance">A reference to the numeric tolerance in effect</param>
        /// <returns>True if the values are equal</returns>
		public static bool AreEqual( object expected, object actual, ref object tolerance )
		{
            if (IsFloatingPointNumeric(expected) || IsFloatingPointNumeric(actual))
                return AreEqual(Convert.ToDouble(expected), Convert.ToDouble(actual), ref tolerance);

			if ( expected is decimal || actual is decimal )
				return AreEqual( Convert.ToDecimal(expected), Convert.ToDecimal(actual), Convert.ToDecimal(tolerance) );
			
			if ( expected is ulong || actual is ulong )
				return AreEqual( Convert.ToUInt64(expected), Convert.ToUInt64(actual), Convert.ToUInt64(tolerance) );
		
			if ( expected is long || actual is long )
				return AreEqual( Convert.ToInt64(expected), Convert.ToInt64(actual), Convert.ToInt64(tolerance) );
			
			if ( expected is uint || actual is uint )
				return AreEqual( Convert.ToUInt32(expected), Convert.ToUInt32(actual), Convert.ToUInt32(tolerance) );

			return AreEqual( Convert.ToInt32(expected), Convert.ToInt32(actual), Convert.ToInt32(tolerance) );
		}

		private static bool AreEqual( double expected, double actual, ref object tolerance )
		{
            if (double.IsNaN(expected) && double.IsNaN(actual))
                return true;
            // handle infinity specially since subtracting two infinite values gives 
            // NaN and the following test fails. mono also needs NaN to be handled
            // specially although ms.net could use either method.
            if (double.IsInfinity(expected) || double.IsNaN(expected) || double.IsNaN(actual))
                return expected.Equals(actual);

            if (tolerance != null)
                return Math.Abs(expected - actual) <= Convert.ToDouble(tolerance);

            if (GlobalSettings.DefaultFloatingPointTolerance > 0.0d
                && !double.IsNaN(expected) && !double.IsInfinity(expected))
            {
                tolerance = GlobalSettings.DefaultFloatingPointTolerance;
                return Math.Abs(expected - actual) <= GlobalSettings.DefaultFloatingPointTolerance;
            }

			return expected.Equals( actual );
		}

		private static bool AreEqual( decimal expected, decimal actual, decimal tolerance )
		{
			if ( tolerance > 0m )
				return Math.Abs(expected - actual) <= tolerance;
				
			return expected.Equals( actual );
		}

		private static bool AreEqual( ulong expected, ulong actual, ulong tolerance )
		{
			if ( tolerance > 0ul )
			{
				ulong diff = expected >= actual ? expected - actual : actual - expected;
				return diff <= tolerance;
			}

			return expected.Equals( actual );
		}

		private static bool AreEqual( long expected, long actual, long tolerance )
		{
			if ( tolerance > 0L )
				return Math.Abs(expected - actual) <= tolerance;

			return expected.Equals( actual );
		}

		private static bool AreEqual( uint expected, uint actual, uint tolerance )
		{
			if ( tolerance > 0 )
			{
				uint diff = expected >= actual ? expected - actual : actual - expected;
				return diff <= tolerance;
			}
				
			return expected.Equals( actual );
		}

		private static bool AreEqual( int expected, int actual, int tolerance )
		{
			if ( tolerance > 0 )
				return Math.Abs(expected - actual) <= tolerance;
				
			return expected.Equals( actual );
		}
		#endregion

		#region Numeric Comparisons 
        /// <summary>
        /// Compare two numeric values, performing the usual numeric conversions.
        /// </summary>
        /// <param name="expected">The expected value</param>
        /// <param name="actual">The actual value</param>
        /// <returns></returns>
		public static int Compare( IComparable expected, object actual )
		{
			if ( expected == null )
				throw new ArgumentException( "Cannot compare using a null reference", "expected" );

			if ( actual == null )
				throw new ArgumentException( "Cannot compare to null reference", "actual" );

			if( IsNumericType( expected ) && IsNumericType( actual ) )
			{
				if ( IsFloatingPointNumeric(expected) || IsFloatingPointNumeric(actual) )
					return Convert.ToDouble(expected).CompareTo(Convert.ToDouble(actual));

				if ( expected is decimal || actual is decimal )
					return Convert.ToDecimal(expected).CompareTo(Convert.ToDecimal(actual));
			
				if ( expected is ulong || actual is ulong )
					return Convert.ToUInt64(expected).CompareTo(Convert.ToUInt64(actual));
		
				if ( expected is long || actual is long )
					return Convert.ToInt64(expected).CompareTo(Convert.ToInt64(actual));
			
				if ( expected is uint || actual is uint )
					return Convert.ToUInt32(expected).CompareTo(Convert.ToUInt32(actual));

				return Convert.ToInt32(expected).CompareTo(Convert.ToInt32(actual));
			}
			else
				return expected.CompareTo(actual);
		}
		#endregion

		private Numerics()
		{
		}
	}
}