用C ++逼近双变量

时间:2018-02-24 10:46:15

标签: c++ function double difference approximation

我试图运行这个程序,你在其中插入两个双值,然后你得到两个数字中哪一个更大,一个消息"数字相等"如果两个double的值相同,并且消息"数字几乎相等"如果两个数字之间的差异小于1 / 10.000.000。

我写的代码是:

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    inline void keep_window_open() { char ch; cin >> ch; }

    double square(double x)
    {
        return x * x;
    }

    int main() {
        double number;
        double number2;
        while (cin >> number, cin >> number2) {
            if (number == '|' || number2 == '|') break;
            else {
                cout << number << " " << number2 << endl;
                if (number > number2) cout << number << " is bigger than " << number2 << endl;
                if (number < number2) cout << number2 << " is bigger than " << number << endl;
                double difference = number - number2;
                if (difference > - 1/10000000 && difference < 0) cout << "the numbers are almost equal" << endl;
                if (difference < 1 / 10000000 && difference > 0) cout << "the numbers are almost equal" << endl;
                if (number == number2) cout << "the numbers are equal" << endl;
            }

        }
    }

当我运行此程序并插入两个差异小于1 / 10.000.000的数字时,程序应该返回&#34;数字几乎相等&#34;,但它没有。< / p>

如果我插入&#34; 1&#34;它返回的是什么(当我运行程序并插入两个数字时)和&#34; 1.0000000000001&#34;:

    "1 1.0000000000001

    1 1

    1 is bigger than 1"

我不明白为什么程序认为我加入的两个数字是1,并返回&#34; 1&#34;和&#34; 1&#34;作为价值观。

它也应该给予回报&#34;数字几乎相等&#34;自从我写道:

     if (difference > - 1/10000000 && difference < 0) cout << "the numbers are almost equal" << endl;
                        if (difference < 1 / 10000000 && difference > 0) cout << "the numbers are almost equal" << endl;

但它没有。

如何让程序返回&#34;数字几乎相等&#34;当我插入两个相差小于1 / 10.000.000的双值时(例如&#34; 1&#34;和&#34; 1.0000000000001&#34;)?

1 个答案:

答案 0 :(得分:1)

当您尝试打印时,

cout会将这些双精度值四舍五入。见here

对于您的代码,整数除法(1/10000000)将始终返回一个整数,因此您需要执行双值或十进制值的除法(1.0 / 10000000.0)

这是一个工作代码,对逻辑进行了一些细微的修改

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }

double square(double x)
{
    return x * x;
}

int main() {
    double number;
    double number2;
    while (cin >> number, cin >> number2) {
        if (number == '|' || number2 == '|') break;
        else {
            cout.precision(15);
            cout << number << " " << number2 << endl;
            double difference = number - number2;
            if (difference > - 1/10000000.0 && difference < 0) cout << "the numbers are almost equal" << endl;
            else if (difference < 1/10000000.0 && difference > 0) cout << "the numbers are almost equal" << endl;
            else if (number == number2) cout << "the numbers are equal" << endl;
            else if (number > number2) cout << number << " is bigger than " << number2 << endl;
            else if (number < number2) cout << number2 << " is bigger than " << number << endl;
        }

    }
}