Floating Point Numeric Stability


nathanielbabiak 2022-10-06 03:15 (Edited)

(Sp4acebar, I'm writing this to continue chatting here, but the intro is written so others can jump in with thoughts/comments too, if they haven't seen the other posts.)

I was messing around on here, on the topic of physics simulations. I was working through some algebra, had it coded up perfectly, and... It. Did. Not. Work.

The code truly was perfect, the only thing it needed was a rational (real) representation of the values in all the variables, and computers don't do that. This console specifically uses FP32. (On modern hardware, 64 bits is common for CPUs, and 32 is still common for dedicated graphics chips.)

So, everywhere in my code where I had originally used an equality to compare two floating point numbers, I needed to consider that the order of operations would introduce some miniscule rounding, and (barring becoming an expert in floating point oddities), the rounding might not be consistent. I ultimately had to change everything over to inequalities instead. What a pain!

If anyone reading this is interested in a resource to learn more, there's an excellent book by Jean Michel that's publicly available online if you search with "PDF" in your query, it's Muller, J., "Handbook of Floating-Point Arithmetic", 2010.

Log in to reply.