@@ -101,29 +101,33 @@ public static T Difference<T>(T a, T b) where T : INumber<T>
101101 {
102102 return a - b ;
103103 }
104- public static bool AreNotEqual < T > ( T a , T b ) where T : INumber < T >
104+ public static bool AreNotEqual < T > ( T a , T b ) where T : INumber < T > , IComparable < T >
105105 {
106- return ! a . Equals ( b ) ;
106+ return a ? . CompareTo ( b ) != 0 ;
107107 }
108- public static bool IsGreaterThan < T > ( T a , T b ) where T : INumber < T >
108+ public static bool GreaterThan < T > ( T a , T b ) where T : INumber < T > , IComparable < T >
109109 {
110- return a > b ;
110+ return a ? . CompareTo ( b ) > 0 ;
111111 }
112- public static bool IsLessThan < T > ( T a , T b ) where T : INumber < T >
112+
113+ public static bool LessThan < T > ( T a , T b ) where T : INumber < T > , IComparable < T >
113114 {
114- return a < b ;
115+ return a ? . CompareTo ( b ) < 0 ;
115116 }
116- public static bool IsGreaterThanOrEqual < T > ( T a , T b ) where T : INumber < T >
117+
118+ public static bool GreaterThanOrEqual < T > ( T a , T b ) where T : INumber < T > , IComparable < T >
117119 {
118- return a >= b ;
120+ return a ? . CompareTo ( b ) >= 0 ;
119121 }
120- public static bool IsLessThanOrEqual < T > ( T a , T b ) where T : INumber < T >
122+
123+ public static bool LessThanOrEqual < T > ( T a , T b ) where T : INumber < T > , IComparable < T >
121124 {
122- return a <= b ;
125+ return a ? . CompareTo ( b ) <= 0 ;
123126 }
124- public static bool AreEqual < T > ( T a , T b ) where T : INumber < T >
127+
128+ public static bool AreEqual < T > ( T a , T b ) where T : INumber < T > , IComparable < T >
125129 {
126- return a . Equals ( b ) ;
130+ return a ? . CompareTo ( b ) == 0 ;
127131 }
128132 public static bool AreEqual < T > ( T expected , T actual , T tolerance ) where T : INumber < T >
129133 {
@@ -244,6 +248,18 @@ public static bool IsNaN<T>(T value) where T : INumber<T>
244248 }
245249 return false ;
246250 }
251+ public static bool IsNaN ( object value )
252+ {
253+ if ( value is double d )
254+ {
255+ return double . IsNaN ( d ) ;
256+ }
257+ if ( value is float f )
258+ {
259+ return float . IsNaN ( f ) ;
260+ }
261+ return false ;
262+ }
247263 public static bool IsInfinity < T > ( T value ) where T : INumber < T >
248264 {
249265 if ( T . IsInfinity ( value ) )
@@ -287,10 +303,52 @@ public static T Factorial<T>(T value) where T : INumber<T>
287303 }
288304 public static T Clamp < T > ( T value , T min , T max ) where T : INumber < T >
289305 {
306+ if ( min > max )
307+ {
308+ throw new NotSupportedException ( "min is greater than max value" ) ;
309+ }
290310 if ( value < min ) return min ;
291311 if ( value > max ) return max ;
292312 return value ;
293313 }
314+ public static T Max < T > ( T x , T y ) where T : INumber < T >
315+ {
316+ if ( x != y )
317+ {
318+ if ( ! T . IsNaN ( x ) )
319+ {
320+ return y < x ? x : y ;
321+ }
322+
323+ return x ;
324+ }
325+ return T . IsNegative ( y ) ? x : y ;
326+ }
327+ public static T Min < T > ( T x , T y ) where T : INumber < T >
328+ {
329+ if ( ( x != y ) && ! T . IsNaN ( x ) )
330+ {
331+ return x < y ? x : y ;
332+ }
333+ return T . IsNegative ( x ) ? x : y ;
334+ }
335+ public static int Sign < T > ( T value ) where T : INumber < T >
336+ {
337+ if ( value != T . Zero )
338+ {
339+ return T . IsNegative ( value ) ? - 1 : + 1 ;
340+ }
341+ return 0 ;
342+ }
343+ public static T CopySign < T > ( T value , T sign ) where T : INumber < T >
344+ {
345+ T result = value ;
346+ if ( T . IsNegative ( value ) != T . IsNegative ( sign ) )
347+ {
348+ result = checked ( - result ) ;
349+ }
350+ return result ;
351+ }
294352 public static bool IsInRange < T > ( T value , T min , T max , bool inclusive = true ) where T : INumber < T >
295353 {
296354 if ( inclusive )
@@ -358,6 +416,4 @@ public static T BinomialCoefficient<T>(T n, T k) where T : INumber<T>
358416
359417 return Factorial ( n ) / ( Factorial ( k ) * Factorial ( n - k ) ) ;
360418 }
361-
362-
363419}
0 commit comments