DoubleRange constructor

DoubleRange([
  1. double? a,
  2. double? b,
  3. double? c
])

Creates a virtual range of numbers containing an arithmetic progressions of double values.

The constructor called without any arguments returns the empty range. For example, DoubleRange() yields <double>[].

The constructor called with one argument returns the range of all numbers up to, but excluding the end. For example, DoubleRange(3.0) yields <double>[0.0, 1.0, 2.0].

The constructor called with two arguments returns the range between the two numbers (including the start, but excluding the end). For example, DoubleRange(3.0, 6.0) yields <double>[3.0, 4.0, 5.0].

The constructor called with three arguments returns the range between the first two numbers (including the start, but excluding the end) and the step value. For example, DoubleRange(1.0, 7.0, 2.1) yields <double>[1.0, 3.1, 5.2].

Implementation

factory DoubleRange([double? a, double? b, double? c]) {
  var start = 0.0;
  var end = 0.0;
  var step = 1.0;
  if (c != null) {
    start = a!;
    end = b!;
    step = c;
  } else if (b != null) {
    start = a!;
    end = b;
    step = start <= end ? 1.0 : -1.0;
  } else if (a != null) {
    end = a;
  }
  if (start < end) {
    if (step == 1.0) {
      return DoubleRange._(start, end, step, (end - start).ceil());
    } else if (step > 0.0) {
      return DoubleRange._(start, end, step, ((end - start) / step).ceil());
    }
  } else if (start > end) {
    if (step == -1.0) {
      return DoubleRange._(start, end, step, (start - end).ceil());
    } else if (step < 0.0) {
      return DoubleRange._(start, end, step, ((start - end) / -step).ceil());
    }
  } else {
    if (step != 0) {
      return DoubleRange._(start, end, step, 0);
    }
  }
  throw ArgumentError.value(
      step, 'step', 'Invalid step size for range $start..$end');
}