public static IEnumerable <double> GetSteps(this double from, double to, double step) { double precision = DoubleHelper.GetPrecision(new double[2] { from, to }); if (DoubleHelper.EqualsWithPrecision(step, 0.0, precision)) { throw new ArgumentOutOfRangeException("step", string.Format("The step={0} is negligebly small for the range of {1} to {2}", (object)step, (object)from, (object)to)); } double x = from; while (!DoubleHelper.EqualsWithPrecision(x, to, precision)) { if (x >= to) { yield break; } else { yield return(x); x += step; } } yield return(to); }
public static IEnumerable <double> GetSteps(this double from, double to, int count) { if (count > 0) { double step = (to - from) / (double)count; double precision = DoubleHelper.GetPrecision(new double[2] { from, to }); double x = from; while (!DoubleHelper.EqualsWithPrecision(x, to, precision)) { if (x >= to) { yield break; } else { yield return(x); x += step; } } yield return(to); } }
public static bool EqualsWithPrecision(this double value1, double value2) { return(DoubleHelper.EqualsWithPrecision(value1, value2, 0.0001)); }