判断浮点数是否相等

我们一般认为两个浮点数相等,当且当他们之间的误差不超过1e-8。1e-8是指1*10的负8次方,为很小数值0.00000001。

我们使用fabs函数。其中abs是对整数取绝对值,而fabs是对浮点数取绝对值。

函数参数:

int abs(int x)
double fabs(double x)

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    double a;
    double b;
    cin >> a >> b ;
    if (fabs(a - b) < 1e-8)
        cout << "yes" << endl;
    else
        cout << "no" << endl;
    return 0;
}

 

posted @ 2017-12-25 20:33  兔子爱吃年糕  阅读(392)  评论(0编辑  收藏  举报