How do you correctly calculate the average of large sets of numbers with a 32-bit floating point?

I am writing a path tracer and must collect the average over a large number of samples per pixel. I get significant visual differences between the runs of 1024 samples and the mileage of 16384; mileage samples 16384 darker. I assume this is because the 16384 sample image works with floating point precision errors. I average the color values ​​by dividing each value by 16384 and then adding them together.

Is there a way to average a large, difficult to compute a set of numbers with a known value, minimizing the rounding error? Preferably, without requiring volatile memory and, of course, without discarding any samples?

+4
source share
2 answers

You probably need a Kahan summation algorithm . This is a simple and effective way to minimize cumulative rounding errors when summing a large number of points with floating point arithmetic with finite accuracy.

+5
source

Since you are divisible by 2, and your numbers are not too small, this step should not affect the accuracy of the result. You simply subtract 14 from the exponent.

What is the number of bits in your samples.

Floats give you 24 bits of accuracy. If you have 2 ^ 14 = 16384 samples, then when you add them, you gradually lose accuracy until at some point it is lost after 24-14 = 10th bit. In other words: at this moment you save only 3 decimal digits.

Is it possible to use int as a battery or even uint? This way you save 8 extra bits, twice as much as the difference between 1024 and 16384 samples.

There is a second, completely different option. I don’t know what the range of your samples is, but if they are the same size, you can subtract the approximate average from each value, average the differences and add an approximate average result at the end.

How much you gain by this method depends on how good your initial approximation of the average value is and how close the values ​​are to the average. Therefore, I would say that it is less reliable in your situation.

+1
source

All Articles