1. Float, double,
and long double
The data type mentioned earlier can only be used to handle integers. If we need to use decimals, we'll use
floating-point types (
floating-point). C provides three floating-point types:
float, double,
and long double. ,
The C standard requires that the
float type be at least
accurate to 6 digits after the decimal point , and that the integer portion of the representation range must reach at least
-37
-
10 +37 .
float is generally
a bit.
The C standard stipulates that the smallest representation range of the integer part of the
double type is the same as float, which is
-37 to
+37 , but it requires that the decimal part of the double type be at least as accurate as
the decimal digit. The double is usually 64 digits.
C also provides a
long double type, which is intended to provide a more precise type than double. However, the C standard merely stipulates that a long double should be at least as accurate as a double.
2.
declaring floating-point variables
the Declaration and initialization of a floating-point variable is the same as an integer variable. For example:
3.
Floating-point constants
Floating-point constants are written in several ways. The basic form is: First write the
integer part (can be signed), then write a
decimal part , and then write
e or
e, and finally write a
signed integer . For example:
where E or E is called a
step code flag , the signed integer following E or E is called the
Order code . The order code represents
the order of 10 of the second square . For example: The value of +1.2E+5 is
1.2 *
5. Assuming that a is the section preceding e, and N is the part behind E, then Aen equals
A *
n. In addition, a
plus sign can be omitted without writing. The
decimal Part is also not required, that is to say,
5e3 is also correct. Order signs and order codes can also not be written, such as:
13.5. After the decimal point, the part of the integer before the order mark can not be written (9.E5), and the integer before the decimal point may not be written (
. 96e-8), but it cannot be written at the same time. For example:
Note:
There can be no spaces in floating-point constants! For example:
3.21E-12/ * There are spaces, wrong! */
3.14 e5/ * have space, wrong! */
Floating-point constants are of type
double by default. Suppose Var_f is a float-type variable if the following statement is available:
Then 9.0 and 3.0 are both constants of type Double. Their product is also a double type. When the assignment is made, the product is converted to a float type and then assigned to Var_f.
of course, we can also specify the type of floating-point constants. If you add
f or
fto a floating-point constant, the compiler uses the
float type to handle the constant. For example:
1.5f,
2.1e6f. After adding
l or
l , the compiler uses a
long double to handle the constant. For example:
4.1l,
50.2e5l. It is best to use capital
l, because lowercase
l is easily confused with the number 1.
C99 Adds a format that represents a floating-point constant: using the hexadecimal prefix (0x or 0x,0 is the
number 0, not the letter O), substituting
p or
p for the preceding E or E, and the order code represents the
2 of the order of the second square . For example:
where
b is equal to one in the decimal,
1e equals 1/16 plus 14/256, P5 equals
2
5, which is 512. This floating-point literal is converted to decimal:
(one + 1/16 + 14/256) *
2
5
= 5692
Note: Not all compilers support this new format for C99!
4.
output floating-point number
format qualifier
%f command printf functions output float and double floating-point numbers in decimal form; the
%e command printf function outputs float and double floating-point numbers in exponential form;
The%a or
%a command printf function to C99 the new hexadecimal format output, but not all compilers support it. If you want to output a
long double floating-point number, use
%lf,
%le,
%la, or
%la. For example:
/* showfloat.c– uses two forms to represent the floating-point number * *
Long double var_ld = 6.51e-5;
printf ("%f is equal to%e\n", Var_f, Var_f);
printf ("%f is equal to%e\n", VAR_DF, VAR_DF);
printf ("%lf is equal to%le\n", Var_ld, VAR_LD);
The output is as follows:
5.000000 is equal to 5.000000e+00
314.000000 is equal to 3.140000e+02
0.000065 is equal to 6.510000e-05
Note: The above is the output that I get from Suse Linux 10 with the GCC 4.02 compile run. If you run this program using dev-c++ 4.9.9.2 compilation, you cannot output var_ld normally. Probably because Dev-c++ uses the compiler gcc, long double is 96-bit, and it uses the printf function in the function library to treat the long double as 64-bit.
5.
floating point Overflow (Overflow) and underflow (underflow)
Suppose that in your compiler, float can only reach 3.4e38, if you have the following statement:
float Toobig = 3.4E38 * 100.0f;
This must lead to overflow! Because Toobig cannot represent the product of 3.4E38 and 100.0f. The consequences of the overflow were not defined in the past, but now C stipulates that if an overflow occurs, a special value representing
Infinity is generated. Therefore, the value of the Toobig will eventually become a special value that represents
infinity . In turn, the printf function will output words like INF or infinity.
divide a floating-point number with a very small absolute value and cause the precision of the floating-point number to be reduced, called
underflow . For example, suppose that 3.1415e-10 is divided by 10 and then becomes 0.3141e-10, which is the underflow.
|