2019-11-19 — Space Nerds in Space — Hunting NaNs
What are NaNs? A better question might be what aren’t NaNs. NaN stands for “Not a Number”, and NaNs are special floating point values that can occur when you attempt to do things like divide by zero or other “impossible” mathematical operations. Most typically in my experience, NaN generation due to dividing by zero happens when you attempt to normalize a vector with zero magnitude. Once NaNs are generated, they typically spread through any calculation in which they are used, corrupting all sorts of calculations and causing weird behavior for anything depending on those calculations (most typically, NPC ship movement.)
Since Space Nerds in Space seems to work pretty well for the most part, I sort of presumed that NaNs weren’t really something I was bumping into much — that my code was correct enough that I wasn’t generally dividing by zero much if at all. And I suppose that must have been mostly true — mostly true in that the game did work well enough, seemingly. But then I tried actually taking a look to see whether any NaNs were scurrying around down in there.
Oh my god… So many NaNs! So the way that you figure out whether you’ve got NaNs is to enable floating point exceptions to terminate your program and produce a core dump. The code to do so looks like this:
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
Then you compile everything without optimization and run it, and see what blows up. Well things blew up *immediately*, leaving a core file for debugging. For a day or so, this cycle continued: Compile, run, explode, debug, fix. Many NaNs knew what it was to be roasted in the depths of the core that day, I can tell you!
After awhile, I slew enough NaN generating bugs that things no longer exploded immediately, but required several minutes of running before exploding. Now, I think I only have one NaN bug left (that I know of). And I have a fix for that one, I’m just not certain my fix is really right, so I want to think about it some more before I commit it (or some better fix).
In any case, if you’re interested in the gory details, you can check out the bug report on github: https://github.com/smcameron/space-nerds-in-space/issues/236







Recent Comments