Difference between revisions of "Signed variable"

From NetHackWiki
Jump to navigation Jump to search
(moved info from scumming)
 
(Expanded.)
Line 1: Line 1:
A '''signed variable''' is a data type used within the NetHack [[source code]]. 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 char variable (<code>schar</code>) which can (on most platforms) store up to 256 values: 127 positive numbers, zero, and 128 negative numbers.
+
A '''signed variable''' is a set of 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 (<code>schar</code>) 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 [http://en.wikipedia.org/wiki/Two%27s_complement 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.
{{stub|Would a C-head like to clarify "on most platforms", and expand this article to explain other data types?}}
 
  
 
[[Category:Annotations]]
 
[[Category:Annotations]]

Revision as of 16:38, 26 August 2006

A signed variable is a set of 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.