Floats aren’t exact, folks 1


I’ve gotten quite comfortable with working now, and the next two units are nearly ready. But there’s an unfortunate problem, which I had hoped would only affect optional, future features.

Those ideas were multiplayer and a replay system. (This guy expressed confidence in the feature.) And I agree it would make sense!

But there is a big issue regarding how Unity works–it’s non-deterministic. Basically, even if you give the program the same exact inputs, the output is not guaranteed. I’ll have to work on making the program deterministic for these features to even be considered.

In addition, distances are being measured as a “float”. That’s a number that can represent fractional value, like 0.3. But because floats are stored as binary (0’s and 1’s), most floats are actually close approximations, not exact values.

This is the reason behind some unexpected results. It’s affecting the Cheese Grater, one of the next units to be adeed. Normally, any unit that attacks it in melee is supposed to receive damage back as well. Whether the attacking unit is in melee range is determined by measuring the distance between their hit boxes. But the imprecision of the numbers and timing mean things like this happen:

hitboxes

The Bacon Knight should be taking damage from hitting the Cheese Grater. But it doesn’t, as the Cheese Grater doesn’t consider the Bacon Knight close enough to damage. (Of course, sometimes the numbers work out so that the behavior works correctly!) So, gotta deal with this.


Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Floats aren’t exact, folks