Professional Documents
Culture Documents
short Counter;
• The Ants variable will occupy more
bits than the standard int (e.g. 64
bits, so it can be used to store
numbers from the range of [-
9223372036854775808 ..
9223372036854775807] – can you
read such huge numbers? →
Note – we can again omit the
word int:
long long Ants;
• If we come to the conclusion that
a variable will never be a
negative value, we can use
the unsigned modifier →
unsigned Positive;
• We can also mix some of the modifiers
together – take a look →
Don’t forget that we’re not allowed to omit the word char. Most of
the compilers currently in use assume that the chars are stored using
8 bits (1 byte). That may be enough to store a small value such as the
number of months or even the day of the month.
• If we treat the char variable as a signed integer number, its range would be
[-128 .. 127]. If we don’t need any signed value (as in the example here → ),
its range shifts to [0 .. 255]. This may be sufficient for many applications
and may also result in significant savings in memory usage.
•
But we need to add an important remark. So far we’ve used integer literals,
assuming that all of them are of type int. This is generally the case, but
there are some cases when the compiler recognizes literals of type long.
This will happen if:
• a literal value goes beyond the acceptable range of type int;
• a letter L or l is appended to the literal, such as 0L or 1981l – both of these
literals are of type long.
•
Another float type
• The short modifier cannot be used alongside
the float, but we may use the long modifier here. It’s
assumed that type long float is a synonym for
another type named double. The variables of
type double may differ from the variables of
type float, not only in range, but also in accuracy.
• What does this mean? The data stored in a floating-
point variable has finite precision – in other words,
only a certain number of digits are precisely
stored in the variable.
For example, we expect that the value:
•
1111111111111111111.111111111111111111111
• will be stored by a specific type of computer as:
•
1111111131851653120.000000
•
We say that the variable saves (only) 8 precise
digits. This is within the expected accuracy of
32-bit long floats. Using a double (which is
usually 64 bits long) guarantees that the variable
will save a more significant number of digits –
about 15-17. This is where the
name double comes from – its accuracy
is doubled compared to float.
• We told you some time ago that computer
addition is not always commutative. Do you
know why? Imagine that you have to add a large
number of floating-point values – some of them
are very large, some very small (close to zero). If
a very small float value is added to another that’s
very large, the result can be quite surprising.
• Let’s go back to the previous example – we’ll
assume that our computer only saves 8 precise
digits of any float. If we add these two floats,
we’ll probably get:
•
11111110656.000000
•
as the result. The lower value simply vanished
without a trace.
• We can’t avoid these effects when we add/subtract the numbers of
type float (and of double as well, because they’re also affected by this
issue). The phenomenon described here is what we call a numerical
anomaly.