两个双变量的减法精度c ++

时间:2018-07-19 02:18:11

标签: c++ math double

我是C ++的新手。我必须减去两个双数,并且如果差小于1/100,则必须打印“数字几乎相等”,我的代码在小于64的数字上工作良好,但是如果我输入更大的数字而差为精确到0.01的句子不会打印。任何人都可以帮助我,为什么会这样?下面是我的代码:

#include <iostream>

using namespace std;

int main()
{
    double num1,num2;
    double subs;
    while(cin >> num1)
    {
        cin >> num2 ;

        if(num1 < num2)
        {
            cout << "\nthe smaller value is : "<< num1;
            cout << "\nthe larger value is : " << num2<<"\n\n";
            subs = num2 - num1;
            if(subs <= 0.01  )
                cout << "\nthe numbers are almost equal\n\n";
        }
        else if(num2 < num1)
        {
            cout << "\nthe smaller value is : "<< num2;
            cout << "\nthe larger value is : " << num1 <<"\n\n";
            subs = num1 - num2;
            if(subs <= 0.01 )
                cout << "\nthe numbers are almost equal\n\n";
        }
        else
        {
            cout << "\nLos numeros son iguales\n\n";
        }
    }
}

0 个答案:

没有答案