浮点除法导致应为 1 的值变为 .9999999,因此将其设为 0 而不是 1,因此所需更改的计算不正确。处理这个问题的最简单方法是什么?(另外,如果有一些关于如何处理这个问题的指南/文章,我希望有一个链接供参考。) 例如。3.51 坏了。
两个版本的代码都有相同的问题。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//3.51 is broken due to floating point issue of .999999999 instead of 1. Work around?
int main(int argc, char *argv[])
{
const float DOLLARVALUE=1.0;
const float QUARTERVALUE=0.25;
const float DIMEVALUE=0.10;
const float NICKELVALUE=0.05;
const float PENNIEVALUE=0.01;
const int CELLWIDTH=12;
float totalChange=0;
int countDollars=0;
int countQuarters=0;
int countDimes=0;
int countNickels=0;
int countPennies=0;
float remainingTotal=0;
cout<<"Please insert total amount of change needed: ";
cin>>totalChange;
cout<<endl;
remainingTotal=totalChange;
countDollars=(remainingTotal/DOLLARVALUE);
remainingTotal=(remainingTotal-(countDollars*DOLLARVALUE));
countQuarters=(remainingTotal/QUARTERVALUE);
remainingTotal=(remainingTotal-(countQuarters*QUARTERVALUE));
countDimes=(remainingTotal/DIMEVALUE);
remainingTotal=(remainingTotal-(countDimes*DIMEVALUE));
countNickels=(remainingTotal/NICKELVALUE);
remainingTotal=(remainingTotal-(countNickels*NICKELVALUE));
countPennies=(remainingTotal/PENNIEVALUE);
remainingTotal=(remainingTotal-(countPennies*PENNIEVALUE));
//cout<<fixed<<setprecision(0);
cout<<"This amount can be given in change by:";
cout<<endl;
cout<<endl;
cout<<"Dollars"<<setw(CELLWIDTH)<<"Quarters";
cout<<setw(CELLWIDTH)<<"Dimes"<<setw(CELLWIDTH)<<"Nickles";
cout<<setw(CELLWIDTH)<<"Pennies";
cout<<endl;
cout<<"------------------------------------------------------";
cout<<endl;
cout<<setw(4)<<countDollars;
cout<<setw(CELLWIDTH)<<countQuarters;
cout<<setw(CELLWIDTH)<<countDimes;
cout<<setw(CELLWIDTH)<<countNickels;
cout<<setw(CELLWIDTH)<<countPennies;
cout<<endl;
cout<<endl;
return 0;
}
下一个版本以更明确的方式具有相同的问题。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
//3.51 is broken due to floating point issue of .999999999 instead of 1. Work around?
int main(int argc, char *argv[])
{
const float DOLLARVALUE=1.0;
const float QUARTERVALUE=0.25;
const float DIMEVALUE=0.10;
const float NICKELVALUE=0.05;
const float PENNIEVALUE=0.01;
const int CELLWIDTH=12;
float totalChange=0;
float countDollars=0;
float countQuarters=0;
float countDimes=0;
float countNickels=0;
float countPennies=0;
float remainingTotal=0;
cout<<"Please insert total amount of change needed: ";
cin>>totalChange;
cout<<endl;
remainingTotal=totalChange;
countDollars=floor(remainingTotal/DOLLARVALUE);
remainingTotal=(remainingTotal-(countDollars*DOLLARVALUE));
countQuarters=floor(remainingTotal/QUARTERVALUE);
remainingTotal=(remainingTotal-(countQuarters*QUARTERVALUE));
countDimes=floor(remainingTotal/DIMEVALUE);
remainingTotal=(remainingTotal-(countDimes*DIMEVALUE));
countNickels=floor(remainingTotal/NICKELVALUE);
remainingTotal=(remainingTotal-(countNickels*NICKELVALUE));
countPennies=floor(remainingTotal/PENNIEVALUE);
remainingTotal=(remainingTotal-(countPennies*PENNIEVALUE));
cout<<"Amount of change needed is: "<<totalChange;
cout<<endl;
cout<<endl;
//cout<<fixed<<setprecision(0);
cout<<"This amount can be given in change by:";
cout<<endl;
cout<<endl;
cout<<"Dollars"<<setw(CELLWIDTH)<<"Quarters";
cout<<setw(CELLWIDTH)<<"Dimes"<<setw(CELLWIDTH)<<"Nickles";
cout<<setw(CELLWIDTH)<<"Pennies";
cout<<endl;
cout<<"------------------------------------------------------";
cout<<endl;
cout<<setw(4)<<countDollars;
cout<<setw(CELLWIDTH)<<countQuarters;
cout<<setw(CELLWIDTH)<<countDimes;
cout<<setw(CELLWIDTH)<<countNickels;
cout<<setw(CELLWIDTH)<<countPennies;
cout<<endl;
cout<<endl;
return 0;
}
有一个简单的解决方法吗?我对所有想法持开放态度。