Summary: to compute (a + b) / 2 and get the best answer for arbitrary inputs, you want:<p>0 if a == –b<p>±realmax if a or b is ±∞<p>otherwise compute (a - a/2) + b/2 at higher precision, then round to the nearest float
Similar things are relevant for calculations with integers (like array indexes), if you haven‘t yet, read this classic post about it: <a href="https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html?m=1" rel="nofollow">https://ai.googleblog.com/2006/06/extra-extra-read-all-about...</a> (reader mode makes it accessible on iPhones, why don‘t people test their layouts with common browsers?).
This post is a great reminder of just how much we need to respect the people who write or have written system libraries. Even simple things can be so complicated when you have to factor for all eventualities. A rocket could blow up because the library function you wrote didn't account for floating point arithmetic correctly.
Take an average in a range! Neat. Floating point uses exponential indexing and gets blurry. Taking a chunk of an exponential range and averaging that seems to solve the inaccuracy (!)