title: Floating Point division, floor, and when a simple C++ Change Computing program gets complex
body: <p>Floating point division is causing a value that should be 1 to be .9999999 thus flooring it becomes 0 instead of 1 so the calculation for the required change is incorrect. 
  What's the simplest way to deal with this? (Also if there's some sort of guide/article for how to deal with this I'd love a link for reference.)
  Ex. 3.51 is broken.</p>
  
  <p>Both versions of the code have the same issue.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;cmath&gt;
  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&lt;&lt;"Please insert total amount of change needed: ";
      cin&gt;&gt;totalChange;
      cout&lt;&lt;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&lt;&lt;fixed&lt;&lt;setprecision(0);
      cout&lt;&lt;"This amount can be given in change by:";
      cout&lt;&lt;endl;
      cout&lt;&lt;endl;
  
      cout&lt;&lt;"Dollars"&lt;&lt;setw(CELLWIDTH)&lt;&lt;"Quarters";
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;"Dimes"&lt;&lt;setw(CELLWIDTH)&lt;&lt;"Nickles";
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;"Pennies";
      cout&lt;&lt;endl;
      cout&lt;&lt;"------------------------------------------------------";
      cout&lt;&lt;endl;
  
      cout&lt;&lt;setw(4)&lt;&lt;countDollars;
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;countQuarters;
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;countDimes;
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;countNickels;
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;countPennies;
  
      cout&lt;&lt;endl;
      cout&lt;&lt;endl;
  
      return 0;
  }
  </code></pre>
  
  <p>The next version has the same issues in a more explicit way.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;cmath&gt;
  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&lt;&lt;"Please insert total amount of change needed: ";
      cin&gt;&gt;totalChange;
      cout&lt;&lt;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&lt;&lt;"Amount of change needed is: "&lt;&lt;totalChange;
      cout&lt;&lt;endl;
      cout&lt;&lt;endl;
      //cout&lt;&lt;fixed&lt;&lt;setprecision(0);
      cout&lt;&lt;"This amount can be given in change by:";
      cout&lt;&lt;endl;
      cout&lt;&lt;endl;
  
      cout&lt;&lt;"Dollars"&lt;&lt;setw(CELLWIDTH)&lt;&lt;"Quarters";
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;"Dimes"&lt;&lt;setw(CELLWIDTH)&lt;&lt;"Nickles";
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;"Pennies";
      cout&lt;&lt;endl;
      cout&lt;&lt;"------------------------------------------------------";
      cout&lt;&lt;endl;
  
  
  
      cout&lt;&lt;setw(4)&lt;&lt;countDollars;
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;countQuarters;
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;countDimes;
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;countNickels;
      cout&lt;&lt;setw(CELLWIDTH)&lt;&lt;countPennies;
  
      cout&lt;&lt;endl;
      cout&lt;&lt;endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Is there a simple workaround for this? I'm open to all ideas. </p>