Floating Point in C
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<n; 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 convertsType 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;a 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.
0 comments:
Post a Comment