意外舍入双变量 C++
unintended round up double variable c++
题主总是四舍五入0.x而且答案是错误的
我正在使用 code:blocks mingw
请查看交易 2 上的这张图片,它是 99855.7-145.022,但答案已经四舍五入
#include <iostream>
using namespace std;
int main()
{
double x,amort,principalminus,interest,amortbal,theo,adv,advtemp,paid,mri;
double principal;
char con;
adv=0;
cout<<"CMP Calculator "<<endl;
cout<<"Base Principal:";
cin>>principal;
cout<<"MRI:";
cin>>mri;
cout<<"Monthly Amortization:";
cin>>amort;
do
{
x++;
interest = (principal*0.06)/12;
cout<<"Number of transaction:"<<x<<endl;
cout<<"Amortization amount paid:";
cin>>paid;
if(paid>=685.3)
{
principalminus = amort - (interest+mri);
advtemp = paid-amort;
}
principal=principal-principalminus;
adv=adv+advtemp;
cout<<"Interest:"<<interest<<endl;
cout<<"Principal Paid:"<<principalminus<<endl;
cout<<"Advance:"<<adv<<endl;
cout<<"Total Principal:"<<principal<<endl;
cout<<"Do you want to continue?:[Y/N]";
cin>>con;
cout<<endl<<endl;
}
while(con=='Y'||con=='y');
return 0;
}
it's 99855.7-145.022 but the answer is already rounded up
你只需要打印更多的数字,例如:
cout << std::setprecision(15);
在其他打印之前的某个地方,为此你需要
#include <iomanip>
你将拥有
pi@raspberrypi:/tmp $ ./a.out
CMP Calculator
Base Principal:100000
MRI:41
Monthly Amortization:685.3
Number of transaction:1
Amortization amount paid:686
Interest:500
Principal Paid:144.3
Advance:0.700000000000045
Total Principal:99855.7
Do you want to continue?:[Y/N]y
Number of transaction:2
Amortization amount paid:686
Interest:499.2785
Principal Paid:145.0215
Advance:1.40000000000009
Total Principal:99710.6785
Do you want to continue?:[Y/N]n
Total Principal 的打印值是正确的,无论如何根据定义理解任何浮点数的精度都有限制,您可以在打印值中看到前进.
如果您的编译器支持 long double
,您可以使用它们,同时要求打印 20 位数字,结果将是:
bruno@bruno-XPS-8300:/tmp$ ./a.out
CMP Calculator
Base Principal:100000
MRI:41
Monthly Amortization:685.3
Number of transaction:1
Amortization amount paid:686
Interest:499.99999999999998149
Principal Paid:144.30000000000001853
Advance:0.7000000000000000111
Total Principal:99855.699999999999982
Do you want to continue?:[Y/N]y
Number of transaction:2
Amortization amount paid:686
Interest:499.27849999999998143
Principal Paid:145.02150000000001856
Advance:1.4000000000000000222
Total Principal:99710.678499999999964
Do you want to continue?:[Y/N]n
还请注意,您错过了用 0 初始化 x,它可以是 int
而不是 double
题主总是四舍五入0.x而且答案是错误的
我正在使用 code:blocks mingw
请查看交易 2 上的这张图片,它是 99855.7-145.022,但答案已经四舍五入
#include <iostream>
using namespace std;
int main()
{
double x,amort,principalminus,interest,amortbal,theo,adv,advtemp,paid,mri;
double principal;
char con;
adv=0;
cout<<"CMP Calculator "<<endl;
cout<<"Base Principal:";
cin>>principal;
cout<<"MRI:";
cin>>mri;
cout<<"Monthly Amortization:";
cin>>amort;
do
{
x++;
interest = (principal*0.06)/12;
cout<<"Number of transaction:"<<x<<endl;
cout<<"Amortization amount paid:";
cin>>paid;
if(paid>=685.3)
{
principalminus = amort - (interest+mri);
advtemp = paid-amort;
}
principal=principal-principalminus;
adv=adv+advtemp;
cout<<"Interest:"<<interest<<endl;
cout<<"Principal Paid:"<<principalminus<<endl;
cout<<"Advance:"<<adv<<endl;
cout<<"Total Principal:"<<principal<<endl;
cout<<"Do you want to continue?:[Y/N]";
cin>>con;
cout<<endl<<endl;
}
while(con=='Y'||con=='y');
return 0;
}
it's 99855.7-145.022 but the answer is already rounded up
你只需要打印更多的数字,例如:
cout << std::setprecision(15);
在其他打印之前的某个地方,为此你需要
#include <iomanip>
你将拥有
pi@raspberrypi:/tmp $ ./a.out
CMP Calculator
Base Principal:100000
MRI:41
Monthly Amortization:685.3
Number of transaction:1
Amortization amount paid:686
Interest:500
Principal Paid:144.3
Advance:0.700000000000045
Total Principal:99855.7
Do you want to continue?:[Y/N]y
Number of transaction:2
Amortization amount paid:686
Interest:499.2785
Principal Paid:145.0215
Advance:1.40000000000009
Total Principal:99710.6785
Do you want to continue?:[Y/N]n
Total Principal 的打印值是正确的,无论如何根据定义理解任何浮点数的精度都有限制,您可以在打印值中看到前进.
如果您的编译器支持 long double
,您可以使用它们,同时要求打印 20 位数字,结果将是:
bruno@bruno-XPS-8300:/tmp$ ./a.out
CMP Calculator
Base Principal:100000
MRI:41
Monthly Amortization:685.3
Number of transaction:1
Amortization amount paid:686
Interest:499.99999999999998149
Principal Paid:144.30000000000001853
Advance:0.7000000000000000111
Total Principal:99855.699999999999982
Do you want to continue?:[Y/N]y
Number of transaction:2
Amortization amount paid:686
Interest:499.27849999999998143
Principal Paid:145.02150000000001856
Advance:1.4000000000000000222
Total Principal:99710.678499999999964
Do you want to continue?:[Y/N]n
还请注意,您错过了用 0 初始化 x,它可以是 int
而不是 double