【发布时间】:2012-08-01 12:11:28
【问题描述】:
可能重复:
strange output in comparision of float with float literal
我无法理解这段代码。如何比较两个相同的数字?
#include<stdio.h>
int main()
{
float a=0.8;
if(0.8>a) //how can we compare same numbers?
printf("c");
else
printf("c++");
return 0;
}
如何解决这个问题?
【问题讨论】:
-
a中的 0.8 比if语句中的 0.8 精度低,因此提升中的错误会导致意外结果。 -
我在某处读到浮点数的默认数据类型是双精度
-
float只有32位,float a中的0.8用32位表示,而if语句中的0.8则是double64位. -
你确定这编译正确吗?因为 if 条件中 > 运算符的左侧是常数?即使它编译a
-
@rain 它可以正确编译两种方式
标签: c if-statement floating-point operators