Monday, July 9, 2012

Float Data Types & Variables in C Programming Language

Integer and character data types are incapable of storing numbers with fractional parts. Depending on the precision required, C provides two variable types for computation with "floating point" numbers, i.e. numbers with a decimal (internally a binary) point. Such numbers are called floats because the binary point can only be represented in the binary- digits expansion of the number, in which it is made to "float" to the appropriate "position" for optimal precision. (You can immediately see the difficulty of imagining a binary "point" within any particular bit of a floating point word, which can contain only a 0 or a 1!) Typically, some of the leftmost bits of a floating point number store its characteristic (the positive or negative power of two to which it is raised), and the remaining its mantissa, the digits which comprise the number. In base 10, for example, if 2.3 is written as 0.0023 *103, the mantissa is 0.0023, and the characteristic (exponent) is 3.

Single precision floating point variables are declared by the float specification, for example:

float bank_balance = 1.234567E8;

/*En means 10 raised to the power n */

The scientific notation En, where the lowercase form en is also acceptable, is optional; one may alternatively write:

float bank_balance = 123456700.0;

Floats are stored in four bytes and are accurate to about seven significant digits; on PCs their range extends over the interval [E-38, E37].

It must never be lost sight of that the floating point numbers held in a computer's memory are at best approximations to real numbers. There are two reasons for this shortcoming. First, the finite extent of the word size of any computer forces a truncation or round-off of the value to be stored; whether a storage location is two bytes wide, or four, or even eight, the value stored therein can be precise only to so many binary digits.

Second, it is inherently impossible to represent with unlimited accuracy some fractional values as a finite series of digits preceded by a binary or decimal point. For example

1/7 = 0. 142857142857142857 ..... ad infinitum

As long as a finite number of digits is written after the decimal point, you will be unable to accurately represent the fraction 1/7. But rational fractions that expand into an infinite series of decimals aren't the only types of floating point numbers that are impossible to store accurately in a computer. Irrational numbers such as the square root of 2 have a periodic (non- repeating) expansions__there's no way that you can predict the next digit, as you could in the, expansion of 1/7 above, at any point in the series. Therefore it's inherently impossible to store such numbers infinitely accurately inside the machine. PI, the ratio of the circumference of any circle to its diameter, is another number that you cannot represent as a finite sequence of digits. It's not merely irrational, it's a transcendental number. (It cannot be the root of any algebraic equation with rational coefficients.) So an element of imprecision may be introduced by the very nature of the numbers to be stored.

Third, in any computation with floating point numbers, errors of round-off or truncation are necessarily introduced. For suppose you multiply two n-bit numbers; the result will in general be a 2n-bit number. If this number (2n) of bits is larger than the number of bits in the location which will hold the result, you will be forcing a large object into a small hole! Ergo, there'll be a problem! Some of it will just have to be chopped off. Therefore it is wisest to regard with a pinch of salt any number emitted by a computer as the result of a computation, that has a long string of digit-, after the decimal point. It may not be quite as accurate as it seems.

The %e, %f and %g format conversion characters are used in the scant () and printf () functions to read and print floating point numbers. %e (or %E) is used with floating point numbers in exponent format, while %g (or %G) may be used with floats in either format. %g (or %G) in the printf () outputs a float variable either as a string of decimal numbers including a decimal point, or in exponent notation, whichever is shorter. An uppercase E or G prints an uppercase E in the output. We shall be discussing more about format control in a later Unit.) Program 2.15 below finds the average of five numbers input from the keyboard, and print, it:

/* Program 1 */
#include <stdio.h>  
main()  
{   
 float val_1, val_2, val_3, val_4, val_5, total 0.0, avg;   
 pritf ("\nEnter first number...");   
 scanf ("%f", &val_1);   
 printf ("\nEnter third number...");  
 printf ("\nEnter second number...");   
 scanf ("%f', &,val_2);   
 scanf ("%f', &val_3);   
 printf ("\nEnter fourth number...");  
 scanf ("%f", &val_4);   
 avg = total / 5;  
 printf ("\nEnter fifth number...");   
 scanf ("%f", &val_5);  
 total = val_1 + val_2 + val_3 + val_4 + val_5;  
 printf ("\nThe average of the numbers you entered is: %f\n", avg);   
} 

Here's a sample conversation with the program:

Enter first number .. 32.4
Enter second number .. 56.7
Enter third number .. 78.3
Enter fourth number .. 67.8
Enter fifth number... -93.9
The average of the numbers you entered is: 28.260000

0 comments:

Post a Comment