triangularPdf function

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

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

The maximum occurs at (max - min) / 2.

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

Implementation

num triangularPdf(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 0.0;
  final h = (max - min) / 2;
  if (x > min && x < min + h) {
    return (x - min) / (h * h);
  } else {
    return (max - x) / (h * h);
  }
}