ow Integers Should Work

bearophile bearophileHUGS at lycos.com
Wed Dec 7 02:41:52 PST 2011


Kagamin:

>I played a game, where experience counter was int32, it wasn't meant to overflow during normal play, but it allowed console commands, which could be a lot of fun, so I made xp overflow to negative values, it didn't make any trouble, just in the case it used checked arithmetic, it would crash, not so much fun.<

If that game is written in C/C++ then those languages don't define what happens when you add 1 to the max signed integer. This means crashing the game in that case is OK according to the specs of those languages.

And even if that experience counter is an unsigned integer, that in C/C++ has to wrap to zero, how many signed or unsigned 32 bit integers there are in a game? A lot. I presume that for most of them a silent wrap to the minimum integer doesn't lead to a fun game. As it usually happens in complex systems, there are far more ways for that system to not work than to work. In most cases implicit modular arithmetic doesn't lead to smooth error behaviours.

Bye,
bearophile


More information about the Digitalmars-d mailing list