c语言初学者,这道题目是要求用fabs(x-y)<le-5验证两个数是否相等,我是这么编程的,但是错误的额

#include <stdio.h>
#include <math.h>
void main()
{
float x,y=0.3;
y=y*11;
x=3+0.3;
if(fabs(x-y)<1*e(-5)) printf("ok,x==y");
else printf("no,x!=y");
}

e(-5)这个不是math.h里的函数,如果你想算指数用exp(-5.0)这样的函数
另外这个位置写个比x和y都小很多的数就行,因为float类型的精度是和这个数的大小有关的
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-09
//c++ code
#include <limits>

template<typename T>
bool numeric_equal(T lhs, T rhs)
{
static const T epsilon = std::numeric_limit<T>::epsilon();

T delta = lhs - rhs;

return (-epsilon <= delta) && (delta <= epsilon);
}

//for test
int main(int, char*[])
{
float f1 = 11 * 0.3;
float f2 = 3 + 0.3f;

numberic_equal(f1, f2);
numberic_equal(1, 2);
numberic_equal(3.14, 3.141592653);

return 0;
}
第2个回答  2013-04-09
#include <stdio.h>
#include <math.h>
void main()
{
float x,y=0.3;
y=y*11;
x=3+0.3;
if(fabs(x-y)<0.00001) printf("ok,x==y");
else printf("no,x!=y");
}
第3个回答  2013-04-09
1e-5 直接这样写 不要写成 1*e(-5)
相似回答