Flylib.com

Books Software

 
 
 

5.8 Types of Floats

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 ) variables .

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 numbers are always signed.

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 figs/u03c0.gif . The keyword const indicates a variable that never changes. To declare a value for pi , we use the statement:

const float PI = 3.1415926;    // The classic circle constant

By convention variable names use lowercase only while constants use uppercase only. However, there is nothing in the language that requires this, and some programming projects use a different convention.

In fact, there are major programming environments such as the X Windows System that use their own naming conventions with mixed-case constants and variables ( VisibilityChangeMask , XtConvertAndStore , etc.). The Microsoft Windows API also uses its own unique naming convention.

Constants must be initialized at declaration time and can never be changed. For example, if we tried to reset the value of PI to 3.0 we would generate an error message:

PI = 3.0;      // Illegal

Integer constants can be used as a size parameter when declaring an array:

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 illustrated in Figure 5-1.

Figure 5-1. Reference variables
figs/c++2_0501.gif

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 follows . Table 5-2 summarizes the various qualifiers; they are explained in detail in the following sections.

Table 5-2. Qualifiers and simple types

Special

Constant

Storage class

Size

Sign

Type

volatile

const

register

long

signed

int

<blank>

<blank>

static

short

unsigned

float

   

extern

double

<blank>

char

   

auto

<blank>

 

wchar_t

   

<blank>

   

<blank>

5.10.1 Special

The 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.

volatile

Indicates a special variable whose value may change at any time

<blank>

Normal variable

5.10.2 Constant

The const keyword indicates a value that cannot be changed.

const

Indicates that this is a declaration of constant data

<blank>

Normal variable

5.10.3 Storage Class

The class of a variable is discussed in detail in Chapter 9. A brief description of the various classes follows:

register

This indicates a frequently used variable that should be kept in a machine register. See Chapter 17.

static

The meaning of this word depends on the context. This keyword is described in Chapter 9 and Chapter 23.

extern

The variable is defined in another file. See Chapter 23 for more information.

auto

A variable allocated from the stack. This keyword is hardly ever used.

<blank>

Indicates that the default storage class is selected. For variables declared outside a function, this makes the variable global. Variables inside a function are declared auto .

5.10.4 Size

The size qualifier allows you to select the most efficient size for the variable.

long

Indicates a larger than normal number.

short

Indicates a smaller than normal integer.

double

Indicates a double-size floating-point number.

<blank>

Indicates a normal-size number.

5.10.5 Sign

Numbers can be signed or unsigned . This qualifier applies only to char and int types. Floating-point numbers are always signed. The default is signed for int and undefined for characters .

5.10.6 Type

This specifies the type of the variable. Simple types include:

int

Integer.

float

Floating-point number.

char

Single character, but can also be used for a very short integer.

wchar_t

Single wide character. Can also be used for a short integer, but most people don't because the other integer types are more appropriate to use.

I l @ ve RuBoard