coerceZeroD function

double coerceZeroD(
  1. double a,
  2. double maximumAbsoluteError
)

Forces small numbers near zero to zero, according to the specified absolute accuracy.

Zero if |a| is smaller than maximumAbsoluteError, a otherwise. Throws ArgumentError if maximumAbsoluteError is smaller than zero.

Implementation

double coerceZeroD(double a, double maximumAbsoluteError) {
  if (maximumAbsoluteError < 0) {
    throw ArgumentError.value(maximumAbsoluteError, 'maximumAbsoluteError',
        messages.argumentNotNegative);
  }

  if (a.isInfinite || a.isNaN) {
    return a;
  }

  if (a.abs() < maximumAbsoluteError) {
    return 0.0;
  }

  return a;
}