Signed variable

From NetHackWiki
Revision as of 16:50, 26 August 2006 by Eidolos (talk | contribs) (Rewrote first sentence.)
Jump to navigation Jump to search

Signedeness is a property of nearly all of the numeric data types used within the NetHack source code. The word "signed" refers to the fact that these variables can store negative values; unsigned variables cannot. Its use is reponsible for the many seemingly non-round numbers found as limits within the game. For example, the lowest AC attainable is -128, because the AC value is stored in a signed 8-bit variable (schar) which can store up to 256 values: 127 positive numbers, zero, and 128 negative numbers. The lack of symmetry is due to the use of two's complement. An unsigned 8-bit variable would be able to store zero and 255 positive values.

Signed variables are ubiquitious in the NetHack source code, even where it doesn't particularly make sense. Score, for example, is a signed variable (whose magnititude is dependent on machine word size; generally it's 32 bits, but today computers are shifting to 64-bit). Does a negative score make sense? It'd almost certainly be better to have the additional 2147483648 positive score values. Portability might be an issue -- perhaps unsigned variables are less portable than signed ones. Or it could be that the DevTeam simply didn't think anyone would achieve two billion points.