“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute every day to remember the basics of C language.
“Series of 100 Essential Knowledge Points for C Language Beginners“
9. Differences Between Float and Double
1. Overview of Floating Point Types
The floating point type is used in C language to represent real numbers, mainly divided into two types:
- 1. float – Single precision floating point (32 bits)
- 2. double – Double precision floating point (64 bits)
2. Memory Usage and Precision Comparison
| Type | Storage Size | Significant Digits | Value Range (Absolute Value) |
| float | 4 bytes | 6-7 digits | 1.2E-38 to 3.4E+38 |
| double | 8 bytes | 15-16 digits | 2.3E-308 to 1.7E+308 |
3. Declaration and Initialization
1. float Variable
float price = 9.99f; // Note the f suffix
float temperature = -12.5f;
2. double Variable
double pi = 3.141592653589793;
double bigNumber = 1.23456e+20; // Scientific notation
4. Input and Output Methods
1. printf Formatted Output
float f = 123.456f;
double d = 123.456789;
printf("float: %f\n", f); // Default 6 decimal places
printf("float: %.2f\n", f); // Keep 2 decimal places
printf("Scientific notation: %e\n", d); // 1.234568e+02
2. scanf Input
float input_f;
double input_d;
scanf("%f", &input_f); // float uses %f
scanf("%lf", &input_d); // double uses %lf
5. Precision Difference Example
#include <stdio.h>
int main() {
float f = 1.23456789f;
double d = 1.23456789;
printf("float storage: %.10f\n", f); // 1.2345678806
printf("double storage: %.10f\n", d); // 1.2345678900
return 0;
}
Output result:
float storage: 1.2345678806
double storage: 1.2345678900
6. Recommended Usage Scenarios
Recommended Scenarios for Using float
- 1. Memory-constrained embedded systems
- 2. Graphics processing (GPUs are better at handling float)
- 3. Simple calculations that do not require high precision
Recommended Scenarios for Using double
- 1. Scientific computing and engineering applications
- 2. Financial calculations (require precise decimals)
- 3. Numerical computations that require multiple iterations
7. Common Errors
- 1. Omitting the f suffix for float:
float x = 3.14; // Compiler will warn, automatically converts to double then to float - 2. Confusing input and output formats:
double d; scanf("%f", &d); // Error! Use %lf - 3. Directly comparing floating point numbers:
if (0.1 + 0.2 == 0.3) // May return falseCorrect approach:
#include <math.h> if (fabs(0.1 + 0.2 - 0.3) < 1e-6) // Allow small error printf("Equal");
8. Handling Special Values
The floating point type supports special value representations:
double inf = 1.0 / 0.0; // Infinity
double nan = 0.0 / 0.0; // Not a Number (NaN)
#include <math.h>
if (isinf(inf)) printf("Infinity");
if (isnan(nan)) printf("Not a Number");
9. Type Conversion Rules
- 1. Automatic conversion during mixed operations:
float f = 1.2f; double d = f; // Safe conversion - 2. Explicit type conversion:
double d = 3.14; float f = (float)d; - 3. Conversion with integers:
int i = 5; float f = i; // f=5.0 int j = 3.14; // j=3 (truncates decimal)
———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author, some content and images are sourced from the internet and AI, please feel free to consume, the views are for learning reference only~~]

“If you like C, please like”
Click the bottom right to see
“