C++ and doubles


  • I survived the hour long Uno hand

    I had my lab in a really stupid mandatory freshmen-level "intro to computer science" class this morning; the only thing that throws me from time to time is I haven't touched C++ since I was 12. However, even the teacher was a bit confused about a bug that was cropping up. Maybe someone here can explain it?

     

    int stock = 600;

    float commission = .02f; //2 percent commission on sales paid to broker

    double cost;

    cost = stock * price;

    cout << "Cost of stock alone: " << cost << endl;

    cout << "Commission: " << cost * commission << endl;

    cost += cost * commission;

    cout << "Total: " << cost;

     

    It would output:

    "Cost of stock alone: 13062

    Commission:  261.24

    Total: 13323.2"

     

    What happened to the 4 cents? Changing cost to a float didn't fix anything, nor did making comission a double if my classmates are to be believed. (I decided to skim ahead to the next chapter to find the syntax for setprecision and forced it to display all 7 digits). 



  • @yamikuronue said:

    I had my lab in a really stupid mandatory freshmen-level "intro to computer science" class this morning; the only thing that throws me from time to time is I haven't touched C++ since I was 12. However, even the teacher was a bit confused about a bug that was cropping up. Maybe someone here can explain it?

    Not a bug...@yamikuronue said:

    What happened to the 4 cents? Changing cost to a float didn't fix anything, nor did making comission a double if my classmates are to be believed. (I decided to skim ahead to the next chapter to find the syntax for setprecision and forced it to display all 7 digits). 

    http://tinyurl.com/cpmlvl

  • I survived the hour long Uno hand

     Interesting. That's why I asked - because I didn't know cout had a default precision. Thanks for the information!



  • Your bug is in using floating points as monetary units and expecting nicely formatted results.


  • I survived the hour long Uno hand

     Doubles didn't fix it. 



  • @yamikuronue said:

    What happened to the 4 cents? Changing cost to a float didn't fix anythin
     

    You're on the right track. If you feel like a reading assignment: http://dlc.sun.com/pdf//800-7895/800-7895.pdf

    In short, your default float precision for the output stream is 24 bits (or 6 significant digits).



  • @tster said:

    Your bug is in using floating points as monetary units and expecting [b]correct and[/b] nicely formatted results.



  • @yamikuronue said:

     Doubles didn't fix it. 

     

    That isn't his point.  The issue is that for calculations that must be absolutely precise, single and double precision types are inherently inaccurate.  So, while for most scientific computing, this is still ok, for accounting purposes, it can pose an issue.  Many higher-level languages will have a special "decimal" type for these purposes -- calculations are a little bit slower, but is much more correct.

    Now, for C and C++, go ahead and use floats and doubles, but then use .setprecision() or printf() to display the value *nicely*, keeping in mind that the value is still, technically, inaccurate.


Log in to reply