【发布时间】:2010-08-31 09:36:25
【问题描述】:
可能重复:
strange output in comparision of float with float literal
#include<stdio.h>
int main()
{
float me = 1.7;
if(me==1.7)
printf("C");
else
printf("C++");
}
Output: C++
现在说这种行为的原因是许多浮点数不能用二进制的绝对精度表示。
我的问题是 - 如果计算机以二进制方式思考和操作。比较时,me 表示的任何不确定性对于 1.7 都是相同的。所以两者应该相等。
另外,类型转换如何解决这个问题? (float)1.7
【问题讨论】:
标签: c++ c floating-point