1

I'm writing a simple program for my class to calculate and display certain numbers, and my code all works with no issue, however on the display some of my numbers are getting rounded where I don't want them to be. I have the precision set to two decimal places because a few of the input numbers are decimals, but for example, if the answer is 3000, the number 2999 is being displayed.

Here is my code, if this helps

int main()
{

    double expense = 0.0;
    double expenseTotal = 0.0;
    double income = 0.0;
    double incomeTotal = 0.0;

    double netProfit = 0.0;
    double netIncome = 0.0;
    double netExpense = 0.0;

    int incNo = -1;
    int expNo = -1;

    cout << "ENTER A NEGATIVE NUMBER TO STOP INCOME AMOUNT DATA ENTRY." << endl << endl;

    do
    {
        cout << "Enter Income Amount: ";
        cin >> income;
        netIncome = income;
        incomeTotal += netIncome;
        incNo += 1;

    } while (income > 0);

    cout << endl << "ENTER A NEGATIVE NUMBER TO STOP EXPENSE AMOUNT DATA ENTRY." << endl << endl;

    do
    {
        cout << "Enter Expense Amount: ";
        cin >> expense;
        netExpense = expense;
        expenseTotal += netExpense;
        expNo += 1;

    } while (expense > 0);


    cout << endl << "Total of the " << incNo << " income amounts entered ----> $" << fixed << setprecision(2) << incomeTotal << endl;
    cout <<"Total of the " << expNo << " expense amounts entered ----> $" << fixed << setprecision(2) << expenseTotal << endl;
    netProfit = incomeTotal - expenseTotal;

    if (netProfit > 0)
        {
            cout << "Net Profit earned ----> $" << fixed << setprecision(2) << netProfit;
            cout << endl;
        }

    else
        {
        cout << "Net Loss incurred ----> $" << fixed << setprecision(2) << netProfit;
        cout << endl;
        cout << "Note . . . Loss figire most be reported to company CEO." << endl << endl;
        }


    return 0;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Dariani Disani
  • 79
  • 2
  • 3
  • 9
  • You're using floating point numbers with all the loss in precision that that entails (see [this link](http://floating-point-gui.de/) for a full discussion). Your results are being truncated, hence the odd results. Multiply your inputs by 100 and truncate, then do all your arithmetic in integers before converting back at the end. –  Oct 14 '14 at 03:28
  • possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Drew McGowen Oct 14 '14 at 03:32
  • It's not true, the code is wrong. See my answer – Edwin Oct 14 '14 at 03:48

3 Answers3

2

It doesn't have to do with precision. Your code is wrong.

do {
    cout << "Enter Expense Amount: ";
    cin >> expense;             // --> what if users enter negative value to quit?
    netExpense = expense;       // --> hint: netExpense = netExpense + (negative value)
    expenseTotal += netExpense; // --> hint: expenseTotal = expenseTotal + (negative value)
    expNo += 1;
} while (expense > 0);

fix:

do {
    cout << "Enter Income Amount: ";
    cin >> income;
    if (income > 0){                // now when the input is negative
        netIncome = income;         // it will not change anything but quit
        incomeTotal += netIncome;
        incNo += 1;
    }     
} while (income > 0);

do the same for the other one

Edwin
  • 825
  • 1
  • 7
  • 15
-1

Welcome to "C/C++ floating precision club" buddy :) . It's just the way computers write down decimal numbers :)

This link should be useful:

http://www.parashift.com/c++-faq-lite/floating-pt-errs.html

Btw, printf does it bit better, try this one:

       printf("%.2f", netProfit);

That will do the trick you wanted ;)

@Limantara: code in the above code works for me.

Dushan Savich
  • 178
  • 15
-1

Welcome to the world of floating-point arithmetic. The first rule of floating point is: don't use it for currency. People really do care about what happens to their money, and with floating-point arithmetic you'll have endless trouble ensuring correctness due to rounding errors and imprecision. Floating-point numbers simply cannot accurately represent certain numbers that we use in our everyday lives, so you absolutely shouldn't use them in a scenario where accuracy is required (if this seems strange, look up how floating-point numbers are implemented).

So what should you use? Well, there are dedicated currency libraries for pretty much every language out there, but you can also just use arbitrary-precision libraries like GMP that function like Java's BigDecimal library.

If you have a guaranteed quantum of value you care about (and this is a bigger "if" than it might first appear) you can just use a long to represent multiples of that quantum (for example, a cent). But before you do that make sure you'll never need to care about fractions of that value (half a cent, etc).

Adam
  • 713
  • 1
  • 8
  • 16