↓ Skip to main content

“The trick here is to pre-divide the values before adding.”

Raymond Chendevblogs.microsoft.com ↗

Averaging two numbers is a one-liner that overflows. The safe versions fill a long post, with compiler output for half a dozen CPUs. One of them, three lines of C, held a U.S. patent until 2016. Somebody got paid to own (a / 2) + (b / 2).