I’m writing a path tracer and have to collect an average over a large number of samples per pixel. I get significant visual differences between a 1024-samples run and a 16384-samples run; the 16384-samples run is darker. I conjecture that this is because the 16384-samples image runs into floating-point precision errors. I average the color values by dividing each value by 16384, then adding them together.
Is there a way to average a large, difficult to compute set of numbers with a known magnitude, while minimizing rounding error? Preferably without requiring non-constant memory, and definitely without discarding any samples?
You probably want the Kahan summation algorithm. This is a simple and effective way of minimising cumulative rounding errors when summing a large number of points with finite precision floating point arithmetic.