题目大意
比较a^b与c^d的大小,输出“>””<”“=”.
解题思路
取对数比较b×log(a)和d×log(c),注意浮点数精度误差eps。
浮点数精度误差很好容易理解,就像十进制不能精确表示1/3(0.33333)一样,计算机二进制也不能精确表示和存储1/10,0.999999998,结论是
浮点数是有精度的,不能直接比较相等,只能控制在一定的误差范围内相等。
比如这样的一段代码
执行结果是死循环
原因就是浮点数有精度误差,不能直接比较相等。
这道题也是这样的一个问题,判断等不等时要控制误差范围eps,只要落在这个范围内就可以认为相等。
参考代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
#include <cstring>
#include<cmath>
#define eps 1e-8
using namespace std;
typedef long long ll;
const int maxn = 50;
int main()
{
// freopen("input.txt","r",stdin);
double a,b,c,d;
while(cin>>a>>b>>c>>d){
double temp=b*log(a)-d*log(c);
if(fabs(temp)<eps) puts("="); //与0等不等
else if(temp>eps) puts(">");
else puts("<");
}
return 0;
}