If you're a beginning programmer and want to get more deeply into programming with variables, you've come to the right place. This article, the second of three parts, is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).

First Steps in (C) Programming, continued - Floating-Point Variables (Page 4 of 8 )

There are three different types of floating-point variables, as shown in Table 2-5.

Table 2-5. Floating-Point Variable Types

Number

Keyword

of Bytes

Range of Values

float

4

±3.4E38 (7 decimal digits precision)

double

8

±1.7E308 (15 decimal digits precision)

long double

10

±1.2E4932 (19 decimal digits precision)

These are typical values for the number of bytes occupied and the ranges of values that are supported. Like the integer types, the memory occupied and the range of values are dependent on the machine and the compiler. The typelong doubleis sometimes exactly the same as typedoublewith some compilers. Note that the number of decimal digits of precision is only an approximation because floating-point values will be stored internally in binary form.

A floating-point variable is declared in a similar way to an integer variable. You just use the keyword for the floating-point type that you want to use:

float Radius; double Biggest;

If you need to store numbers with up to seven digits of accuracy (a range of 10^{-38 }to 10^{+38}), then you should use variables of typefloat. Values of typefloatare known as single precisionfloating-point numbers. This type will occupy 4 bytes in memory, as you can see from the table. Using variables of typedoublewill allow you to store double precision floating-point values. Each variable of typedouble will occupy 8 bytes in memory and give you 15-digit precision with a range of 10^{-308 }to 10^{+308}. Variables of typedouble suffice for the majority of requirements, but some specialized applications require even more accuracy and range. Thelong doubletype provides the exceptional range and precision shown in the table.

To write a constant of typefloat, you append anfto the number to distinguish it from typedouble. You could have initialized the last two variables with these statements:

float Radius = 2.5f; double Biggest = 123E30;

The variableRadiushas the initial value 2.5, and the variableBiggestis initialized to the number that corresponds to 123 followed by 30 zeroes. Any number that you write containing a decimal point is of typedoubleunless you append theFto make it typefloat. When you specify an exponent value withEore, the constant need not contain a decimal point. For instance,1E3f is of typefloatand3E8is of typedouble.

To specify along doubleconstant, you need to append an upper- or lowercase letterLto the number, for example:

long double huge = 1234567.89123L;

Division Using Floating-Point Values

As you’ve seen, division operations with integer operands always produce an integer result. Unless the left operand of a division is an exact multiple of the right operand, the result will be inherently inaccurate. Of course, the way integer division works is an advantage if you’re distributing cookies to children, but it isn’t particularly useful when you want to cut a 10-foot plank into four equal pieces. This is a job for floating-point values.

Division operations with floating-point values will give you an exact result—at least, a result that is as exact as it can be with a fixed number of digits of precision. The next example illustrates how division operations work with variables of typefloat.

Here’s a simple example that just divides one floating-point value by another and displays the result:

/* Program 2.7 Division with float values */ #include <stdio.h> void main() { float plank_length = 10.0f; /* In feet */ float piece_count = 4.0f; /* Number of equal pieces */ float piece_length = 0.0f; /* Length of a piece in feet */ piece_length = plank_length/piece_ccount; printf("A plank %f feet long can be cut into %f pieces %f feet long.", plank_length, piece_count, piece_length); }

This program should produce the following output:

----------------------------------------------------------------------A plank 10.000000 feet long can be cut into 4.000000 pieces 2.500000 feet long. --------------------------------------------

HOW IT WORKS

You shouldn’t have any trouble understanding how you chop the plank into equal pieces. Note that you use a new format specifier for values of typefloatin theprintf()statement:

printf("A plank %f feet long can be cut into %f pieces %f feet long.", plank_length, piece_count, piece_length);

You use the format specifier%fto display floating-point values. In general, the format specifier that you use must correspond to the type of value that you’re outputting. If you output a value of typefloatwith the specifier%dthat’s intended for use with integer values, you’ll get rubbish. This is because thefloatvalue will be interpreted as an integer, which it isn’t. Similarly, if you use%fwith a value of an integer type, then you’ll also get rubbish as output.