1.float精度为小数点后6位,6位以后可能正确,可能错误
#include<stdio.h>
void main(){
float a = 3.789456123;
float b = 3.789456223;
float c = 3.789457123;
if (a==b)
{
printf("两数相等");
}
if (a==c)
{
printf("a,c相等");
}
getchar();
// float精度为小数点后6位
}
上述代码输出为:两数相等
2.double精度为小数点后15位
#include<stdio.h>
void main(){
double a = 3.123451234512345123;
double b = 3.123451234512345223;
double c = 3.123451234512346223;
if (a==b)
{
printf("两数相等");
}
if (a==c)
{
printf("a,c相等");
}
getchar();
}
3.浮点型打印
#include<stdio.h>
#include<stdlib.h>
void main(){
float f1; // float占4字节
double db;// double占8字节
long double ldb;// long double在某些平台占16字节
printf("%d,%d,%d", sizeof(f1), sizeof(db), sizeof(ldb));
scanf_s("%f", &f1);
scanf_s("%lf", &db);
scanf_s("%Lf", &ldb);
// printf打印float使用%f,打印double使用%lf,打印long double使用%Lf
printf("\n%f,%lf,%Lf", f1, db, ldb);
// printf打印小数默认打印6位
printf("\n%.7f,%.20lf,%.40lf", f1, db, ldb);
system("pause");
}