Game Development Reference
In-Depth Information
Behind the scenes, the computer will automatically place a zero behind the decimal
point. Other than with the int type, dividing double variables results in only small
rounding errors:
d=d/3;
The variable d now contains the number 3.33333333.
Apart from int and double there are nine other types in C# for numerical variables.
Eight of the 11 numerical types can be used for whole numbers. The difference be-
tween the types is the range of values that can be represented by a type. Some types
allow for a bigger range of values to be represented, but the downside is that these
types require more memory. Especially when you are developing a console game
or a mobile game, memory is often limited. So when you have to store a number,
think beforehand which type is best suited. For example, if you want to store the
current level index, it makes no sense to use a double type, since level indices are
whole numbers. In that case a type that only holds positive whole numbers would
be more suited. There are types that can contain both negative and positive values,
other types only contain positive values.
type
space
smallest value
largest value
sbyte
1 byte
128
127
short
2 bytes
32 , 768
32,767
int
4 bytes
214 , 7483 , 648
2,147,483,647
long
8 bytes
9 , 223 , 372 , 036 , 854 , 775 , 808
9,223,372,036,854,775,807
byte
1 byte
0
255
ushort
2 bytes
0
65,535
uint
4 bytes
0
4,294,967,295
ulong
8 bytes
0
18,446,744,073,709,551,615
The long type is only needed if you are planning to use extremely large or small
values. The types byte and short are used if the range of the values is limited. In gen-
eral, the memory that this saves is only relevant if a lot of these variables (thousands,
or even millions) are required. The types short , int and long each have an 'unsigned'
version, of which the name begins with a 'u'. Unsigned types can only contain pos-
itive values. The type byte already is unsigned by itself, and it has a 'signed' version
called sbyte .
For non-whole numbers, there are three different types available. They do not
only differ in the maximum value that can be stored, but also in precision after the
decimal point.
type
space
significant digits
largest value
10 38
float
4 bytes
7
3 . 4
×
10 308
double
8 bytes
15
1 . 7
×
10 28
decimal
16 bytes
28
7 . 9
×
 
Search WWH ::




Custom Search