jeudi 25 mars 2021

"Less Than" Operator Not Behaving as Expected

I am doing a very simple exercise that compares the difference between two numbers and outputs "The numbers are almost equal" if the difference is less than 0.01

However, The if-statement triggers even when the difference is EQUAL to 0.01 What am I doing wrong?

int main()
{
    double val1;
    double val2;
    while (cin >> val1 >> val2)
        if (val1 < val2) {
            cout << "The smaller value is: " << val1 << "\n";
            cout << "The larger value is: " << val2 << "\n";
            if ((val2 - val1) < (1.0 / 100))
                cout << "The numbers are almost equal" << "\n";
        }
        else if (val1 == val2)
            cout << "The numbers are equal\n";
        else {
            cout << "The smaller value is: " << val2 << "\n";
            cout << "The larger value is: " << val1 << "\n";
            if ((val1 - val2) < (1.0 / 100))
                cout << "The numbers are almost equal" << "\n";
        }
}

Aucun commentaire:

Enregistrer un commentaire