@@ -11,42 +11,30 @@ public static class PrecisionExtensions
1111 /// Accurate way to convert float to decimal by converting to string first. Avoids tolerance issues.
1212 /// </summary>
1313 public static decimal ToDecimal ( this float value )
14- {
15- return decimal . Parse ( value . ToString ( CultureInfo . InvariantCulture ) ) ;
16- }
14+ => decimal . Parse ( value . ToString ( CultureInfo . InvariantCulture ) ) ;
1715
1816 /// <summary>
1917 /// Shortcut for validating a if a floating point value is considered zero (within epsilon tolerance).
2018 /// </summary>
21- public static bool IsZero ( this float value )
22- {
23- return IsPreciseEqual ( value , 0f ) ;
24- }
19+ public static bool IsZero ( this float value ) => IsPreciseEqual ( value , 0f ) ;
2520
2621 /// <summary>
2722 /// Shortcut for validating a if a double tolerance floating point value is considered zero (within epsilon tolerance).
2823 /// </summary>
29- public static bool IsZero ( this double value )
30- {
31- return IsPreciseEqual ( value , 0d ) ;
32- }
24+ public static bool IsZero ( this double value ) => IsPreciseEqual ( value , 0d ) ;
3325
3426
3527 /// <summary>
3628 /// Shortcut for validating a if a double tolerance floating point value is considered zero (within provided tolerance).
3729 /// </summary>
3830 public static bool IsNearZero ( this double value , double precision = 0.001 )
39- {
40- return IsNearEqual ( value , 0d , precision ) ;
41- }
31+ => IsNearEqual ( value , 0d , precision ) ;
4232
4333 /// <summary>
4434 /// Shortcut for returning true zero if a double tolerance floating point value is considered zero (within epsilon tolerance).
4535 /// </summary>
4636 public static double FixZero ( this double value )
47- {
48- return ! value . Equals ( 0 ) && value . IsZero ( ) ? 0 : value ;
49- }
37+ => ! value . Equals ( 0 ) && value . IsZero ( ) ? 0 : value ;
5038
5139 static double ReturnZeroIfFinite ( this float value )
5240 {
@@ -78,25 +66,24 @@ public static int DecimalPlaces(this double source)
7866 /// Shortcut for validating a if a floating point value is close enough to another addValue using the given tolerance tolerance.
7967 /// </summary>
8068 public static bool IsNearEqual ( this float a , float b , float tolerance )
81- {
82- return a . Equals ( b ) || float . IsNaN ( a ) && float . IsNaN ( b ) || Math . Abs ( a - b ) < tolerance ;
83- }
69+ => a . Equals ( b )
70+ || float . IsNaN ( a ) && float . IsNaN ( b )
71+ || Math . Abs ( a - b ) < tolerance ;
8472
8573 /// <summary>
8674 /// Shortcut for validating a if a double precision floating point value is close enough to another addValue using the given tolerance tolerance.
8775 /// </summary>
8876 public static bool IsNearEqual ( this double a , double b , double tolerance )
89- {
90- return a . Equals ( b ) || double . IsNaN ( a ) && double . IsNaN ( b ) || Math . Abs ( a - b ) < tolerance ;
91- }
77+ => a . Equals ( b )
78+ || double . IsNaN ( a ) && double . IsNaN ( b )
79+ || Math . Abs ( a - b ) < tolerance ;
9280
9381 /// <summary>
9482 /// Shortcut for validating a if a decimal addValue is close enough to another addValue using the given tolerance tolerance.
9583 /// </summary>
9684 public static bool IsNearEqual ( this decimal a , decimal b , decimal tolerance )
97- {
98- return a . Equals ( b ) || Math . Abs ( a - b ) < tolerance ;
99- }
85+ => a . Equals ( b )
86+ || Math . Abs ( a - b ) < tolerance ;
10087
10188 /// <summary>
10289 /// Shortcut for validating a if a decimal addValue is close enough to another addValue using the given tolerance tolerance.
@@ -117,46 +104,39 @@ public static bool IsRelativeNearEqual(this double a, double b, uint minDecimalP
117104 /// Validates if values are equal within epsilon tolerance.
118105 /// </summary>
119106 public static bool IsPreciseEqual ( this double a , double b , bool stringValidate = false )
120- {
121- return IsNearEqual ( a , b , double . Epsilon )
122- || ( stringValidate && ! double . IsNaN ( a ) && ! double . IsNaN ( b ) && a . ToString ( CultureInfo . InvariantCulture ) == b . ToString ( CultureInfo . InvariantCulture ) ) ;
123- }
107+ => IsNearEqual ( a , b , double . Epsilon )
108+ || ( stringValidate && ! double . IsNaN ( a ) && ! double . IsNaN ( b ) && a . ToString ( CultureInfo . InvariantCulture ) == b . ToString ( CultureInfo . InvariantCulture ) ) ;
124109
125110
126111 /// <summary>
127112 /// Validates if values are equal within epsilon tolerance.
128113 /// </summary>
129114 public static bool IsPreciseEqual ( this float a , float b , bool stringValidate = false )
130- {
131- return IsNearEqual ( a , b , float . Epsilon ) || ( stringValidate && ! float . IsNaN ( a ) && ! float . IsNaN ( b ) && a . ToString ( CultureInfo . InvariantCulture ) == b . ToString ( CultureInfo . InvariantCulture ) ) ;
132- }
115+ => IsNearEqual ( a , b , float . Epsilon )
116+ || ( stringValidate && ! float . IsNaN ( a ) && ! float . IsNaN ( b ) && a . ToString ( CultureInfo . InvariantCulture ) == b . ToString ( CultureInfo . InvariantCulture ) ) ;
133117
134118 /// <summary>
135119 /// Validates if values are equal within epsilon tolerance.
136120 /// </summary>
137121 public static bool IsPreciseEqual ( this double ? a , double ? b , bool stringValidate = false )
138- {
139- return ! a . HasValue && ! b . HasValue || a . HasValue && b . HasValue && a . Value . IsPreciseEqual ( b . Value , stringValidate ) ;
140- }
122+ => ! a . HasValue && ! b . HasValue
123+ || a . HasValue && b . HasValue && a . Value . IsPreciseEqual ( b . Value , stringValidate ) ;
141124
142125 /// <summary>
143126 /// Validates if values are equal within epsilon tolerance.
144127 /// </summary>
145128 public static bool IsPreciseEqual ( this float ? a , float ? b , bool stringValidate = false )
146- {
147- return ! a . HasValue && ! b . HasValue || a . HasValue && b . HasValue && a . Value . IsPreciseEqual ( b . Value , stringValidate ) ;
148- }
149-
150-
129+ => ! a . HasValue && ! b . HasValue
130+ || a . HasValue && b . HasValue && a . Value . IsPreciseEqual ( b . Value , stringValidate ) ;
151131
152132 /// <summary>
153133 /// Shortcut for validating a if a potential floating pointvalue is close enough to another addValue using the given tolerance tolerance.
154134 /// </summary>
155135 public static bool IsNearEqual ( this IComparable a , IComparable b , IComparable tolerance )
156136 {
157- if ( a == null )
137+ if ( a is null )
158138 throw new NullReferenceException ( ) ;
159- if ( b == null )
139+ if ( b is null )
160140 throw new ArgumentNullException ( nameof ( b ) ) ;
161141 Contract . EndContractBlock ( ) ;
162142
@@ -201,9 +181,7 @@ public static double ToDouble(this float value)
201181 /// Accurate way to convert possible float to double by converting to string first. Avoids tolerance issues.
202182 /// </summary>
203183 public static double ToDouble ( this float ? value )
204- {
205- return value ? . ToDouble ( ) ?? double . NaN ;
206- }
184+ => value ? . ToDouble ( ) ?? double . NaN ;
207185
208186
209187 /// <summary>
0 commit comments