判断浮点数是否相等

版权声明:转载请附上地址 https://blog.csdn.net/weixin_44574520/article/details/87460068

判断浮点数是否相等

Codevs天梯

题目描述 Description

给出两个浮点数,请你判断这两个浮点数是否相等

输入描述 Input Description

输入仅一行,包含两个浮点数

输出描述 Output Description

输出仅一行,如果相等则输出yes,否则输出no。

样例输入 Sample Input

2.980000001 2.9800000000001

样例输出 Sample Output

yes

扫描二维码关注公众号,回复: 5379024 查看本文章

数据范围及提示 Data Size & Hint

我们一般认为两个浮点数相等,当且当他们之间的误差不超过1e-8。

Code:

#include <bits/stdc++.h>
using namespace std;
#define maxn 0.00000001

int main(){
	double a,b;
	scanf("%lf%lf",&a,&b);
	double pd=a-b;
	if(pd>=-maxn&&pd<=maxn) printf("yes");
	else printf("no");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44574520/article/details/87460068