如何解决为什么在将返回值赋给 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 举报,一经查实,本站将立刻删除。