| I l @ ve RuBoard |
5.8 Types of Floats
The
float
type also comes in various flavors.
float
denotes normal precision (usually 4 bytes).
double
indicates double precision (usually 8 bytes), giving the programmer twice the range and precision of single-precision (
float
)
The quantifier
long double
denotes extended precision. On some systems this is the same as
double
; on others, it offers additional precision. All types of floating-point
On most machines, single-precision floating-point instructions execute faster (but less accurately) than double precision. Double precision gains accuracy at the expense of time and storage. In most cases float is adequate; however, if accuracy is a problem, switch to double (see Chapter 19). |
| I l @ ve RuBoard |
| I l @ ve RuBoard |
5.9 Constant and Reference Declarations
Sometimes you want to use a value that does not change, such as
const float PI = 3.1415926; // The classic circle constant
Constants must be
PI = 3.0; // Illegal
Integer constants can be used as a
const int TOTAL_MAX = 50; // Max. number of elements in total list float total_list[TOTAL_MAX]; // Total values for each category Another special variable type is the reference type. The following is a typical reference declaration: int count; // Number of items so far int& actual_count = count; // Another name for count The special character & is used to tell C++ that actual_count is a reference. The declaration causes the names count and actual_count to refer to the same variable. For example, the following two statements are equivalent: count = 5; // "Actual_count" changes too actual_count = 5; // "Count" changes too
In other words, a simple variable declaration declares a box to put data in. A reference variable slaps another name on the box, as
Figure 5-1. Reference variables
This form of the reference variable is not very useful. In fact, it is almost never used in actual programming. In Chapter 9, you'll see how another form of the reference variable can be very useful. |
| I l @ ve RuBoard |
| I l @ ve RuBoard |
5.10 Qualifiers
As you've seen, C++ allows you to specify a number of qualifiers for variable declarations. Qualifiers may be thought of as adjectives that describe the type that
Table 5-2. Qualifiers and simple types
5.10.1 SpecialThe volatile keyword is used for specialized programming such as I/O drivers and shared memory applications. It is an advanced modifier whose use is far beyond the scope of this book.
5.10.2 ConstantThe const keyword indicates a value that cannot be changed.
5.10.3 Storage ClassThe class of a variable is discussed in detail in Chapter 9. A brief description of the various classes follows:
5.10.4 SizeThe size qualifier allows you to select the most efficient size for the variable.
5.10.5 Sign
5.10.6 TypeThis specifies the type of the variable. Simple types include:
|
| I l @ ve RuBoard |