why when assigning the returned value to int, it by itself reduces by one? [duplicate]

  approximation, c++, precision

The problem is that when multiplying a double type number like 9.58 and storing it in a integer type number, I get 957 instead of 958.

When I separately try to assign 9.58*100 to an integer in a program, it works.
How come?

#include <bits/stdc++.h>
using namespace std;
double roundit(double x)
{
    double e = x * 100;
    e += 0.5;
    cout << "func" << e << endl;
    int d = e;
    cout << "func" << d << endl;
    e = (float(d)) / 100;
    cout << "func" << e << endl;
    return e;
}
int main()
{
    int t;
    cin >> t;

    while (t--) {
        double a, b, c, d;

        cin >> a >> b >> c >> d;
        /*
    say give 
    3
    1.0 1.0 1.0 10.45
    1.0 1.0 1.0 10.44
    1.0 1.0 0.9 10.44
    as input
    */

        double ans = a * b * c * d;

        ans = 100 / ans;

        cout << ans << endl;
        ans = roundit(ans);
        cout << "main" << ans << endl; //this part right here
        int x = (ans * 100.0); //say instead of giving 9.58*100 =958, it gives 957
        cout << "main" << x << endl;
        cout << (x >= 958 ? "NO" : "YES") << endl;
    }
    return 0;
}

i’ve checked this separately and this problem doesn’t appear.

Can you help

Source: Windows Questions C++

LEAVE A COMMENT