Skip to content

Commit 74115bf

Browse files
author
Oren (electricessence)
committed
Some important conditional fixes.
1 parent a5edddf commit 74115bf

7 files changed

Lines changed: 36 additions & 31 deletions

File tree

IProcedureResult.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
namespace Open.Numeric
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace Open.Numeric
24
{
3-
interface IProcedureResult<T>
5+
// ReSharper disable once ArrangeTypeModifiers
6+
[SuppressMessage("ReSharper", "UnusedMemberInSuper.Global")]
7+
interface IProcedureResult<out T>
48
{
59
int Count { get; }
610
T Sum { get; }

Numeric.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ public static double ToDouble(object value)
4444
if (value == null)
4545
return double.NaN;
4646

47-
return value is float ? ((float)value).ToDouble() : Convert.ToDouble(value);
47+
return value is float f
48+
? f.ToDouble()
49+
: Convert.ToDouble(value);
4850
}
4951
}
5052
}

Precision.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static int DecimalPlaces(this double source)
6666
if (source.IsNaN())
6767
return 0;
6868

69-
var valueString = source.ToString();
69+
var valueString = source.ToString(CultureInfo.InvariantCulture); // To
7070
var index = valueString.IndexOf('.');
7171
return index == -1 ? 0 : valueString.Length - index - 1;
7272
}
@@ -118,7 +118,8 @@ public static bool IsRelativeNearEqual(this double a, double b, uint minDecimalP
118118
/// </summary>
119119
public static bool IsPreciseEqual(this double a, double b, bool stringValidate = false)
120120
{
121-
return IsNearEqual(a, b, double.Epsilon) || (stringValidate && !double.IsNaN(a) && !double.IsNaN(b) && a.ToString() == b.ToString());
121+
return IsNearEqual(a, b, double.Epsilon)
122+
|| (stringValidate && !double.IsNaN(a) && !double.IsNaN(b) && a.ToString(CultureInfo.InvariantCulture) == b.ToString(CultureInfo.InvariantCulture));
122123
}
123124

124125

@@ -127,7 +128,7 @@ public static bool IsPreciseEqual(this double a, double b, bool stringValidate =
127128
/// </summary>
128129
public static bool IsPreciseEqual(this float a, float b, bool stringValidate = false)
129130
{
130-
return IsNearEqual(a, b, float.Epsilon) || (stringValidate && !float.IsNaN(a) && !float.IsNaN(b) && a.ToString() == b.ToString());
131+
return IsNearEqual(a, b, float.Epsilon) || (stringValidate && !float.IsNaN(a) && !float.IsNaN(b) && a.ToString(CultureInfo.InvariantCulture) == b.ToString(CultureInfo.InvariantCulture));
131132
}
132133

133134
/// <summary>
@@ -151,20 +152,26 @@ public static bool IsPreciseEqual(this float? a, float? b, bool stringValidate =
151152
/// <summary>
152153
/// Shortcut for validating a if a potential floating pointvalue is close enough to another addValue using the given tolerance tolerance.
153154
/// </summary>
154-
public static bool IsNearEqual(this IComparable a, IComparable b, double tolerance)
155+
public static bool IsNearEqual(this IComparable a, IComparable b, IComparable tolerance)
155156
{
156157
if (a == null)
157158
throw new NullReferenceException();
158159
if (b == null)
159160
throw new ArgumentNullException(nameof(b));
160161
Contract.EndContractBlock();
161162

162-
if (a is float)
163-
return ((float)a).IsNearEqual((float)b, tolerance);
164-
if (a is double)
165-
return ((double)a).IsNearEqual((double)b, tolerance);
166-
if (a is decimal)
167-
return ((decimal)a).IsNearEqual((decimal)b, tolerance);
163+
if (a.Equals(b))
164+
return true;
165+
166+
switch (a)
167+
{
168+
case float f:
169+
return IsNearEqual(f, (float)b, (float)tolerance);
170+
case double d:
171+
return IsNearEqual(d, (double)b, (double)tolerance);
172+
case decimal @decimal:
173+
return IsNearEqual(@decimal, (decimal)b, (decimal)tolerance);
174+
}
168175

169176
throw new InvalidCastException();
170177
}
@@ -199,21 +206,20 @@ public static double ToDouble(this float value)
199206
/// </summary>
200207
public static double ToDouble(this float? value)
201208
{
202-
return value.HasValue ? value.Value.ToDouble() : double.NaN;
209+
return value?.ToDouble() ?? double.NaN;
203210
}
204211

205212

206213
/// <summary>
207214
/// Accurate way to convert a possible float to double by rounding finite values to a decimal point tolerance level.
208215
/// </summary>
209-
/// <param name="tolerance"></param>
210216
public static double ToDouble(this float? value, int precision)
211217
{
212218
if (precision < 0 || precision > 15)
213219
throw new ArgumentOutOfRangeException(nameof(precision), precision, "Must be bewteen 0 and 15.");
214220
Contract.EndContractBlock();
215221

216-
return value.HasValue ? value.Value.ToDouble(precision) : double.NaN;
222+
return value?.ToDouble(precision) ?? double.NaN;
217223
}
218224

219225

ProcedureResult.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
using Open.Numeric.Precision;
33
using System;
4+
using System.Globalization;
45

56
namespace Open.Numeric
67
{
@@ -26,7 +27,7 @@ public int CompareTo(ProcedureResult other)
2627
{
2728
var a = Average;
2829
var b = other.Average;
29-
if (a.IsNearEqual(b, 0.00000001) && a.ToString() == b.ToString()) return 0; // We hate precision issues. :( 1==1 dammit!
30+
if (a.IsNearEqual(b, 0.00000001) && a.ToString(CultureInfo.InvariantCulture) == b.ToString(CultureInfo.InvariantCulture)) return 0; // We hate precision issues. :( 1==1 dammit!
3031
if (a < b || double.IsNaN(a) && !double.IsNaN(b)) return -1;
3132
if (a > b || !double.IsNaN(a) && double.IsNaN(b)) return +1;
3233
if (Count < other.Count) return -1;

ProcedureResults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ProcedureResults(ReadOnlySpan<double> sum, int count)
1818

1919
static double[] SumValues(ReadOnlySpan<double> a, ReadOnlySpan<double> b)
2020
{
21-
if (a.Length != a.Length)
21+
if (a.Length != b.Length)
2222
throw new ArgumentException("Length mismatch.");
2323

2424
var len = a.Length;

Random.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@
55

66
using System;
77
using System.Collections.Generic;
8+
using System.Diagnostics.CodeAnalysis;
89
using System.Linq;
910

1011
namespace Open.Numeric
1112
{
1213
public static class RandomUtilities
1314
{
1415
static readonly Lazy<Random> R = new Lazy<Random>(() => new Random());
15-
public static Random Random
16-
{
17-
get
18-
{
19-
return R.Value;
20-
}
21-
}
16+
public static Random Random => R.Value;
2217

18+
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
2319
public static bool TryRandomPluck<T>(this LinkedList<T> source, out T value)
2420
{
2521
if (source.Count == 0)

UEnumerable.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public static IEnumerable<ushort> Range(ushort start, ushort count)
1515
}
1616

1717
public static IEnumerable<ushort> Range(ushort count)
18-
{
19-
return Range(0, count);
20-
}
18+
=> Range(0, count);
2119

2220

2321
public static IEnumerable<uint> Range(uint start, uint count)
@@ -31,8 +29,6 @@ public static IEnumerable<uint> Range(uint start, uint count)
3129
}
3230

3331
public static IEnumerable<uint> Range(uint count)
34-
{
35-
return Range(0, count);
36-
}
32+
=> Range(0, count);
3733
}
3834
}

0 commit comments

Comments
 (0)