diff --git a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/MathExtensions.cs b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/MathExtensions.cs index de0dc77c..3c149ed8 100644 --- a/src/UglyToad.PdfPig.DocumentLayoutAnalysis/MathExtensions.cs +++ b/src/UglyToad.PdfPig.DocumentLayoutAnalysis/MathExtensions.cs @@ -15,7 +15,7 @@ /// The mode of the sequence. Returns if the sequence has no mode or if it is not unique. public static float Mode(this IEnumerable array) { - if (array == null || array.Count() == 0) return float.NaN; + if (array == null || !array.Any()) return float.NaN; var sorted = array.GroupBy(v => v).Select(v => (v.Count(), v.Key)).OrderByDescending(g => g.Item1); var mode = sorted.First(); if (sorted.Count() > 1 && mode.Item1 == sorted.ElementAt(1).Item1) return float.NaN; @@ -29,11 +29,32 @@ /// The mode of the sequence. Returns if the sequence has no mode or if it is not unique. public static double Mode(this IEnumerable array) { - if (array == null || array.Count() == 0) return double.NaN; + if (array == null || !array.Any()) return double.NaN; var sorted = array.GroupBy(v => v).Select(v => (v.Count(), v.Key)).OrderByDescending(g => g.Item1); var mode = sorted.First(); if (sorted.Count() > 1 && mode.Item1 == sorted.ElementAt(1).Item1) return double.NaN; return mode.Key; } + + /// + /// Test for almost equality to 0. + /// + /// + /// + public static bool AlmostEqualsToZero(this double number, double epsilon = 1e-5) + { + return (number > -epsilon) && (number < epsilon); + } + + /// + /// Test for almost equality. + /// + /// + /// + /// + public static bool AlmostEquals(this double number, double other, double epsilon = 1e-5) + { + return AlmostEqualsToZero(number - other, epsilon); + } } }