为什么在将返回值赋给 int 时,它自己减一?

问题描述

问题是,当乘以像 9.58 这样的 double 类型的数字并将其存储在一个整数类型的数字中时,我得到的是 957 而不是 958。

当我单独尝试将 9.58*100 分配给程序中的整数时,它起作用了。 怎么来的?

#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;
}

我已经单独检查过,没有出现这个问题。

你能帮忙吗

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)