Signed variable

2,032pages on
this wiki
Add New Page
Add New Page Talk0

Signedness 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 on most machines 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.

Some older machines use other signed number representations, ending up with a different range of integers than two's complement. These notations are rarely seen on the average person's desktop. (Excess-N notation is still used for exponents of floating point numbers, but is rare for integers.)

Signed variables are ubiquitious in the NetHack source code, even where it doesn't particularly make sense. This is possibly due to different coding practices that prevailed among early developers -- Hack 1.0 was released in December 1984, well before ISO C existed, and developers wrote their C code in quite different styles than what prevails today. Unsigned variables were possibly a new addition to C, and then-active programmers were not yet accustomed to using them.

Score, for example, is a signed variable (whose magnitude 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.

Whether a variable is signed or not affects what happens during integer overflow.

Also on Fandom

Random Wiki