triangularCdf function

num triangularCdf(
  1. num x,
  2. num min,
  3. num max
)

Triangular cumulative probability density function with non-zero support over the interval (min, max).

The maximum of the probability density occurs at (max - min) / 2.

Throws an error of type ErrorOfType<InvalidFunctionParameter> if min >= max.

Implementation

num triangularCdf(num x, num min, num max) {
  if (min >= max) {
    throw ErrorOfType<InvalidFunctionParameter>(
      invalidState: 'min: $min >= max: $max',
      expectedState: 'min < max',
    );
  }
  if (x <= min) return 0.0;
  if (x >= max) return 1.0;
  final range = max - min;
  final factor = 2.0 / (range * range);
  if (x > min && x < min + 0.5 * range) {
    return factor * (x - min) * (x - min);
  } else {
    return 1.0 - factor * (x - max) * (x - max);
  }
}