Real data is also known as floating-point numbers or real numbers. In the C language, the real number is only used in decimal. It has two types: decimal decimal form and exponential form.
Representation of a real number
1) Decimal number form
Consists of a digital 0~ 9 and a decimal point. For example: 0.0, 25.0, 5.789, 0.13, 5.0, 300., 267.8230, etc. are all valid real numbers.
Note that there must be a decimal.
2) Exponential form
Consists of a decimal number, an additional code symbol "E" or "E", and a Step code (integer only, can be signed). The general form is:
A E N (A is a decimal number and n is a decimal integer)
Its value is a*10n. Such as:
2.1E5 (equals 2.1*105)
3.7E-2 (equals 3.7*10-2)
0.5E7 (equals 0.5*107)
-2.8E-2 (equals -2.8*10-2)
The following are not valid real numbers:
345 (no decimal point)
E7 (no digits before the order symbol E)
-5 (no order symbol)
53.-e3 (negative position is not correct)
2.7E (no order code)
"Example 3-5" outputs a real number.
- #include <stdio.h>
- int main(void){
- printf("%f\ n ",356.);
- printf("%f\ n ",356);
- printf("%f\ n ",356.0);
- return 0;
- }
3) Real number in-memory storage form
Real numbers typically account for 4 bytes (32 bits) of memory space. stored as an exponential. The real number 3.14159 is stored in memory in the following form:
Description
- The greater the number of bits in the small part, the more effective the number, the higher the accuracy.
- The more digits the exponent part occupies, the greater the range of values that can be expressed.
Real variables
The real variables are divided into: single-precision (float), double-precision (dual-type) and long double-precision (long double) class three.
The single-precision type in the VC6.0 occupies 4 bytes (32-bit) of memory space, with a value range of 3.4e-38~3.4e+38 and only seven digits of valid digits. The double is a 8-byte (64-bit) memory space with a value range of 1.7e-308~1.7e+308 and a 16-bit valid number.
Type descriptor |
Number of bits (bytes) |
valid numbers |
the range of the number |
Float |
32 (4) |
6~7 |
10-37~1038 |
Double |
64 (8) |
15~16 |
10-307~10308 |
Long double |
128 (16) |
18~19 |
10-4931~104932 |
A real variable defines the same formatting and writing rules as an integral type. For example:
- float x, y; //x, Y is a single-precision real amount
- Double A, b, C; //A,b,c is a double-precision real quantity
Rounding error of real numbers
Since real numbers are made up of a finite number of storage units, the valid numbers available are always limited. The following example.
The rounding error of the "example 3-6" real number.
- #include <stdio.h>
- int main(void){
- float a, b;
- A=123456.789e5;
- b=a+20;
- printf("a=%f\ n", a);
- printf("b=%f\ n", b);
- return 0;
- }
Note: The result of 1.0/3*3 is not equal to 1.
"Example 3-7"
Copy Plain Text New Window
- #include <stdio.h>
- int main(void){
- float a;
- Double b;
- A=33333.33333;
- b=33333.33333333333333;
- printf("a=%f\b=%f\ n", a, b);
- return 0;
- }
As you can see from this example:
- Because A is a single-precision floating-point type, the number of valid digits is only seven bits. And the integer is five bits, so the decimal two digits after the number is invalid.
- B is a double type with a valid bit of 16 bits. However, the VC6.0 specifies a maximum of six digits after the decimal, and the remainder is rounded.
C-language real data (floating-point number)