The article directories
- Floating point types
-
- 1. Basic introduction
- 2. Case Demonstration:
- 3. Classification of floating point types
- 4. 4. Smart refrigerator
- 5. Floating point usage details
Floating point types
1. Basic introduction
C floating-point types can represent a decimal number, such as 123.4, 7.8, 0.12, and so on
2. Case Demonstration:
3. Classification of floating point types
4. 4. Smart refrigerator
- A simple explanation of how floating-point numbers can be stored in a machine,
Floating point = sign bit + exponent bit + mantissa bit
, Floating point numbers are myopic values - Mantissa may be lost, resulting in loss of accuracy.
5. Floating point usage details
- Floating-point constants default to
double
Type, the statementfloat
Type constant, must be followed by ‘f
‘or’F
‘. - Floating-point constants can be represented in two ways
- In decimal notation:
5.12
.512.0 f
,512.
(Must have decimal point) - Scientific notation:
5.12 e2
、5.12 e-2
- In general, it should be used
double
Type, because it is more thanfloat
Type is more accurate. printf("d1=%f ", d1);
// The decimal point is kept by default when output6
位- code
- In the output, if
%f
The decimal point is kept by default6
Bit if you want the given number to exceedsix
, you can write:d1=%7f
.7
It stands behind the decimal pointseven
#include<stdio.h>
void main(a){
// Float constants are double by default. Float constants must be followed by 'f 'or 'f'.
float d1 =1.1; // Truncate from "double "to" float",1.1 is double
float d2 =1.1 f;/ / 1.1 f is a float
double d3= 1.3; // ok
double d4 = 5.12;
double d5 =512.;/ / equivalent to 0.512
double d6 = 5.12 e2; / / equivalent 5.12 * 10 ^ (2) = 512
double d7 = 5.12 e-2; // Equivalent 5.12*(10^-2)=5.12/100= 0.0512
printf("d1=%f d2=%f d3=%f d4=%f d5=%f d6=%f d7=%f",d1,d2,d3,d4,d5,d6,d7);
getchar();
}
Copy the code