我试图做一些总和,我遇到了这个问题,我想知道是否有办法修复它?
这是我的代码
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
float a=(3.0/2.0)+2; // a = 3.5
float b=(5.5/2.0)+2; // b = 4.8
float c=((a+b)/2.0)+2; // c = 6.1
float d=((3.5+4.8)/2)+2; // d = 6.2
cout<<"a = "<<fixed << setprecision(1)<<a<<endl;
cout<<"b = "<<fixed << setprecision(1)<<b<<endl;
cout<<"c = "<<fixed << setprecision(1)<<c<<endl;
cout<<"d = "<<fixed << setprecision(1)<<d<<endl;
system("pause");
return 0;
}
结果将是:
a = 3.5
b = 4.8
c = 6.1
d = 6.2
我怎样才能使c = 6.2?