roundHalfEven function

double roundHalfEven(
  1. double x
)

x to the nearest whole number, ties to even, as WGSL's round.

Implementation

double roundHalfEven(double x) {
  if (!x.isFinite) return x;
  final floor = x.floorToDouble();
  final fraction = x - floor;
  if (fraction < 0.5) return floor;
  if (fraction > 0.5) return floor + 1.0;
  return floor % 2.0 == 0.0 ? floor : floor + 1.0;
}