Monday, January 26, 2009

Floating Point

We've skipped over floating point so far, and the treatment here will be hasty. C has single and double precision numbers (where the precision depends on the machine at hand). For example,

double sum;
float avg, y[10];
sum = 0.0;
for( i=0; i
sum =+ y[i];
avg = sum/n;

forms the sum and average of the array y.
All floating arithmetic is done in double precision. Mixed mode arithmetic is legal; if an arithmetic operator in an expression has both operands int or char, the arithmetic done is integer, but if one operand is int or char and the other is float or double, both operands are converted to double. Thus if i and j are int and x is float,

(x+i)/j converts i and j to float
x + i/j does i/j integer, then converts

Type conversion may be made by assignment; for instance,

int m, n;
float x, y;
m = x;
y = n;

converts x to integer (truncating toward zero), and n to floating point.
Floating constants are just like those in Fortran or PL/I, except that the exponent letter is `e' instead of `E'. Thus:

pi = 3.14159;
large = 1.23456789e10;

printf will format floating point numbers: ``%w.df'' in the format string will print the corresponding variable in a field w digits wide, with d decimal places. An e instead of an f will produce exponential notation.


Kia Dealers - Get multiple Kia price quotes from competing Kia Dealers


Used Autos - Autotropolis.com is your #1 stop for auto reviews and comparisons on new automobiles and trucks.
cigars - Cigar Experience is a great site to find information on all your favorite brands of top quality cigars.

1 comment:

  1. If you have a business and have no customer for business, then you can hire SEO Services Chandigarh India for certain sales leads, we will provide guaranteed leads for your business as per your requirement of your business.

    ReplyDelete