0

浮点除法导致应为 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;
}

有一个简单的解决方法吗?我对所有想法持开放态度。

4

1 回答 1

0

有几种方法可以轻松解决此问题:

  1. 使用整数或有理数而不是浮点数。

  2. 在适用的情况下使用舍入而不是截断。您可以round(x)简单地实现为floor(x + 0.5). 不是特别适合您的情况,但它直接导致下一个。

  3. 您可以在小数点后四舍五入,然后再取整。假设您想将低于 0.9999 的任何值设为 0,但 0.9999 或更高的值应设为 1。那么您所需要的只是floor(x + 0.0001)使用floor(x).

于 2014-02-11T19:17:15.270 回答